diff --git a/REST-Tests/namespaces.http b/REST-Tests/namespaces.http index fb2b19cc..34243df2 100644 --- a/REST-Tests/namespaces.http +++ b/REST-Tests/namespaces.http @@ -17,7 +17,7 @@ Authorization: Bearer {{auth_token}} ### # Give a user access to that namespace -PUT http://localhost:8080/api/v1/namespaces/12/users +PUT http://localhost:8080/api/v1/namespaces/1/users Authorization: Bearer {{auth_token}} Content-Type: application/json @@ -25,6 +25,15 @@ Content-Type: application/json ### +# Update a users access to that namespace +POST http://localhost:8080/api/v1/namespaces/1/users/3 +Authorization: Bearer {{auth_token}} +Content-Type: application/json + +{"right": 2} + +### + # Delete a user from a namespace DELETE http://localhost:8080/api/v1/namespaces/1/users/2 Authorization: Bearer {{auth_token}} diff --git a/models/namespace_users_rights.go b/models/namespace_users_rights.go index e9f0df18..2c7685a2 100644 --- a/models/namespace_users_rights.go +++ b/models/namespace_users_rights.go @@ -13,3 +13,10 @@ func (nu *NamespaceUser) CanDelete(doer *User) bool { n, _ := GetNamespaceByID(nu.NamespaceID) return n.CanWrite(doer) } + +// CanUpdate checks if the user can update a user <-> namespace relation +func (nu *NamespaceUser) CanUpdate(doer *User) bool { + // Get the namespace and check if the user has write access on it + n, _ := GetNamespaceByID(nu.NamespaceID) + return n.CanWrite(doer) +} diff --git a/models/namespace_users_update.go b/models/namespace_users_update.go new file mode 100644 index 00000000..c5bf0f78 --- /dev/null +++ b/models/namespace_users_update.go @@ -0,0 +1,16 @@ +package models + +// Update updates a user <-> namespace relation +func (nu *NamespaceUser) Update() (err error) { + + // Check if the right is valid + if err := nu.Right.isValid(); err != nil { + return err + } + + _, err = x. + Where("namespace_id = ? AND user_id = ?", nu.NamespaceID, nu.UserID). + Cols("right"). + Update(nu) + return +} diff --git a/routes/routes.go b/routes/routes.go index acf0fd63..63648077 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -125,6 +125,7 @@ func RegisterRoutes(e *echo.Echo) { a.GET("/namespaces/:namespace/users", namespaceUserHandler.ReadAllWeb) a.PUT("/namespaces/:namespace/users", namespaceUserHandler.CreateWeb) a.DELETE("/namespaces/:namespace/users/:user", namespaceUserHandler.DeleteWeb) + a.POST("/namespaces/:namespace/users/:user", namespaceUserHandler.UpdateWeb) teamHandler := &crud.WebHandler{ CObject: &models.Team{},