github.com/cozy/cozy-stack@v0.0.0-20240603063001-31110fa4cae1/web/sharings/revoke.go (about) 1 package sharings 2 3 import ( 4 "errors" 5 "net/http" 6 "strconv" 7 8 "github.com/cozy/cozy-stack/model/sharing" 9 "github.com/cozy/cozy-stack/pkg/jsonapi" 10 "github.com/cozy/cozy-stack/web/middlewares" 11 "github.com/labstack/echo/v4" 12 ) 13 14 // RevokeSharing is used to revoke a sharing by the sharer, for all recipients 15 func RevokeSharing(c echo.Context) error { 16 inst := middlewares.GetInstance(c) 17 sharingID := c.Param("sharing-id") 18 s, err := sharing.FindSharing(inst, sharingID) 19 if err != nil { 20 return wrapErrors(err) 21 } 22 _, err = checkCreatePermissions(c, s) 23 if err != nil { 24 return echo.NewHTTPError(http.StatusForbidden) 25 } 26 if err = s.Revoke(inst); err != nil { 27 return wrapErrors(err) 28 } 29 return c.NoContent(http.StatusNoContent) 30 } 31 32 // RevokeRecipient is used by the owner to revoke a recipient 33 func RevokeRecipient(c echo.Context) error { 34 inst := middlewares.GetInstance(c) 35 sharingID := c.Param("sharing-id") 36 s, err := sharing.FindSharing(inst, sharingID) 37 if err != nil { 38 return wrapErrors(err) 39 } 40 _, err = checkCreatePermissions(c, s) 41 if err != nil { 42 return echo.NewHTTPError(http.StatusForbidden) 43 } 44 index, err := strconv.Atoi(c.Param("index")) 45 if err != nil { 46 return jsonapi.InvalidParameter("index", err) 47 } 48 if index == 0 || index >= len(s.Members) { 49 return jsonapi.InvalidParameter("index", errors.New("Invalid index")) 50 } 51 if err = s.RevokeRecipient(inst, index); err != nil { 52 return wrapErrors(err) 53 } 54 go s.NotifyRecipients(inst, nil) 55 return c.NoContent(http.StatusNoContent) 56 } 57 58 // RevokeGroup is used by the owner to revoke a group 59 func RevokeGroup(c echo.Context) error { 60 inst := middlewares.GetInstance(c) 61 sharingID := c.Param("sharing-id") 62 s, err := sharing.FindSharing(inst, sharingID) 63 if err != nil { 64 return wrapErrors(err) 65 } 66 _, err = checkCreatePermissions(c, s) 67 if err != nil { 68 return echo.NewHTTPError(http.StatusForbidden) 69 } 70 index, err := strconv.Atoi(c.Param("index")) 71 if err != nil { 72 return jsonapi.InvalidParameter("index", err) 73 } 74 if index >= len(s.Groups) { 75 return jsonapi.InvalidParameter("index", errors.New("Invalid index")) 76 } 77 if err = s.RevokeGroup(inst, index); err != nil { 78 return wrapErrors(err) 79 } 80 go s.NotifyRecipients(inst, nil) 81 return c.NoContent(http.StatusNoContent) 82 } 83 84 // RevocationRecipientNotif is used to inform a recipient that the sharing is revoked 85 func RevocationRecipientNotif(c echo.Context) error { 86 inst := middlewares.GetInstance(c) 87 sharingID := c.Param("sharing-id") 88 s, err := sharing.FindSharing(inst, sharingID) 89 if err != nil { 90 return wrapErrors(err) 91 } 92 if err = s.RevokeByNotification(inst); err != nil { 93 return wrapErrors(err) 94 } 95 return c.NoContent(http.StatusNoContent) 96 } 97 98 // RevocationOwnerNotif is used to inform the owner that a recipient has revoked 99 // himself/herself from the sharing 100 func RevocationOwnerNotif(c echo.Context) error { 101 inst := middlewares.GetInstance(c) 102 sharingID := c.Param("sharing-id") 103 s, err := sharing.FindSharing(inst, sharingID) 104 if err != nil { 105 return wrapErrors(err) 106 } 107 member, err := requestMember(c, s) 108 if err != nil { 109 return wrapErrors(err) 110 } 111 if err = s.RevokeRecipientByNotification(inst, member); err != nil { 112 return wrapErrors(err) 113 } 114 go s.NotifyRecipients(inst, nil) 115 return c.NoContent(http.StatusNoContent) 116 } 117 118 // RevokeRecipientBySelf is used by a recipient to revoke himself/herself 119 // from the sharing 120 func RevokeRecipientBySelf(c echo.Context) error { 121 inst := middlewares.GetInstance(c) 122 sharingID := c.Param("sharing-id") 123 s, err := sharing.FindSharing(inst, sharingID) 124 if err != nil { 125 return wrapErrors(err) 126 } 127 _, err = checkCreatePermissions(c, s) 128 if err != nil { 129 return echo.NewHTTPError(http.StatusForbidden) 130 } 131 if err = s.RevokeRecipientBySelf(inst, sharing.SharingDirNotTrashed); err != nil { 132 return wrapErrors(err) 133 } 134 return c.NoContent(http.StatusNoContent) 135 }