fix: filter of domain claimed users (#2752)

This commit is contained in:
Livio Amstutz 2021-11-29 17:36:24 +01:00 committed by GitHub
parent 209ea09e30
commit 7d45ae3c6c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 28 deletions

View File

@ -3,14 +3,14 @@ package admin
import ( import (
"context" "context"
"github.com/caos/zitadel/internal/api/authz" "google.golang.org/protobuf/types/known/timestamppb"
"github.com/caos/zitadel/internal/api/grpc/object" "github.com/caos/zitadel/internal/api/grpc/object"
org_grpc "github.com/caos/zitadel/internal/api/grpc/org" org_grpc "github.com/caos/zitadel/internal/api/grpc/org"
"github.com/caos/zitadel/internal/domain" "github.com/caos/zitadel/internal/domain"
usr_model "github.com/caos/zitadel/internal/user/model" usr_model "github.com/caos/zitadel/internal/user/model"
admin_pb "github.com/caos/zitadel/pkg/grpc/admin" admin_pb "github.com/caos/zitadel/pkg/grpc/admin"
obj_pb "github.com/caos/zitadel/pkg/grpc/object" obj_pb "github.com/caos/zitadel/pkg/grpc/object"
"google.golang.org/protobuf/types/known/timestamppb"
) )
func (s *Server) IsOrgUnique(ctx context.Context, req *admin_pb.IsOrgUniqueRequest) (*admin_pb.IsOrgUniqueResponse, error) { func (s *Server) IsOrgUnique(ctx context.Context, req *admin_pb.IsOrgUniqueRequest) (*admin_pb.IsOrgUniqueResponse, error) {
@ -68,12 +68,7 @@ func (s *Server) getClaimedUserIDsOfOrgDomain(ctx context.Context, orgDomain str
{ {
Key: usr_model.UserSearchKeyPreferredLoginName, Key: usr_model.UserSearchKeyPreferredLoginName,
Method: domain.SearchMethodEndsWithIgnoreCase, Method: domain.SearchMethodEndsWithIgnoreCase,
Value: orgDomain, Value: "@" + orgDomain,
},
{
Key: usr_model.UserSearchKeyResourceOwner,
Method: domain.SearchMethodNotEquals,
Value: authz.GetCtxData(ctx).OrgID,
}, },
}, },
}) })

View File

@ -50,7 +50,7 @@ func (s *Server) ListOrgChanges(ctx context.Context, req *mgmt_pb.ListOrgChanges
} }
func (s *Server) AddOrg(ctx context.Context, req *mgmt_pb.AddOrgRequest) (*mgmt_pb.AddOrgResponse, error) { func (s *Server) AddOrg(ctx context.Context, req *mgmt_pb.AddOrgRequest) (*mgmt_pb.AddOrgResponse, error) {
userIDs, err := s.getClaimedUserIDsOfOrgDomain(ctx, domain.NewIAMDomainName(req.Name, s.systemDefaults.Domain)) userIDs, err := s.getClaimedUserIDsOfOrgDomain(ctx, domain.NewIAMDomainName(req.Name, s.systemDefaults.Domain), "")
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -185,7 +185,7 @@ func GenerateOrgDomainValidationRequestToDomain(ctx context.Context, req *mgmt_p
} }
func (s *Server) ValidateOrgDomain(ctx context.Context, req *mgmt_pb.ValidateOrgDomainRequest) (*mgmt_pb.ValidateOrgDomainResponse, error) { func (s *Server) ValidateOrgDomain(ctx context.Context, req *mgmt_pb.ValidateOrgDomainRequest) (*mgmt_pb.ValidateOrgDomainResponse, error) {
userIDs, err := s.getClaimedUserIDsOfOrgDomain(ctx, req.Domain) userIDs, err := s.getClaimedUserIDsOfOrgDomain(ctx, req.Domain, authz.GetCtxData(ctx).OrgID)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -284,20 +284,24 @@ func (s *Server) RemoveOrgMember(ctx context.Context, req *mgmt_pb.RemoveOrgMemb
}, nil }, nil
} }
func (s *Server) getClaimedUserIDsOfOrgDomain(ctx context.Context, orgDomain string) ([]string, error) { func (s *Server) getClaimedUserIDsOfOrgDomain(ctx context.Context, orgDomain, orgID string) ([]string, error) {
users, err := s.user.SearchUsers(ctx, &usr_model.UserSearchRequest{ queries := []*usr_model.UserSearchQuery{
Queries: []*usr_model.UserSearchQuery{
{ {
Key: usr_model.UserSearchKeyPreferredLoginName, Key: usr_model.UserSearchKeyPreferredLoginName,
Method: domain.SearchMethodEndsWithIgnoreCase, Method: domain.SearchMethodEndsWithIgnoreCase,
Value: orgDomain, Value: "@" + orgDomain,
}, },
{ }
if orgID != "" {
queries = append(queries,
&usr_model.UserSearchQuery{
Key: usr_model.UserSearchKeyResourceOwner, Key: usr_model.UserSearchKeyResourceOwner,
Method: domain.SearchMethodNotEquals, Method: domain.SearchMethodNotEquals,
Value: authz.GetCtxData(ctx).OrgID, Value: orgID,
}, })
}, }
users, err := s.user.SearchUsers(ctx, &usr_model.UserSearchRequest{
Queries: queries,
}, false) }, false)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -168,12 +168,7 @@ func (l *Login) getClaimedUserIDsOfOrgDomain(ctx context.Context, orgName string
{ {
Key: usr_model.UserSearchKeyPreferredLoginName, Key: usr_model.UserSearchKeyPreferredLoginName,
Method: domain.SearchMethodEndsWithIgnoreCase, Method: domain.SearchMethodEndsWithIgnoreCase,
Value: domain.NewIAMDomainName(orgName, l.iamDomain), Value: "@" + domain.NewIAMDomainName(orgName, l.iamDomain),
},
{
Key: usr_model.UserSearchKeyResourceOwner,
Method: domain.SearchMethodNotEquals,
Value: authz.GetCtxData(ctx).OrgID,
}, },
}, },
}) })