Skip to content

Commit

Permalink
refactor: Fix authorization error in updateClientPolicy
Browse files Browse the repository at this point in the history
Signed-off-by: JeffMboya <jangina.mboya@gmail.com>
  • Loading branch information
JeffMboya committed May 22, 2024
1 parent 7ab0c20 commit 4c5cd54
Showing 1 changed file with 5 additions and 13 deletions.
18 changes: 5 additions & 13 deletions users/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,9 +386,14 @@ func (svc service) UpdateClientRole(ctx context.Context, token string, cli mgcli
UpdatedBy: tokenUserID,
}

if _, err := svc.authorize(ctx, auth.UserType, auth.UsersKind, client.ID, auth.MembershipPermission, auth.PlatformType, auth.MagistralaObject); err != nil {
return mgclients.Client{}, err
}

if err := svc.updateClientPolicy(ctx, cli.ID, cli.Role); err != nil {
return mgclients.Client{}, err
}

client, err = svc.clients.UpdateRole(ctx, client)
if err != nil {
// If failed to update role in DB, then revert back to platform admin policy in spicedb
Expand Down Expand Up @@ -694,19 +699,6 @@ func (svc service) addClientPolicyRollback(ctx context.Context, userID string, r
}

func (svc service) updateClientPolicy(ctx context.Context, userID string, role mgclients.Role) error {
res, err := svc.auth.Authorize(ctx, &magistrala.AuthorizeReq{
SubjectType: auth.UserType,
Subject: userID,
Permission: auth.MembershipPermission,
ObjectType: auth.PlatformType,
Object: auth.MagistralaObject,
})
if err != nil {
return err
}
if !res.Authorized {
return svcerr.ErrAuthorization
}
switch role {
case mgclients.AdminRole:
resp, err := svc.auth.AddPolicy(ctx, &magistrala.AddPolicyReq{
Expand Down

0 comments on commit 4c5cd54

Please sign in to comment.