Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions core/organization/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,11 @@ func (s Service) SetMemberRole(ctx context.Context, orgID, userID, newRoleID str
return ErrNotMember
}

// skip if the user already has exactly this role
if len(existingPolicies) == 1 && existingPolicies[0].RoleID == newRoleID {
Comment thread
AmanGIT07 marked this conversation as resolved.
return nil
}

// check minimum owner constraint
err = s.validateMinOwnerConstraint(ctx, orgID, newRoleID, existingPolicies)
if err != nil {
Expand Down
19 changes: 19 additions & 0 deletions core/organization/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,25 @@ func TestService_SetMemberRole(t *testing.T) {
newRoleID: ownerRoleID,
wantErr: nil,
},
{
name: "should skip delete+create when role is unchanged",
setup: func(repo *mocks.Repository, userSvc *mocks.UserService, roleSvc *mocks.RoleService, policySvc *mocks.PolicyService, _ *mocks.AuditRecordRepository) {
repo.EXPECT().GetByID(ctx, orgID).Return(organization.Organization{ID: orgID, State: organization.Enabled}, nil)
userSvc.EXPECT().GetByID(ctx, userID).Return(user.User{ID: userID}, nil)
roleSvc.EXPECT().Get(ctx, memberRoleID).Return(role.Role{ID: memberRoleID, Name: "member", Scopes: []string{schema.OrganizationNamespace}}, nil)
// user already has the same role
policySvc.EXPECT().List(ctx, policy.Filter{
OrgID: orgID,
PrincipalID: userID,
PrincipalType: schema.UserPrincipal,
}).Return([]policy.Policy{{ID: "policy-1", RoleID: memberRoleID}}, nil)
// no Delete or Create should be called — early return
},
orgID: orgID,
userID: userID,
newRoleID: memberRoleID,
wantErr: nil,
},
}

for _, tt := range tests {
Expand Down
5 changes: 5 additions & 0 deletions core/project/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,11 @@ func (s Service) SetMemberRole(ctx context.Context, projectID, principalID, prin
return err
}

// skip if the principal already has exactly this role
if len(existingPolicies) == 1 && existingPolicies[0].RoleID == newRoleID {
return nil
}

for _, p := range existingPolicies {
if err := s.policyService.Delete(ctx, p.ID); err != nil {
return err
Expand Down
21 changes: 21 additions & 0 deletions core/project/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1215,6 +1215,27 @@ func TestService_SetMemberRole(t *testing.T) {
},
wantErr: nil,
},
{
name: "should skip delete+create when role is unchanged",
projectID: projectID,
principalID: userID,
principalType: schema.UserPrincipal,
roleID: roleID,
setup: func(repo *mocks.Repository, userSvc *mocks.UserService, suserSvc *mocks.ServiceuserService, groupSvc *mocks.GroupService, policySvc *mocks.PolicyService, roleSvc *mocks.RoleService) {
repo.EXPECT().GetByID(ctx, projectID).Return(project.Project{ID: projectID, Organization: organization.Organization{ID: orgID}}, nil)
userSvc.EXPECT().GetByID(ctx, userID).Return(user.User{ID: userID}, nil)
policySvc.EXPECT().List(ctx, policy.Filter{
OrgID: orgID, PrincipalID: userID, PrincipalType: schema.UserPrincipal,
}).Return([]policy.Policy{{ID: "org-p1"}}, nil)
roleSvc.EXPECT().Get(ctx, roleID).Return(role.Role{ID: roleID, Scopes: []string{schema.ProjectNamespace}}, nil)
// user already has the same role on this project
policySvc.EXPECT().List(ctx, policy.Filter{
ProjectID: projectID, PrincipalID: userID, PrincipalType: schema.UserPrincipal,
}).Return([]policy.Policy{{ID: "existing-p1", RoleID: roleID}}, nil)
// no Delete or Create should be called — early return
},
wantErr: nil,
},
}

for _, tt := range tests {
Expand Down
Loading