Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix. fix update role name error #424

Merged
merged 2 commits into from
Apr 24, 2024
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
24 changes: 24 additions & 0 deletions internal/keycloak/keycloak.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type IKeycloak interface {
LeaveGroup(ctx context.Context, organizationId string, userId string, groupName string) error
CreateGroup(ctx context.Context, organizationId string, groupName string) (string, error)
DeleteGroup(ctx context.Context, organizationId string, groupName string) error
UpdateGroup(ctx context.Context, organizationId string, oldGroupName string, newGroupName string) error
EnsureClientRoleWithClientName(ctx context.Context, organizationId string, clientName string, roleName string) error
DeleteClientRoleWithClientName(ctx context.Context, organizationId string, clientName string, roleName string) error

Expand Down Expand Up @@ -85,6 +86,29 @@ func (k *Keycloak) DeleteGroup(ctx context.Context, organizationId string, group
return nil
}

func (k *Keycloak) UpdateGroup(ctx context.Context, organizationId string, oldGroupName string, newGroupName string) error {
token := k.adminCliToken
groups, err := k.client.GetGroups(context.Background(), token.AccessToken, organizationId, gocloak.GetGroupsParams{
Search: &oldGroupName,
})
if err != nil {
log.Error(ctx, err)
return httpErrors.NewInternalServerError(err, "", "")
}
if len(groups) == 0 {
return httpErrors.NewNotFoundError(fmt.Errorf("group not found"), "", "")
}
err = k.client.UpdateGroup(context.Background(), token.AccessToken, organizationId, gocloak.Group{
ID: groups[0].ID,
Name: gocloak.StringP(newGroupName + "@" + organizationId),
})
if err != nil {
log.Error(ctx, err)
return httpErrors.NewInternalServerError(err, "", "")
}
return nil
}

func (k *Keycloak) LoginAdmin(ctx context.Context, accountId string, password string) (*model.User, error) {
JWTToken, err := k.client.LoginAdmin(context.Background(), accountId, password, DefaultMasterRealm)
if err != nil {
Expand Down
12 changes: 10 additions & 2 deletions internal/usecase/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,16 @@ func (r RoleUsecase) DeleteTksRole(ctx context.Context, organizationId string, i
return r.repo.Delete(ctx, id)
}

func (r RoleUsecase) UpdateTksRole(ctx context.Context, role *model.Role) error {
err := r.repo.Update(ctx, role)
func (r RoleUsecase) UpdateTksRole(ctx context.Context, newRole *model.Role) error {
role, err := r.repo.GetTksRole(ctx, newRole.OrganizationID, newRole.ID)
if err != nil {
return err
}
err = r.kc.UpdateGroup(ctx, role.OrganizationID, role.Name, newRole.Name)
if err != nil {
return err
}
err = r.repo.Update(ctx, newRole)
if err != nil {
return err
}
Expand Down
Loading