From 89d4ad7a6a095cd2ad76fd5d9a780a075a742784 Mon Sep 17 00:00:00 2001 From: Shivam-nagar23 Date: Fri, 27 Sep 2024 12:29:06 +0530 Subject: [PATCH] generics --- api/auth/user/UserRestHandler.go | 6 +++--- api/bean/UserRequest.go | 9 --------- pkg/auth/user/RoleGroupService.go | 2 +- pkg/auth/user/UserService.go | 4 ++-- pkg/auth/user/helper/helper.go | 23 ++++++++++++----------- 5 files changed, 18 insertions(+), 26 deletions(-) diff --git a/api/auth/user/UserRestHandler.go b/api/auth/user/UserRestHandler.go index e333f661feb..0f6ec76d5db 100644 --- a/api/auth/user/UserRestHandler.go +++ b/api/auth/user/UserRestHandler.go @@ -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 } @@ -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) } } @@ -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 } diff --git a/api/bean/UserRequest.go b/api/bean/UserRequest.go index a70c6f8cb1b..2ba15a68ca1 100644 --- a/api/bean/UserRequest.go +++ b/api/bean/UserRequest.go @@ -18,7 +18,6 @@ package bean import ( "encoding/json" - "fmt" "github.com/devtron-labs/devtron/pkg/auth/user/bean" "time" ) @@ -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"` @@ -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) -} diff --git a/pkg/auth/user/RoleGroupService.go b/pkg/auth/user/RoleGroupService.go index 732980c747e..60907866ae5 100644 --- a/pkg/auth/user/RoleGroupService.go +++ b/pkg/auth/user/RoleGroupService.go @@ -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) diff --git a/pkg/auth/user/UserService.go b/pkg/auth/user/UserService.go index b14d31a6de1..94e5509cb45 100644 --- a/pkg/auth/user/UserService.go +++ b/pkg/auth/user/UserService.go @@ -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) diff --git a/pkg/auth/user/helper/helper.go b/pkg/auth/user/helper/helper.go index 3737d26d621..b77cf0d1290 100644 --- a/pkg/auth/user/helper/helper.go +++ b/pkg/auth/user/helper/helper.go @@ -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) }