Skip to content

Commit 39e8f75

Browse files
authored
fix: updated rbac for notification and permission group apis (#2497)
* updated rbac in notification recipients fetch api * updated rbac * updated error handling * updated rbac for permission group fetch api
1 parent 9b6de9e commit 39e8f75

File tree

2 files changed

+57
-4
lines changed

2 files changed

+57
-4
lines changed

api/restHandler/NotificationRestHandler.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,11 @@ func (impl NotificationRestHandlerImpl) FindAllNotificationConfig(w http.Respons
576576
return
577577
}
578578

579+
if ok := impl.enforcer.Enforce(token, casbin.ResourceNotification, casbin.ActionGet, "*"); !ok {
580+
// if user does not have notification level access then return unauthorized
581+
common.WriteJsonResp(w, fmt.Errorf("unauthorized user"), nil, http.StatusForbidden)
582+
return
583+
}
579584
//RBAC
580585
pass := true
581586
if len(slackConfigs) > 0 {
@@ -602,10 +607,6 @@ func (impl NotificationRestHandlerImpl) FindAllNotificationConfig(w http.Respons
602607
if pass {
603608
channelsResponse.SlackConfigs = slackConfigs
604609
}
605-
606-
if ok := impl.enforcer.Enforce(token, casbin.ResourceNotification, casbin.ActionGet, "*"); !ok {
607-
pass = false
608-
}
609610
sesConfigs, fErr := impl.sesService.FetchAllSESNotificationConfig()
610611
if fErr != nil && fErr != pg.ErrNoRows {
611612
impl.logger.Errorw("service err, FindAllNotificationConfig", "err", err)
@@ -723,6 +724,11 @@ func (impl NotificationRestHandlerImpl) RecipientListingSuggestion(w http.Respon
723724
common.WriteJsonResp(w, err, "Unauthorized User", http.StatusUnauthorized)
724725
return
725726
}
727+
token := r.Header.Get("token")
728+
if ok := impl.enforcer.Enforce(token, casbin.ResourceNotification, casbin.ActionGet, "*"); !ok {
729+
common.WriteJsonResp(w, errors.New("unauthorized"), "Forbidden", http.StatusForbidden)
730+
return
731+
}
726732
vars := mux.Vars(r)
727733
value := vars["value"]
728734
//var teams []int

api/user/UserRestHandler.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,53 @@ func (handler UserRestHandlerImpl) FetchRoleGroups(w http.ResponseWriter, r *htt
557557
common.WriteJsonResp(w, err, "Unauthorized User", http.StatusUnauthorized)
558558
return
559559
}
560+
// RBAC enforcer applying
561+
token := r.Header.Get("token")
562+
isAuthorised := false
563+
//checking superAdmin access
564+
isAuthorised, err = handler.userService.IsSuperAdmin(int(userId))
565+
if err != nil {
566+
handler.logger.Errorw("error in checking superAdmin access of user", "err", err)
567+
common.WriteJsonResp(w, err, "", http.StatusInternalServerError)
568+
return
569+
}
570+
if !isAuthorised {
571+
user, err := handler.userService.GetById(userId)
572+
if err != nil {
573+
handler.logger.Errorw("error in getting user by id", "err", err)
574+
common.WriteJsonResp(w, err, "", http.StatusInternalServerError)
575+
return
576+
}
577+
var roleFilters []bean.RoleFilter
578+
if len(user.Groups) > 0 {
579+
groupRoleFilters, err := handler.userService.GetRoleFiltersByGroupNames(user.Groups)
580+
if err != nil {
581+
handler.logger.Errorw("Error in getting role filters by group names", "err", err, "groupNames", user.Groups)
582+
common.WriteJsonResp(w, err, "", http.StatusInternalServerError)
583+
return
584+
}
585+
if len(groupRoleFilters) > 0 {
586+
roleFilters = append(roleFilters, groupRoleFilters...)
587+
}
588+
}
589+
if user.RoleFilters != nil && len(user.RoleFilters) > 0 {
590+
roleFilters = append(roleFilters, user.RoleFilters...)
591+
}
592+
if len(roleFilters) > 0 {
593+
for _, filter := range roleFilters {
594+
if len(filter.Team) > 0 {
595+
if ok := handler.enforcer.Enforce(token, casbin.ResourceUser, casbin.ActionGet, strings.ToLower(filter.Team)); ok {
596+
isAuthorised = true
597+
break
598+
}
599+
}
600+
}
601+
}
602+
}
603+
if !isAuthorised {
604+
common.WriteJsonResp(w, errors.New("unauthorized"), nil, http.StatusForbidden)
605+
return
606+
}
560607
res, err := handler.roleGroupService.FetchRoleGroups()
561608
if err != nil {
562609
handler.logger.Errorw("service err, FetchRoleGroups", "err", err)

0 commit comments

Comments
 (0)