From c258e0473b613534db02e11e15b78399d8505e55 Mon Sep 17 00:00:00 2001 From: richjohnrambo Date: Wed, 16 Sep 2026 13:19:55 +0800 Subject: [PATCH] fix: resolve bound im tenant for company admins --- server/internal/logic/sys/im_admin_scope.go | 6 +++--- .../internal/logic/sys/im_admin_scope_test.go | 21 +++++++++++++++++-- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/server/internal/logic/sys/im_admin_scope.go b/server/internal/logic/sys/im_admin_scope.go index 8022642..6091cd5 100644 --- a/server/internal/logic/sys/im_admin_scope.go +++ b/server/internal/logic/sys/im_admin_scope.go @@ -53,7 +53,7 @@ func resolveImAdminScopeWithAll(ctx context.Context, selectedTenantId int64, all return scope, nil } - if !contexts.IsTenantDept(ctx) { + if !contexts.IsTenantDept(ctx) && !(contexts.IsCompanyDept(ctx) && contexts.GetTenantId(ctx) > 0) { return scope, gerror.New("当前账号无权管理IM租户") } tenantId, err := resolveImAdminBoundTenant(ctx) @@ -68,10 +68,10 @@ func resolveImAdminScopeWithAll(ctx context.Context, selectedTenantId int64, all } func isImPlatformAdmin(ctx context.Context, operatorId int64) bool { - if contexts.IsCompanyDept(ctx) { + if service.AdminMember().VerifySuperId(ctx, operatorId) { return true } - return service.AdminMember().VerifySuperId(ctx, operatorId) + return contexts.IsCompanyDept(ctx) && contexts.GetTenantId(ctx) <= 0 } func resolveImAdminBoundTenant(ctx context.Context) (int64, error) { diff --git a/server/internal/logic/sys/im_admin_scope_test.go b/server/internal/logic/sys/im_admin_scope_test.go index 4b8e97d..d61f648 100644 --- a/server/internal/logic/sys/im_admin_scope_test.go +++ b/server/internal/logic/sys/im_admin_scope_test.go @@ -8,12 +8,21 @@ import ( "hotgo/internal/model" ) -func TestCompanyDepartmentIsImPlatformAdmin(t *testing.T) { +func TestBoundCompanyDepartmentUsesBoundTenant(t *testing.T) { ctx := context.WithValue(context.Background(), consts.ContextHTTPKey, &model.Context{ User: &model.Identity{Id: 14, TenantId: 1, DeptType: consts.DeptTypeCompany, RoleKey: "imManage"}, }) + if isImPlatformAdmin(ctx, 14) { + t.Fatal("expected bound company department IM manager to use its bound tenant") + } +} + +func TestUnboundCompanyDepartmentIsImPlatformAdmin(t *testing.T) { + ctx := context.WithValue(context.Background(), consts.ContextHTTPKey, &model.Context{ + User: &model.Identity{Id: 14, DeptType: consts.DeptTypeCompany, RoleKey: "imManage"}, + }) if !isImPlatformAdmin(ctx, 14) { - t.Fatal("expected company department IM manager to use platform tenant selection") + t.Fatal("expected unbound company department IM manager to use platform tenant selection") } } @@ -31,6 +40,14 @@ func TestResolveImAdminBoundTenant(t *testing.T) { }, want: 2, }, + { + name: "company department with tenant binding", + identity: &model.Identity{ + TenantId: 1, + DeptType: consts.DeptTypeCompany, + }, + want: 1, + }, } for _, tt := range tests {