@@ -315,13 +315,13 @@ func Startup(ctx context.Context, js jetstream.JetStream, logger *zap.Logger, po
315315 }
316316 }
317317
318- // POST /entity/{name}/{id}/share/{userid}: share ownership of the resource with another user
318+ // PUT /entity/{name}/{id}/share/{userid}: share ownership of the resource with another user
319319 r .HandleFunc ("/entity/{name}/{id}/share/{userid}" , func (writer http.ResponseWriter , request * http.Request ) {
320320 if stop := handleCors (writer , request ); stop {
321321 return
322322 }
323323
324- if request .Method != "POST " {
324+ if request .Method != "PUT " {
325325 http .Error (writer , "Method Not Allowed" , http .StatusMethodNotAllowed )
326326 return
327327 }
@@ -355,6 +355,77 @@ func Startup(ctx context.Context, js jetstream.JetStream, logger *zap.Logger, po
355355 logger .Error ("Failed to share ownership" , zap .Error (err ))
356356 http .Error (writer , "Internal Server Error" , http .StatusInternalServerError )
357357 }
358+
359+ err = r .Update (ctx , logger )
360+ if err != nil {
361+ logger .Error ("Failed to update resource" , zap .Error (err ))
362+ http .Error (writer , "Internal Server Error" , http .StatusInternalServerError )
363+ }
364+
365+ logger .Info ("Shared ownership" , zap .String ("id" , id .String ()), zap .String ("newUser" , newUser ))
366+ http .Error (writer , "" , http .StatusOK )
367+ })
368+
369+ // PUT /entity/{name}/{id}/grant/{user}/{operation}
370+ r .HandleFunc ("/entity/{name}/{id}/grant/{type}/{user}/{operation}" , func (writer http.ResponseWriter , request * http.Request ) {
371+ if stop := handleCors (writer , request ); stop {
372+ return
373+ }
374+
375+ vars := mux .Vars (request )
376+ id := & glue.EntityId {
377+ Name : strings .TrimSpace (vars ["name" ]),
378+ Id : strings .TrimSpace (vars ["id" ]),
379+ }
380+ stateId := id .ToStateId ()
381+
382+ operation := auth .Owner
383+ switch strings .ToLower (vars ["operation" ]) {
384+ case "signal" :
385+ operation = auth .Signal
386+ break
387+ case "completion" :
388+ operation = auth .Completion
389+ break
390+ case "output" :
391+ operation = auth .Output
392+ case "call" :
393+ operation = auth .Call
394+ case "lock" :
395+ operation = auth .Lock
396+ case "sharePlus" :
397+ operation = auth .SharePlus
398+ case "shareMinus" :
399+ operation = auth .ShareMinus
400+ default :
401+ http .Error (writer , "" , http .StatusBadRequest )
402+ return
403+ }
404+
405+ r , err := rm .DiscoverResource (ctx , stateId , logger , true )
406+ if err != nil {
407+ logger .Error ("Failed to discover resource" , zap .Error (err ))
408+ http .Error (writer , "" , http .StatusNotFound )
409+ }
410+
411+ switch vars ["type" ] {
412+ case "user" :
413+ err = r .GrantUser (auth .UserId (vars ["user" ]), operation , ctx )
414+ case "role" :
415+ err = r .GrantRole (auth .Role (vars ["user" ]), operation , ctx )
416+ }
417+ if err != nil {
418+ logger .Error ("Failed to grant resource" , zap .Error (err ))
419+ http .Error (writer , "" , http .StatusForbidden )
420+ }
421+
422+ err = r .Update (ctx , logger )
423+ if err != nil {
424+ logger .Error ("Failed to update resource" , zap .Error (err ))
425+ http .Error (writer , "" , http .StatusInternalServerError )
426+ }
427+
428+ http .Error (writer , "" , http .StatusOK )
358429 })
359430
360431 // GET /entity/{name}/{id}
0 commit comments