Skip to content

Commit 0cf295c

Browse files
authored
Merge pull request #55 from devtron-labs/user-get-forbidden-issue
User get for super admin forbidden issue
2 parents 7d6cfb5 + 4f16b9f commit 0cf295c

File tree

2 files changed

+43
-22
lines changed

2 files changed

+43
-22
lines changed

Diff for: api/restHandler/UserRestHandler.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,11 @@ func (handler UserRestHandlerImpl) UpdateUser(w http.ResponseWriter, r *http.Req
192192
}
193193

194194
func (handler UserRestHandlerImpl) GetById(w http.ResponseWriter, r *http.Request) {
195+
userId, err := handler.userService.GetLoggedInUser(r)
196+
if userId == 0 || err != nil {
197+
writeJsonResp(w, err, "Unauthorized User", http.StatusUnauthorized)
198+
return
199+
}
195200
vars := mux.Vars(r)
196201
/* #nosec */
197202
id, err := strconv.Atoi(vars["id"])
@@ -206,7 +211,12 @@ func (handler UserRestHandlerImpl) GetById(w http.ResponseWriter, r *http.Reques
206211
writeJsonResp(w, err, "Failed to get by id", http.StatusInternalServerError)
207212
return
208213
}
209-
214+
isActionUserSuperAdmin, err := handler.userService.IsSuperAdmin(int(userId))
215+
if err != nil {
216+
handler.logger.Errorw("service err, GetById", "err", err, "id", id)
217+
writeJsonResp(w, err, "Failed to check is super admin", http.StatusInternalServerError)
218+
return
219+
}
210220
// NOTE: if no role assigned, user will be visible to all manager.
211221
// RBAC enforcer applying
212222
token := r.Header.Get("token")
@@ -219,6 +229,9 @@ func (handler UserRestHandlerImpl) GetById(w http.ResponseWriter, r *http.Reques
219229
}
220230
}
221231
}
232+
if isActionUserSuperAdmin {
233+
authPass = true
234+
}
222235
if authPass == false {
223236
response.WriteResponse(http.StatusForbidden, "FORBIDDEN", w, errors.New("unauthorized"))
224237
return

Diff for: pkg/user/UserService.go

+29-21
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ type UserService interface {
4949
CheckUserRoles(id int32) ([]string, error)
5050
SyncOrchestratorToCasbin() (bool, error)
5151
GetUserByToken(token string) (int32, error)
52+
IsSuperAdmin(userId int) (bool, error)
5253
}
5354

5455
type UserServiceImpl struct {
@@ -311,41 +312,27 @@ func (impl UserServiceImpl) CreateUser(userInfo *bean.UserInfo) ([]*bean.UserInf
311312
}
312313

313314
func (impl UserServiceImpl) UpdateUser(userInfo *bean.UserInfo) (*bean.UserInfo, error) {
314-
315315
//validating if action user is not admin and trying to update user who has super admin polices, return 403
316-
isSuperAdmin := false
317-
userCasbinRoles, err := impl.CheckUserRoles(userInfo.Id)
316+
isUserSuperAdmin, err := impl.IsSuperAdmin(int(userInfo.Id))
318317
if err != nil {
319318
return nil, err
320319
}
321-
actionUserRoles, err := impl.CheckUserRoles(userInfo.UserId)
320+
isActionPerformingUserSuperAdmin, err := impl.IsSuperAdmin(int(userInfo.UserId))
322321
if err != nil {
323322
return nil, err
324323
}
325324
//if request comes to make user as a super admin, action performing user also be super admin
326325
if userInfo.SuperAdmin {
327-
for _, item := range actionUserRoles {
328-
if item == bean.SUPERADMIN {
329-
isSuperAdmin = true
330-
}
331-
}
332-
if isSuperAdmin == false {
326+
if !isUserSuperAdmin {
333327
err = &util.ApiError{HttpStatusCode: http.StatusForbidden, UserMessage: "Invalid request, not allow to update super admin type user"}
334328
return nil, err
335329
}
336330
}
337331
//if user which going to updated is super admin, action performing user also be super admin
338-
for _, item := range userCasbinRoles {
339-
if item == bean.SUPERADMIN {
340-
for _, item := range actionUserRoles {
341-
if item == bean.SUPERADMIN {
342-
isSuperAdmin = true
343-
}
344-
}
345-
if isSuperAdmin == false {
346-
err = &util.ApiError{HttpStatusCode: http.StatusForbidden, UserMessage: "Invalid request, not allow to update super admin type user"}
347-
return nil, err
348-
}
332+
if isUserSuperAdmin {
333+
if !isActionPerformingUserSuperAdmin {
334+
err = &util.ApiError{HttpStatusCode: http.StatusForbidden, UserMessage: "Invalid request, not allow to update super admin type user"}
335+
return nil, err
349336
}
350337
}
351338

@@ -525,6 +512,10 @@ func (impl UserServiceImpl) UpdateUser(userInfo *bean.UserInfo) (*bean.UserInfo,
525512
//ROLE GROUP SETUP
526513
newGroupMap := make(map[string]string)
527514
oldGroupMap := make(map[string]string)
515+
userCasbinRoles, err := impl.CheckUserRoles(userInfo.Id)
516+
if err != nil {
517+
return nil, err
518+
}
528519
for _, oldItem := range userCasbinRoles {
529520
oldGroupMap[oldItem] = oldItem
530521
}
@@ -944,3 +935,20 @@ func (impl UserServiceImpl) SyncOrchestratorToCasbin() (bool, error) {
944935
}*/
945936
return true, nil
946937
}
938+
939+
func (impl UserServiceImpl) IsSuperAdmin(userId int) (bool, error) {
940+
//validating if action user is not admin and trying to update user who has super admin polices, return 403
941+
isSuperAdmin := false
942+
userCasbinRoles, err := impl.CheckUserRoles(int32(userId))
943+
if err != nil {
944+
return isSuperAdmin, err
945+
}
946+
//if user which going to updated is super admin, action performing user also be super admin
947+
for _, item := range userCasbinRoles {
948+
if item == bean.SUPERADMIN {
949+
isSuperAdmin = true
950+
break
951+
}
952+
}
953+
return isSuperAdmin, nil
954+
}

0 commit comments

Comments
 (0)