@@ -21,6 +21,7 @@ import (
21
21
"encoding/json"
22
22
"errors"
23
23
"fmt"
24
+ "golang.org/x/exp/maps"
24
25
"io"
25
26
"net/http"
26
27
"strconv"
@@ -416,12 +417,19 @@ func (handler *PipelineConfigRestHandlerImpl) PatchCiPipelines(w http.ResponseWr
416
417
}
417
418
resourceName := handler .enforcerUtil .GetAppRBACName (app .AppName )
418
419
workflowResourceName := handler .enforcerUtil .GetRbacObjectNameByAppAndWorkflow (app .AppName , appWorkflowName )
419
- var ok bool
420
- ok = handler .enforcer .Enforce (token , casbin .ResourceApplications , casbin .ActionCreate , resourceName )
421
- if ! ok {
422
- ok = handler .enforcer .Enforce (token , casbin .ResourceJobs , casbin .ActionCreate , resourceName ) && handler .enforcer .Enforce (token , casbin .ResourceWorkflow , casbin .ActionCreate , workflowResourceName )
420
+
421
+ cdPipelines , err := handler .pipelineRepository .FindByCiPipelineId (patchRequest .CiPipeline .Id )
422
+ if err != nil && err != pg .ErrNoRows {
423
+ handler .Logger .Errorw ("error in finding ccd cdPipelines by ciPipelineId" , "ciPipelineId" , patchRequest .CiPipeline .Id , "err" , err )
424
+ common .WriteJsonResp (w , err , nil , http .StatusInternalServerError )
425
+ return
423
426
}
424
- if ! ok {
427
+
428
+ haveCiPatchAccess := handler .checkCiPatchAccess (token , resourceName , cdPipelines )
429
+ if ! haveCiPatchAccess {
430
+ haveCiPatchAccess = handler .enforcer .Enforce (token , casbin .ResourceJobs , casbin .ActionCreate , resourceName ) && handler .enforcer .Enforce (token , casbin .ResourceWorkflow , casbin .ActionCreate , workflowResourceName )
431
+ }
432
+ if ! haveCiPatchAccess {
425
433
common .WriteJsonResp (w , fmt .Errorf ("unauthorized user" ), "Unauthorized User" , http .StatusForbidden )
426
434
return
427
435
}
@@ -470,6 +478,33 @@ func (handler *PipelineConfigRestHandlerImpl) PatchCiPipelines(w http.ResponseWr
470
478
common .WriteJsonResp (w , err , createResp , http .StatusOK )
471
479
}
472
480
481
+ // checkCiPatchAccess assumes all the cdPipelines belong to same app
482
+ func (handler * PipelineConfigRestHandlerImpl ) checkCiPatchAccess (token string , resourceName string , cdPipelines []* pipelineConfig.Pipeline ) bool {
483
+
484
+ if len (cdPipelines ) == 0 {
485
+ // no cd pipelines are present , so user can edit if he has app admin access
486
+ return handler .enforcer .Enforce (token , casbin .ResourceApplications , casbin .ActionCreate , resourceName )
487
+ }
488
+
489
+ appId := 0
490
+ envIds := make ([]int , len (cdPipelines ))
491
+ for _ , cdPipeline := range cdPipelines {
492
+ envIds = append (envIds , cdPipeline .EnvironmentId )
493
+ appId = cdPipeline .AppId
494
+ }
495
+
496
+ rbacObjectsMap , _ := handler .enforcerUtil .GetRbacObjectsByEnvIdsAndAppId (envIds , appId )
497
+ envRbacResultMap := handler .enforcer .EnforceInBatch (token , casbin .ResourceEnvironment , casbin .ActionUpdate , maps .Values (rbacObjectsMap ))
498
+
499
+ for _ , hasAccess := range envRbacResultMap {
500
+ if hasAccess {
501
+ return true
502
+ }
503
+ }
504
+
505
+ return false
506
+ }
507
+
473
508
func (handler * PipelineConfigRestHandlerImpl ) GetCiPipeline (w http.ResponseWriter , r * http.Request ) {
474
509
vars := mux .Vars (r )
475
510
appId , err := strconv .Atoi (vars ["appId" ])
0 commit comments