From 0ac60af2b2c591bb3adca2ea58c6ca5ecf307032 Mon Sep 17 00:00:00 2001 From: ioito Date: Thu, 2 Mar 2023 14:31:23 +0800 Subject: [PATCH] fix(keystone): filter users by role assignment domain --- cmd/climc/shell/identity/users.go | 11 ++++++----- pkg/apis/identity/input.go | 3 +++ pkg/keystone/models/users.go | 17 +++++++++++++++-- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/cmd/climc/shell/identity/users.go b/cmd/climc/shell/identity/users.go index ecc42305e3..90d4b73e88 100644 --- a/cmd/climc/shell/identity/users.go +++ b/cmd/climc/shell/identity/users.go @@ -27,11 +27,12 @@ import ( func init() { type UserListOptions struct { options.BaseListOptions - Name string `help:"Filter by name"` - OrderByDomain string `help:"order by domain name" choices:"asc|desc"` - Role string `help:"Filter by role"` - IdpId string `help:"filter by idp_id"` - IdpEntityId string `help:"filter by idp_entity_id"` + Name string `help:"Filter by name"` + OrderByDomain string `help:"order by domain name" choices:"asc|desc"` + Role string `help:"Filter by role"` + RoleAssignmentDomainId string `help:"filter role assignment domain"` + IdpId string `help:"filter by idp_id"` + IdpEntityId string `help:"filter by idp_entity_id"` } R(&UserListOptions{}, "user-list", "List users", func(s *mcclient.ClientSession, args *UserListOptions) error { params, err := options.ListStructToParams(args) diff --git a/pkg/apis/identity/input.go b/pkg/apis/identity/input.go index c56eb45559..f3a8681e22 100644 --- a/pkg/apis/identity/input.go +++ b/pkg/apis/identity/input.go @@ -189,6 +189,9 @@ type UserListInput struct { ProjectFilterListInput RoleFilterListInput + // 角色生效所在的域 + RoleAssignmentDomainId string `json:"role_assignment_domain_id"` + // email Email string `json:"email"` // mobile diff --git a/pkg/keystone/models/users.go b/pkg/keystone/models/users.go index cf884055fb..2b1e1b2a5e 100644 --- a/pkg/keystone/models/users.go +++ b/pkg/keystone/models/users.go @@ -413,8 +413,21 @@ func (manager *SUserManager) ListItemFilter( return nil, httperrors.NewGeneralError(err) } } - subq := AssignmentManager.fetchRoleUserIdsQuery(role.GetId()) - q = q.In("id", subq.SubQuery()) + + subq := AssignmentManager.Query("actor_id").Equals("role_id", role.GetId()).Equals("type", api.AssignmentUserProject).Distinct() + if len(query.RoleAssignmentDomainId) > 0 { + domain, err := DomainManager.FetchByIdOrName(userCred, query.RoleAssignmentDomainId) + if err != nil { + if err == sql.ErrNoRows { + return nil, httperrors.NewResourceNotFoundError2(DomainManager.Keyword(), query.RoleAssignmentDomainId) + } else { + return nil, httperrors.NewGeneralError(err) + } + } + projects := ProjectManager.Query("id").Equals("domain_id", domain.GetId()).SubQuery() + subq = subq.In("target_id", projects.Query()) + } + q = q.In("id", subq.SubQuery().Query()) } if len(query.IdpId) > 0 {