Skip to content

Commit

Permalink
generics
Browse files Browse the repository at this point in the history
  • Loading branch information
Shivam-nagar23 committed Sep 27, 2024
1 parent 21f322e commit 89d4ad7
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 26 deletions.
6 changes: 3 additions & 3 deletions api/auth/user/UserRestHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -1186,7 +1186,7 @@ func (handler UserRestHandlerImpl) checkRBACForUserUpdate(token string, userInfo
if !isAuthorised {
if roleFilters != nil && len(roleFilters) > 0 { //auth check inside roleFilters
for _, filter := range roleFilters {
if _, ok := mapOfExistingRoleFilter[filter.GetUniqueKey()]; ok {
if _, ok := mapOfExistingRoleFilter[helper.GetUniqueKeyForRoleFilter(filter)]; ok {
isAuthorised = true
continue
}
Expand Down Expand Up @@ -1229,7 +1229,7 @@ func (handler UserRestHandlerImpl) checkRBACForUserUpdate(token string, userInfo
if len(roleGroups) > 0 { // auth check inside groups
filteredRoleGroupsForRbac := make([]bean.UserRoleGroup, 0, len(roleGroups))
for _, group := range roleGroups {
if _, ok := mapOfExistingUserGroup[group.GetUniqueKey()]; !ok {
if _, ok := mapOfExistingUserGroup[helper.GetUniqueKeyForUserGroup(group)]; !ok {
filteredRoleGroupsForRbac = append(filteredRoleGroupsForRbac, group)
}
}
Expand Down Expand Up @@ -1282,7 +1282,7 @@ func (handler UserRestHandlerImpl) checkRBACForRoleGroupUpdate(token string, gro
if !isAuthorised {
if groupInfo.RoleFilters != nil && len(groupInfo.RoleFilters) > 0 { //auth check inside roleFilters
for _, filter := range groupInfo.RoleFilters {
if _, ok := mapOfExitingRoleFiltersKey[filter.GetUniqueKey()]; ok {
if _, ok := mapOfExitingRoleFiltersKey[helper.GetUniqueKeyForRoleFilter(filter)]; ok {
isAuthorised = true
continue
}
Expand Down
9 changes: 0 additions & 9 deletions api/bean/UserRequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package bean

import (
"encoding/json"
"fmt"
"github.com/devtron-labs/devtron/pkg/auth/user/bean"
"time"
)
Expand Down Expand Up @@ -73,10 +72,6 @@ type RoleFilter struct {
Workflow string `json:"workflow"`
}

func (roleFilter RoleFilter) GetUniqueKey() string {
return fmt.Sprintf("%s-%s-%s-%s-%s-%s-%s-%s-%s-%s-%s-%s", roleFilter.Entity, roleFilter.Team, roleFilter.EntityName, roleFilter.Environment, roleFilter.Action, roleFilter.AccessType, roleFilter.Cluster, roleFilter.Namespace, roleFilter.Group, roleFilter.Kind, roleFilter.Resource, roleFilter.Workflow)
}

type Role struct {
Id int `json:"id" validate:"number"`
Role string `json:"role" validate:"required"`
Expand Down Expand Up @@ -160,7 +155,3 @@ type BulkDeleteRequest struct {
type UserRoleGroup struct {
RoleGroup *RoleGroup `json:"roleGroup"`
}

func (group UserRoleGroup) GetUniqueKey() string {
return fmt.Sprintf("%d-%s", group.RoleGroup.Id, group.RoleGroup.Name)
}
2 changes: 1 addition & 1 deletion pkg/auth/user/RoleGroupService.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ func (impl RoleGroupServiceImpl) UpdateRoleGroup(request *bean.RoleGroup, token
impl.logger.Errorw("error encountered in Update role group", "err", err, "roleGroupId", roleGroup.Id)
return nil, err
}
mapOfExitingRoleFiltersKey := helper2.GetMapOfUniqueRoleFilterKey(existingRoleGroupData.RoleFilters)
mapOfExitingRoleFiltersKey := helper2.GetMapOfUniqueKeys(existingRoleGroupData.RoleFilters, helper2.GetUniqueKeyForRoleFilter)
isAuthorised, err := checkRBACForGroupUpdate(token, request, eliminatedRoleModels, mapOfExitingRoleFiltersKey)
if err != nil {
impl.logger.Errorw("error in checking RBAC for role group update", "err", err, "request", request)
Expand Down
4 changes: 2 additions & 2 deletions pkg/auth/user/UserService.go
Original file line number Diff line number Diff line change
Expand Up @@ -808,8 +808,8 @@ func (impl *UserServiceImpl) UpdateUser(userInfo *bean.UserInfo, token string, c
impl.logger.Errorw("error while fetching user from db", "error", err)
return nil, err
}
uniqueRolefilterKeyMap := userHelper.GetMapOfUniqueRoleFilterKey(existingUserInfo.RoleFilters)
existingRoleGroupKeyMap := userHelper.GetMapOfUniqueUserRoleGroup(existingUserInfo.UserRoleGroup)
uniqueRolefilterKeyMap := userHelper.GetMapOfUniqueKeys(existingUserInfo.RoleFilters, userHelper.GetUniqueKeyForRoleFilter)
existingRoleGroupKeyMap := userHelper.GetMapOfUniqueKeys(existingUserInfo.UserRoleGroup, userHelper.GetUniqueKeyForUserGroup)
isAuthorised, err := checkRBACForUserUpdate(token, userInfo, isUserSuperAdmin, eliminatedRoles, eliminatedGroupRoles, uniqueRolefilterKeyMap, existingRoleGroupKeyMap)
if err != nil {
impl.logger.Errorw("error in checking RBAC for user update", "err", err, "userInfo", userInfo)
Expand Down
23 changes: 12 additions & 11 deletions pkg/auth/user/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,19 @@ func CreateErrorMessageForUserRoleGroups(restrictedGroups []bean2.RestrictedGrou
return errorMessageForGroupsWithoutSuperAdmin, errorMessageForGroupsWithSuperAdmin
}

func GetMapOfUniqueRoleFilterKey(roleFilters []bean2.RoleFilter) map[string]bool {
uniqueRoleFilterKeyMap := make(map[string]bool, len(roleFilters))
for _, roleFilter := range roleFilters {
uniqueRoleFilterKeyMap[roleFilter.GetUniqueKey()] = true
// GetMapOfUniqueKeys takes a slice of any type and a function to extract a unique key, returning a map of unique keys.
func GetMapOfUniqueKeys[T any](items []T, getKeyFunc func(T) string) map[string]bool {
uniqueKeyMap := make(map[string]bool, len(items))
for _, item := range items {
uniqueKeyMap[getKeyFunc(item)] = true
}
return uniqueRoleFilterKeyMap
return uniqueKeyMap
}

func GetMapOfUniqueUserRoleGroup(userRoleGroups []bean2.UserRoleGroup) map[string]bool {
uniqueUserRoleGroupKeyMap := make(map[string]bool, len(userRoleGroups))
for _, userRoleGroup := range userRoleGroups {
uniqueUserRoleGroupKeyMap[userRoleGroup.GetUniqueKey()] = true
}
return uniqueUserRoleGroupKeyMap
func GetUniqueKeyForRoleFilter(roleFilter bean2.RoleFilter) string {
return fmt.Sprintf("%s-%s-%s-%s-%s-%s-%s-%s-%s-%s-%s-%s", roleFilter.Entity, roleFilter.Team, roleFilter.EntityName, roleFilter.Environment, roleFilter.Action, roleFilter.AccessType, roleFilter.Cluster, roleFilter.Namespace, roleFilter.Group, roleFilter.Kind, roleFilter.Resource, roleFilter.Workflow)
}

func GetUniqueKeyForUserGroup(group bean2.UserRoleGroup) string {
return fmt.Sprintf("%d-%s", group.RoleGroup.Id, group.RoleGroup.Name)
}

0 comments on commit 89d4ad7

Please sign in to comment.