@@ -27,12 +27,100 @@ import (
2727 "github.com/ovh/cds/sdk/log"
2828)
2929
30+ func (api * API ) getProjectsHandler_FilterByRepo (ctx context.Context , w http.ResponseWriter , r * http.Request ) error {
31+ withPermissions := r .FormValue ("permission" )
32+ filterByRepo := r .FormValue ("repo" )
33+
34+ var projects sdk.Projects
35+ var err error
36+ var filterByRepoFunc = func (db gorp.SqlExecutor , store cache.Store , p * sdk.Project ) error {
37+ //Filter the applications by repo
38+ apps := []sdk.Application {}
39+ for i := range p .Applications {
40+ if p .Applications [i ].RepositoryFullname == filterByRepo {
41+ apps = append (apps , p .Applications [i ])
42+ }
43+ }
44+ p .Applications = apps
45+ ws := []sdk.Workflow {}
46+ //Filter the workflow by applications
47+ for i := range p .Workflows {
48+ w , err := workflow .LoadByID (ctx , db , store , p , p .Workflows [i ].ID , workflow.LoadOptions {})
49+ if err != nil {
50+ return err
51+ }
52+
53+ //Checks the workflow use one of the applications
54+ wapps:
55+ for _ , a := range w .Applications {
56+ for _ , b := range apps {
57+ if a .Name == b .Name {
58+ ws = append (ws , p .Workflows [i ])
59+ break wapps
60+ }
61+ }
62+ }
63+ }
64+ p .Workflows = ws
65+ return nil
66+ }
67+
68+ opts := []project.LoadOptionFunc {
69+ project .LoadOptions .WithPermission ,
70+ }
71+ opts = append (opts , filterByRepoFunc )
72+
73+ if isMaintainer (ctx ) || isAdmin (ctx ) {
74+ projects , err = project .LoadAllByRepo (ctx , api .mustDB (), api .Cache , filterByRepo , opts ... )
75+ if err != nil {
76+ return err
77+ }
78+ } else {
79+ projects , err = project .LoadAllByRepoAndGroupIDs (ctx , api .mustDB (), api .Cache , getAPIConsumer (ctx ).GetGroupIDs (), filterByRepo , opts ... )
80+ if err != nil {
81+ return err
82+ }
83+ }
84+
85+ pKeys := projects .Keys ()
86+ perms , err := permission .LoadProjectMaxLevelPermission (ctx , api .mustDB (), pKeys , getAPIConsumer (ctx ).GetGroupIDs ())
87+ if err != nil {
88+ return err
89+ }
90+ for i := range projects {
91+ if isAdmin (ctx ) {
92+ projects [i ].Permissions = sdk.Permissions {Readable : true , Writable : true , Executable : true }
93+ continue
94+ }
95+ projects [i ].Permissions = perms [projects [i ].Key ]
96+ if isMaintainer (ctx ) {
97+ projects [i ].Permissions .Readable = true
98+ }
99+ }
100+
101+ if strings .ToUpper (withPermissions ) == "W" {
102+ res := make ([]sdk.Project , 0 , len (projects ))
103+ for _ , p := range projects {
104+ if p .Permissions .Writable {
105+ res = append (res , p )
106+ }
107+ }
108+ projects = res
109+ }
110+
111+ return service .WriteJSON (w , projects , http .StatusOK )
112+ }
113+
30114func (api * API ) getProjectsHandler () service.Handler {
31115 return func (ctx context.Context , w http.ResponseWriter , r * http.Request ) error {
116+ withPermissions := r .FormValue ("permission" )
117+ filterByRepo := r .FormValue ("repo" )
118+ if filterByRepo != "" {
119+ return api .getProjectsHandler_FilterByRepo (ctx , w , r )
120+ }
121+
32122 withApplications := FormBool (r , "application" )
33123 withWorkflows := FormBool (r , "workflow" )
34- filterByRepo := r .FormValue ("repo" )
35- withPermissions := r .FormValue ("permission" )
36124 withIcon := FormBool (r , "withIcon" )
37125
38126 requestedUserName := r .Header .Get ("X-Cds-Username" )
@@ -64,7 +152,6 @@ func (api *API) getProjectsHandler() service.Handler {
64152
65153 var projects sdk.Projects
66154 var err error
67-
68155 switch {
69156 case isMaintainer (ctx ) && requestedUser == nil :
70157 projects , err = project .LoadAll (ctx , api .mustDB (), api .Cache , opts ... )
@@ -83,71 +170,32 @@ func (api *API) getProjectsHandler() service.Handler {
83170 return err
84171 }
85172
173+ var groupIDs []int64
174+ var admin bool
175+ var maintainer bool
176+ if requestedUser == nil {
177+ groupIDs = getAPIConsumer (ctx ).GetGroupIDs ()
178+ admin = isAdmin (ctx )
179+ maintainer = isMaintainer (ctx )
180+ } else {
181+ groupIDs = requestedUser .GetGroupIDs ()
182+ admin = requestedUser .Ring == sdk .UserRingAdmin
183+ maintainer = requestedUser .Ring == sdk .UserRingMaintainer
184+ }
185+
86186 pKeys := projects .Keys ()
87- perms , err := permission .LoadProjectMaxLevelPermission (ctx , api .mustDB (), pKeys , getAPIConsumer ( ctx ). GetGroupIDs () )
187+ perms , err := permission .LoadProjectMaxLevelPermission (ctx , api .mustDB (), pKeys , groupIDs )
88188 if err != nil {
89189 return err
90190 }
91191 for i := range projects {
92- projects [i ].Permissions = perms [projects [i ].Key ]
93- }
94-
95- if filterByRepo == "" {
96- if strings .ToUpper (withPermissions ) == "W" {
97- res := make ([]sdk.Project , 0 , len (projects ))
98- for _ , p := range projects {
99- if p .Permissions .Writable {
100- res = append (res , p )
101- }
102- }
103- projects = res
192+ if admin {
193+ projects [i ].Permissions = sdk.Permissions {Readable : true , Writable : true , Executable : true }
194+ continue
104195 }
105-
106- return service .WriteJSON (w , projects , http .StatusOK )
107- }
108-
109- var filterByRepoFunc = func (db gorp.SqlExecutor , store cache.Store , p * sdk.Project ) error {
110- //Filter the applications by repo
111- apps := []sdk.Application {}
112- for i := range p .Applications {
113- if p .Applications [i ].RepositoryFullname == filterByRepo {
114- apps = append (apps , p .Applications [i ])
115- }
116- }
117- p .Applications = apps
118- ws := []sdk.Workflow {}
119- //Filter the workflow by applications
120- for i := range p .Workflows {
121- w , err := workflow .LoadByID (ctx , db , store , p , p .Workflows [i ].ID , workflow.LoadOptions {})
122- if err != nil {
123- return err
124- }
125-
126- //Checks the workflow use one of the applications
127- wapps:
128- for _ , a := range w .Applications {
129- for _ , b := range apps {
130- if a .Name == b .Name {
131- ws = append (ws , p .Workflows [i ])
132- break wapps
133- }
134- }
135- }
136- }
137- p .Workflows = ws
138- return nil
139- }
140- opts = append (opts , filterByRepoFunc )
141-
142- if isMaintainer (ctx ) || isAdmin (ctx ) {
143- projects , err = project .LoadAllByRepo (ctx , api .mustDB (), api .Cache , filterByRepo , opts ... )
144- if err != nil {
145- return err
146- }
147- } else {
148- projects , err = project .LoadAllByRepoAndGroupIDs (ctx , api .mustDB (), api .Cache , getAPIConsumer (ctx ).GetGroupIDs (), filterByRepo , opts ... )
149- if err != nil {
150- return err
196+ projects [i ].Permissions = perms [projects [i ].Key ]
197+ if maintainer {
198+ projects [i ].Permissions .Readable = true
151199 }
152200 }
153201
0 commit comments