8
8
"io"
9
9
"net/http"
10
10
"strconv"
11
+ "strings"
11
12
"time"
12
13
13
14
"github.com/julienschmidt/httprouter"
@@ -89,11 +90,7 @@ func (s *ShibuyaAPI) handleErrors(w http.ResponseWriter, err error) {
89
90
}
90
91
91
92
func (s * ShibuyaAPI ) projectsGetHandler (w http.ResponseWriter , r * http.Request , params httprouter.Params ) {
92
- account := model .GetAccountBySession (r )
93
- if account == nil {
94
- s .makeFailMessage (w , "Need to login" , http .StatusForbidden )
95
- return
96
- }
93
+ account := r .Context ().Value (accountKey ).(* model.Account )
97
94
qs := r .URL .Query ()
98
95
var includeCollections , includePlans bool
99
96
var err error
@@ -145,11 +142,7 @@ func (s *ShibuyaAPI) projectUpdateHandler(w http.ResponseWriter, _ *http.Request
145
142
}
146
143
147
144
func (s * ShibuyaAPI ) projectCreateHandler (w http.ResponseWriter , r * http.Request , params httprouter.Params ) {
148
- account := model .GetAccountBySession (r )
149
- if account == nil {
150
- s .handleErrors (w , makeLoginError ())
151
- return
152
- }
145
+ account := r .Context ().Value (accountKey ).(* model.Account )
153
146
r .ParseForm ()
154
147
name := r .Form .Get ("name" )
155
148
if name == "" {
@@ -191,18 +184,14 @@ func (s *ShibuyaAPI) projectCreateHandler(w http.ResponseWriter, r *http.Request
191
184
}
192
185
193
186
func (s * ShibuyaAPI ) projectDeleteHandler (w http.ResponseWriter , r * http.Request , params httprouter.Params ) {
194
- account := model .GetAccountBySession (r )
195
- if account == nil {
196
- s .handleErrors (w , makeLoginError ())
197
- return
198
- }
187
+ account := r .Context ().Value (accountKey ).(* model.Account )
199
188
project , err := getProject (params .ByName ("project_id" ))
200
189
if err != nil {
201
190
s .handleErrors (w , err )
202
191
return
203
192
}
204
- if _ , ok := account . MLMap [ project . Owner ] ; ! ok {
205
- s .handleErrors (w , noPermissionErr )
193
+ if r := hasProjectOwnership ( project , account ) ; ! r {
194
+ s .handleErrors (w , makeProjectOwnershipError () )
206
195
return
207
196
}
208
197
collectionIDs , err := project .GetCollections ()
@@ -260,20 +249,16 @@ func (s *ShibuyaAPI) collectionAdminGetHandler(w http.ResponseWriter, r *http.Re
260
249
}
261
250
262
251
func (s * ShibuyaAPI ) planCreateHandler (w http.ResponseWriter , r * http.Request , _ httprouter.Params ) {
263
- account := model .GetAccountBySession (r )
264
- if account == nil {
265
- s .handleErrors (w , makeLoginError ())
266
- return
267
- }
252
+ account := r .Context ().Value (accountKey ).(* model.Account )
268
253
r .ParseForm ()
269
254
projectID := r .Form .Get ("project_id" )
270
255
project , err := getProject (projectID )
271
256
if err != nil {
272
257
s .handleErrors (w , err )
273
258
return
274
259
}
275
- if _ , ok := account . MLMap [ project . Owner ] ; ! ok {
276
- s .handleErrors (w , makeNoPermissionErr ( "You don't own the project" ))
260
+ if r := hasProjectOwnership ( project , account ) ; ! r {
261
+ s .handleErrors (w , makeProjectOwnershipError ( ))
277
262
return
278
263
}
279
264
name := r .Form .Get ("name" )
@@ -294,11 +279,7 @@ func (s *ShibuyaAPI) planCreateHandler(w http.ResponseWriter, r *http.Request, _
294
279
}
295
280
296
281
func (s * ShibuyaAPI ) planDeleteHandler (w http.ResponseWriter , r * http.Request , params httprouter.Params ) {
297
- account := model .GetAccountBySession (r )
298
- if account == nil {
299
- s .handleErrors (w , makeLoginError ())
300
- return
301
- }
282
+ account := r .Context ().Value (accountKey ).(* model.Account )
302
283
plan , err := getPlan (params .ByName ("plan_id" ))
303
284
if err != nil {
304
285
s .handleErrors (w , err )
@@ -309,8 +290,8 @@ func (s *ShibuyaAPI) planDeleteHandler(w http.ResponseWriter, r *http.Request, p
309
290
s .handleErrors (w , err )
310
291
return
311
292
}
312
- if _ , ok := account . MLMap [ project . Owner ] ; ! ok {
313
- s .handleErrors (w , makeLoginError ())
293
+ if r := hasProjectOwnership ( project , account ) ; ! r {
294
+ s .handleErrors (w , makeProjectOwnershipError ())
314
295
return
315
296
}
316
297
using , err := plan .IsBeingUsed ()
@@ -355,7 +336,7 @@ func (s *ShibuyaAPI) collectionFilesGetHandler(w http.ResponseWriter, _ *http.Re
355
336
}
356
337
357
338
func (s * ShibuyaAPI ) collectionFilesUploadHandler (w http.ResponseWriter , r * http.Request , params httprouter.Params ) {
358
- collection , err := checkCollectionOwnership (r , params )
339
+ collection , err := hasCollectionOwnership (r , params )
359
340
if err != nil {
360
341
s .handleErrors (w , err )
361
342
return
@@ -375,7 +356,7 @@ func (s *ShibuyaAPI) collectionFilesUploadHandler(w http.ResponseWriter, r *http
375
356
}
376
357
377
358
func (s * ShibuyaAPI ) collectionFilesDeleteHandler (w http.ResponseWriter , r * http.Request , params httprouter.Params ) {
378
- collection , err := checkCollectionOwnership (r , params )
359
+ collection , err := hasCollectionOwnership (r , params )
379
360
if err != nil {
380
361
s .handleErrors (w , err )
381
362
return
@@ -415,11 +396,7 @@ func (s *ShibuyaAPI) planFilesDeleteHandler(w http.ResponseWriter, r *http.Reque
415
396
}
416
397
417
398
func (s * ShibuyaAPI ) collectionCreateHandler (w http.ResponseWriter , r * http.Request , params httprouter.Params ) {
418
- account := model .GetAccountBySession (r )
419
- if account == nil {
420
- s .handleErrors (w , makeLoginError ())
421
- return
422
- }
399
+ account := r .Context ().Value (accountKey ).(* model.Account )
423
400
r .ParseForm ()
424
401
collectionName := r .Form .Get ("name" )
425
402
if collectionName == "" {
@@ -432,8 +409,8 @@ func (s *ShibuyaAPI) collectionCreateHandler(w http.ResponseWriter, r *http.Requ
432
409
s .handleErrors (w , err )
433
410
return
434
411
}
435
- if _ , ok := account . MLMap [ project . Owner ] ; ! ok {
436
- s .handleErrors (w , makeNoPermissionErr ( "You don't have the permission" ))
412
+ if r := hasProjectOwnership ( project , account ) ; ! r {
413
+ s .handleErrors (w , makeProjectOwnershipError ( ))
437
414
return
438
415
}
439
416
collectionID , err := model .CreateCollection (collectionName , project .ID )
@@ -450,7 +427,7 @@ func (s *ShibuyaAPI) collectionCreateHandler(w http.ResponseWriter, r *http.Requ
450
427
}
451
428
452
429
func (s * ShibuyaAPI ) collectionDeleteHandler (w http.ResponseWriter , r * http.Request , params httprouter.Params ) {
453
- collection , err := checkCollectionOwnership (r , params )
430
+ collection , err := hasCollectionOwnership (r , params )
454
431
if err != nil {
455
432
s .handleErrors (w , err )
456
433
return
@@ -480,7 +457,7 @@ func (s *ShibuyaAPI) collectionDeleteHandler(w http.ResponseWriter, r *http.Requ
480
457
}
481
458
482
459
func (s * ShibuyaAPI ) collectionGetHandler (w http.ResponseWriter , r * http.Request , params httprouter.Params ) {
483
- collection , err := checkCollectionOwnership (r , params )
460
+ collection , err := hasCollectionOwnership (r , params )
484
461
if err != nil {
485
462
s .handleErrors (w , err )
486
463
return
@@ -519,7 +496,7 @@ func (s *ShibuyaAPI) collectionUpdateHandler(w http.ResponseWriter, _ *http.Requ
519
496
}
520
497
521
498
func (s * ShibuyaAPI ) collectionUploadHandler (w http.ResponseWriter , r * http.Request , params httprouter.Params ) {
522
- collection , err := checkCollectionOwnership (r , params )
499
+ collection , err := hasCollectionOwnership (r , params )
523
500
if err != nil {
524
501
s .handleErrors (w , err )
525
502
return
@@ -613,7 +590,7 @@ func (s *ShibuyaAPI) collectionUploadHandler(w http.ResponseWriter, r *http.Requ
613
590
}
614
591
615
592
func (s * ShibuyaAPI ) collectionEnginesDetailHandler (w http.ResponseWriter , r * http.Request , params httprouter.Params ) {
616
- collection , err := checkCollectionOwnership (r , params )
593
+ collection , err := hasCollectionOwnership (r , params )
617
594
if err != nil {
618
595
s .handleErrors (w , err )
619
596
return
@@ -627,7 +604,7 @@ func (s *ShibuyaAPI) collectionEnginesDetailHandler(w http.ResponseWriter, r *ht
627
604
}
628
605
629
606
func (s * ShibuyaAPI ) collectionDeploymentHandler (w http.ResponseWriter , r * http.Request , params httprouter.Params ) {
630
- collection , err := checkCollectionOwnership (r , params )
607
+ collection , err := hasCollectionOwnership (r , params )
631
608
if err != nil {
632
609
s .handleErrors (w , err )
633
610
return
@@ -644,7 +621,7 @@ func (s *ShibuyaAPI) collectionDeploymentHandler(w http.ResponseWriter, r *http.
644
621
}
645
622
646
623
func (s * ShibuyaAPI ) collectionTriggerHandler (w http.ResponseWriter , r * http.Request , params httprouter.Params ) {
647
- collection , err := checkCollectionOwnership (r , params )
624
+ collection , err := hasCollectionOwnership (r , params )
648
625
if err != nil {
649
626
s .handleErrors (w , err )
650
627
return
@@ -656,7 +633,7 @@ func (s *ShibuyaAPI) collectionTriggerHandler(w http.ResponseWriter, r *http.Req
656
633
}
657
634
658
635
func (s * ShibuyaAPI ) collectionTermHandler (w http.ResponseWriter , r * http.Request , params httprouter.Params ) {
659
- collection , err := checkCollectionOwnership (r , params )
636
+ collection , err := hasCollectionOwnership (r , params )
660
637
if err != nil {
661
638
s .handleErrors (w , err )
662
639
return
@@ -668,7 +645,7 @@ func (s *ShibuyaAPI) collectionTermHandler(w http.ResponseWriter, r *http.Reques
668
645
}
669
646
670
647
func (s * ShibuyaAPI ) collectionStatusHandler (w http.ResponseWriter , r * http.Request , params httprouter.Params ) {
671
- collection , err := checkCollectionOwnership (r , params )
648
+ collection , err := hasCollectionOwnership (r , params )
672
649
if err != nil {
673
650
s .handleErrors (w , err )
674
651
return
@@ -681,7 +658,7 @@ func (s *ShibuyaAPI) collectionStatusHandler(w http.ResponseWriter, r *http.Requ
681
658
}
682
659
683
660
func (s * ShibuyaAPI ) collectionPurgeHandler (w http.ResponseWriter , r * http.Request , params httprouter.Params ) {
684
- collection , err := checkCollectionOwnership (r , params )
661
+ collection , err := hasCollectionOwnership (r , params )
685
662
if err != nil {
686
663
s .handleErrors (w , err )
687
664
return
@@ -714,7 +691,7 @@ func (s *ShibuyaAPI) planLogHandler(w http.ResponseWriter, r *http.Request, para
714
691
}
715
692
716
693
func (s * ShibuyaAPI ) streamCollectionMetrics (w http.ResponseWriter , r * http.Request , params httprouter.Params ) {
717
- collection , err := checkCollectionOwnership (r , params )
694
+ collection , err := hasCollectionOwnership (r , params )
718
695
if err != nil {
719
696
s .handleErrors (w , err )
720
697
return
@@ -789,7 +766,7 @@ type Route struct {
789
766
type Routes []* Route
790
767
791
768
func (s * ShibuyaAPI ) InitRoutes () Routes {
792
- return Routes {
769
+ routes := Routes {
793
770
& Route {"get_projects" , "GET" , "/api/projects" , s .projectsGetHandler },
794
771
& Route {"create_project" , "POST" , "/api/projects" , s .projectCreateHandler },
795
772
& Route {"delete_project" , "DELETE" , "/api/projects/:project_id" , s .projectDeleteHandler },
@@ -833,4 +810,12 @@ func (s *ShibuyaAPI) InitRoutes() Routes {
833
810
834
811
& Route {"admin_collections" , "GET" , "/api/admin/collections" , s .collectionAdminGetHandler },
835
812
}
813
+ for _ , r := range routes {
814
+ // TODO! We don't require auth for usage endpoint for now.
815
+ if strings .Contains (r .Path , "usage" ) {
816
+ continue
817
+ }
818
+ r .HandlerFunc = s .authRequired (r .HandlerFunc )
819
+ }
820
+ return routes
836
821
}
0 commit comments