@@ -83,8 +83,8 @@ func New(options *Options) (http.Handler, func()) {
83
83
// TODO: @emyrk we should just move this into 'ExtractAPIKey'.
84
84
authRolesMiddleware := httpmw .ExtractUserRoles (options .Database )
85
85
86
- authorize := func (f http.HandlerFunc , actions ... rbac.Action ) http.HandlerFunc {
87
- return httpmw .Authorize (api .Logger , api .Authorizer , actions ... )(f ).ServeHTTP
86
+ authorize := func (f http.HandlerFunc , actions rbac.Action ) http.HandlerFunc {
87
+ return httpmw .Authorize (api .Logger , api .Authorizer , actions )(f ).ServeHTTP
88
88
}
89
89
90
90
r := chi .NewRouter ()
@@ -127,25 +127,20 @@ func New(options *Options) (http.Handler, func()) {
127
127
r .Route ("/files" , func (r chi.Router ) {
128
128
r .Use (
129
129
apiKeyMiddleware ,
130
- authRolesMiddleware ,
131
130
// This number is arbitrary, but reading/writing
132
131
// file content is expensive so it should be small.
133
132
httpmw .RateLimitPerMinute (12 ),
134
- // TODO: @emyrk (rbac) Currently files are owned by the site?
135
- // Should files be org scoped? User scoped?
136
- httpmw .WithRBACObject (rbac .ResourceFile ),
137
133
)
138
134
r .Get ("/{hash}" , api .fileByHash )
139
135
r .Post ("/" , api .postFile )
140
136
})
141
137
r .Route ("/organizations/{organization}" , func (r chi.Router ) {
142
138
r .Use (
143
139
apiKeyMiddleware ,
144
- authRolesMiddleware ,
145
140
httpmw .ExtractOrganizationParam (options .Database ),
141
+ authRolesMiddleware ,
146
142
)
147
- r .With (httpmw .WithRBACObject (rbac .ResourceOrganization )).
148
- Get ("/" , authorize (api .organization , rbac .ActionRead ))
143
+ r .Get ("/" , api .organization )
149
144
r .Get ("/provisionerdaemons" , api .provisionerDaemonsByOrganization )
150
145
r .Post ("/templateversions" , api .postTemplateVersionsByOrganization )
151
146
r .Route ("/templates" , func (r chi.Router ) {
@@ -154,17 +149,12 @@ func New(options *Options) (http.Handler, func()) {
154
149
r .Get ("/{templatename}" , api .templateByOrganizationAndName )
155
150
})
156
151
r .Route ("/workspaces" , func (r chi.Router ) {
157
- r .Use (httpmw .WithRBACObject (rbac .ResourceWorkspace ))
158
- // Posting a workspace is inherently owned by the api key creating it.
159
- r .With (httpmw .WithAPIKeyAsOwner ()).
160
- Post ("/" , authorize (api .postWorkspacesByOrganization , rbac .ActionCreate ))
161
- r .Get ("/" , authorize (api .workspacesByOrganization , rbac .ActionRead ))
162
152
r .Post ("/" , api .postWorkspacesByOrganization )
153
+ r .Get ("/" , api .workspacesByOrganization )
163
154
r .Route ("/{user}" , func (r chi.Router ) {
164
155
r .Use (httpmw .ExtractUserParam (options .Database ))
165
- // TODO: @emyrk add the resource id to this authorize.
166
- r .Get ("/{workspace}" , authorize (api .workspaceByOwnerAndName , rbac .ActionRead ))
167
- r .Get ("/" , authorize (api .workspacesByOwner , rbac .ActionRead ))
156
+ r .Get ("/{workspace}" , api .workspaceByOwnerAndName )
157
+ r .Get ("/" , api .workspacesByOwner )
168
158
})
169
159
})
170
160
r .Route ("/members" , func (r chi.Router ) {
@@ -238,58 +228,38 @@ func New(options *Options) (http.Handler, func()) {
238
228
apiKeyMiddleware ,
239
229
authRolesMiddleware ,
240
230
)
241
- r .Group (func (r chi.Router ) {
242
- // Site wide, all users
243
- r .Use (httpmw .WithRBACObject (rbac .ResourceUser ))
244
- r .Post ("/" , authorize (api .postUser , rbac .ActionCreate ))
245
- r .Get ("/" , authorize (api .users , rbac .ActionRead ))
246
- })
231
+ r .Post ("/" , api .postUser )
232
+ r .Get ("/" , api .users )
247
233
// These routes query information about site wide roles.
248
234
r .Route ("/roles" , func (r chi.Router ) {
249
235
r .Use (httpmw .WithRBACObject (rbac .ResourceUserRole ))
250
236
r .Get ("/" , authorize (api .assignableSiteRoles , rbac .ActionRead ))
251
237
})
252
238
r .Route ("/{user}" , func (r chi.Router ) {
253
239
r .Use (httpmw .ExtractUserParam (options .Database ))
254
- r .Group (func (r chi.Router ) {
255
- r .Use (httpmw .WithRBACObject (rbac .ResourceUser ))
256
- r .Get ("/" , authorize (api .userByName , rbac .ActionRead ))
257
- r .Put ("/profile" , authorize (api .putUserProfile , rbac .ActionUpdate ))
258
- // suspension is deleting for a user
259
- r .Put ("/suspend" , authorize (api .putUserSuspend , rbac .ActionDelete ))
260
- r .Route ("/password" , func (r chi.Router ) {
261
- r .Put ("/" , authorize (api .putUserPassword , rbac .ActionUpdate ))
262
- })
263
- // This route technically also fetches the organization member struct, but only
264
- // returns the roles.
265
- r .Get ("/roles" , authorize (api .userRoles , rbac .ActionRead ))
266
-
267
- // This has 2 authorize calls. The second is explicitly called
268
- // in the handler.
269
- r .Put ("/roles" , authorize (api .putUserRoles , rbac .ActionUpdate ))
270
-
271
- // For now, just use the "user" role for their ssh keys.
272
- // We can always split this out to it's own resource if we need to.
273
- r .Get ("/gitsshkey" , authorize (api .gitSSHKey , rbac .ActionRead ))
274
- r .Put ("/gitsshkey" , authorize (api .regenerateGitSSHKey , rbac .ActionUpdate ))
275
-
276
- r .Post ("/authorization" , authorize (api .checkPermissions , rbac .ActionRead ))
240
+ r .Get ("/" , api .userByName )
241
+ r .Put ("/profile" , api .putUserProfile )
242
+ r .Put ("/suspend" , api .putUserSuspend )
243
+ r .Route ("/password" , func (r chi.Router ) {
244
+ r .Put ("/" , authorize (api .putUserPassword , rbac .ActionUpdate ))
277
245
})
246
+ r .Get ("/organizations" , api .organizationsByUser )
247
+ r .Post ("/organizations" , api .postOrganizationsByUser )
248
+ // These roles apply to the site wide permissions.
249
+ r .Put ("/roles" , api .putUserRoles )
250
+ r .Get ("/roles" , api .userRoles )
278
251
279
- r .With (httpmw .WithRBACObject (rbac .ResourceAPIKey )).Post ("/keys" , authorize (api .postAPIKey , rbac .ActionCreate ))
280
- r .Get ("/workspaces" , api .workspacesByUser )
252
+ r .Post ("/authorization" , api .checkPermissions )
281
253
254
+ r .Post ("/keys" , api .postAPIKey )
282
255
r .Route ("/organizations" , func (r chi.Router ) {
283
- // TODO: @emyrk This creates an organization, so why is it nested under {user}?
284
- // Shouldn't this be outside the {user} param subpath? Maybe in the organizations/
285
- // path?
286
256
r .Post ("/" , api .postOrganizationsByUser )
287
-
288
257
r .Get ("/" , api .organizationsByUser )
289
-
290
- // TODO: @emyrk why is this nested under {user} when the user param is not used?
291
258
r .Get ("/{organizationname}" , api .organizationByUserAndName )
292
259
})
260
+ r .Get ("/gitsshkey" , api .gitSSHKey )
261
+ r .Put ("/gitsshkey" , api .regenerateGitSSHKey )
262
+ r .Get ("/workspaces" , api .workspacesByUser )
293
263
})
294
264
})
295
265
})
0 commit comments