@@ -35,6 +35,13 @@ type GetCustomizeCategoriesOutput struct {
3535 Body []category.Category
3636}
3737
38+ var customizeCategoryPermissionsInternal = map [string ][]string {
39+ "templates" : {authz .PermTemplatesList , authz .PermTemplatesRead },
40+ "registries" : {authz .PermRegistriesList , authz .PermRegistriesRead },
41+ "variables" : {authz .PermTemplatesRead },
42+ "git-repositories" : {authz .PermGitReposList , authz .PermGitReposRead },
43+ }
44+
3845// RegisterCustomize registers customization endpoints using Huma.
3946func RegisterCustomize (api huma.API , customizeSearchService * services.CustomizeSearchService ) {
4047 h := & CustomizeHandler {
@@ -52,7 +59,6 @@ func RegisterCustomize(api huma.API, customizeSearchService *services.CustomizeS
5259 {"BearerAuth" : {}},
5360 {"ApiKeyAuth" : {}},
5461 },
55- Middlewares : humamw .RequirePermission (api , authz .PermCustomizeManage ),
5662 }, h .Search )
5763
5864 huma .Register (api , huma.Operation {
@@ -66,10 +72,41 @@ func RegisterCustomize(api huma.API, customizeSearchService *services.CustomizeS
6672 {"BearerAuth" : {}},
6773 {"ApiKeyAuth" : {}},
6874 },
69- Middlewares : humamw .RequirePermission (api , authz .PermCustomizeManage ),
7075 }, h .GetCategories )
7176}
7277
78+ func canAccessCustomizeCategoryInternal (ps * authz.PermissionSet , categoryID string ) bool {
79+ if ps == nil {
80+ return false
81+ }
82+ if ps .Allows (authz .PermCustomizeManage , "" ) {
83+ return true
84+ }
85+ perms , ok := customizeCategoryPermissionsInternal [categoryID ]
86+ if ! ok {
87+ return false
88+ }
89+ for _ , perm := range perms {
90+ if ps .Allows (perm , "" ) {
91+ return true
92+ }
93+ }
94+ return false
95+ }
96+
97+ func filterCustomizeCategoriesInternal (ps * authz.PermissionSet , categories []category.Category ) []category.Category {
98+ if ps == nil {
99+ return []category.Category {}
100+ }
101+ filtered := make ([]category.Category , 0 , len (categories ))
102+ for _ , cat := range categories {
103+ if canAccessCustomizeCategoryInternal (ps , cat .ID ) {
104+ filtered = append (filtered , cat )
105+ }
106+ }
107+ return filtered
108+ }
109+
73110// Search searches customization options by query.
74111func (h * CustomizeHandler ) Search (ctx context.Context , input * SearchCustomizeInput ) (* SearchCustomizeOutput , error ) {
75112 if h .customizeSearchService == nil {
@@ -80,19 +117,10 @@ func (h *CustomizeHandler) Search(ctx context.Context, input *SearchCustomizeInp
80117 return nil , huma .Error400BadRequest ((& common.QueryParameterRequiredError {}).Error ())
81118 }
82119
83- results := h .customizeSearchService .Search (input .Body .Query )
84-
85120 ps , _ := humamw .PermissionsFromContext (ctx )
86- if ! ps .IsGlobalAdmin () {
87- filtered := []category.Category {}
88- for _ , cat := range results .Results {
89- if cat .ID != "registries" && cat .ID != "variables" {
90- filtered = append (filtered , cat )
91- }
92- }
93- results .Results = filtered
94- results .Count = len (filtered )
95- }
121+ results := h .customizeSearchService .Search (input .Body .Query )
122+ results .Results = filterCustomizeCategoriesInternal (ps , results .Results )
123+ results .Count = len (results .Results )
96124
97125 return & SearchCustomizeOutput {
98126 Body : results ,
@@ -105,18 +133,8 @@ func (h *CustomizeHandler) GetCategories(ctx context.Context, input *GetCustomiz
105133 return nil , huma .Error500InternalServerError ("service not available" )
106134 }
107135
108- categories := h .customizeSearchService .GetCustomizeCategories ()
109-
110136 ps , _ := humamw .PermissionsFromContext (ctx )
111- if ! ps .IsGlobalAdmin () {
112- filtered := []category.Category {}
113- for _ , cat := range categories {
114- if cat .ID != "registries" && cat .ID != "variables" {
115- filtered = append (filtered , cat )
116- }
117- }
118- categories = filtered
119- }
137+ categories := filterCustomizeCategoriesInternal (ps , h .customizeSearchService .GetCustomizeCategories ())
120138
121139 return & GetCustomizeCategoriesOutput {
122140 Body : categories ,
0 commit comments