From 067672dcb4a1c60b95e517bccf946589a08ec546 Mon Sep 17 00:00:00 2001 From: kolaente Date: Wed, 19 Sep 2018 08:16:04 +0200 Subject: [PATCH] Added an endpoint to update a team <-> namespace relation --- REST-Tests/namespaces.http | 30 ++++++++++++++++++++++++++++++ models/team_list_update.go | 2 +- models/team_namespace_rights.go | 6 ++++++ models/team_namespace_update.go | 16 ++++++++++++++++ routes/crud/read_all.go | 4 ++++ routes/routes.go | 1 + 6 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 models/team_namespace_update.go diff --git a/REST-Tests/namespaces.http b/REST-Tests/namespaces.http index 34243df2f..290cea520 100644 --- a/REST-Tests/namespaces.http +++ b/REST-Tests/namespaces.http @@ -38,4 +38,34 @@ Content-Type: application/json DELETE http://localhost:8080/api/v1/namespaces/1/users/2 Authorization: Bearer {{auth_token}} +### + +# Get all teams who have access to that namespace +GET http://localhost:8080/api/v1/namespaces/1/teams +Authorization: Bearer {{auth_token}} + +### + +# Give a team access to that namespace +PUT http://localhost:8080/api/v1/namespaces/1/teams +Authorization: Bearer {{auth_token}} +Content-Type: application/json + +{"team_id":3, "right": 0} + +### + +# Update a teams access to that namespace +POST http://localhost:8080/api/v1/namespaces/1/teams/1 +Authorization: Bearer {{auth_token}} +Content-Type: application/json + +{"right": 0} + +### + +# Delete a team from a namespace +DELETE http://localhost:8080/api/v1/namespaces/1/teams/2 +Authorization: Bearer {{auth_token}} + ### \ No newline at end of file diff --git a/models/team_list_update.go b/models/team_list_update.go index b3fa680fc..de2a09857 100644 --- a/models/team_list_update.go +++ b/models/team_list_update.go @@ -1,6 +1,6 @@ package models -// Update updates a user <-> namespace relation +// Update updates a team <-> list relation func (tl *TeamList) Update() (err error) { // Check if the right is valid diff --git a/models/team_namespace_rights.go b/models/team_namespace_rights.go index 9d832244b..4a4903e60 100644 --- a/models/team_namespace_rights.go +++ b/models/team_namespace_rights.go @@ -11,3 +11,9 @@ func (tn *TeamNamespace) CanDelete(user *User) bool { n, _ := GetNamespaceByID(tn.NamespaceID) return n.IsAdmin(user) } + +// CanUpdate checks if a user can update a team from a namespace. Only namespace admins can do that. +func (tn *TeamNamespace) CanUpdate(user *User) bool { + n, _ := GetNamespaceByID(tn.NamespaceID) + return n.IsAdmin(user) +} diff --git a/models/team_namespace_update.go b/models/team_namespace_update.go new file mode 100644 index 000000000..af122114a --- /dev/null +++ b/models/team_namespace_update.go @@ -0,0 +1,16 @@ +package models + +// Update updates a team <-> namespace relation +func (tl *TeamNamespace) Update() (err error) { + + // Check if the right is valid + if err := tl.Right.isValid(); err != nil { + return err + } + + _, err = x. + Where("namespace_id = ? AND team_id = ?", tl.TeamID, tl.TeamID). + Cols("right"). + Update(tl) + return +} diff --git a/routes/crud/read_all.go b/routes/crud/read_all.go index a1be9f78e..7afde804c 100644 --- a/routes/crud/read_all.go +++ b/routes/crud/read_all.go @@ -24,6 +24,10 @@ func (c *WebHandler) ReadAllWeb(ctx echo.Context) error { return echo.NewHTTPError(http.StatusForbidden, "You need to have read access to this list.") } + if models.IsErrNamespaceDoesNotExist(err) { + return echo.NewHTTPError(http.StatusNotFound, "This namespace does not exist.") + } + return echo.NewHTTPError(http.StatusInternalServerError, "An error occured.") } diff --git a/routes/routes.go b/routes/routes.go index 8650b15a1..5b819f83d 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -119,6 +119,7 @@ func RegisterRoutes(e *echo.Echo) { a.GET("/namespaces/:namespace/teams", namespaceTeamHandler.ReadAllWeb) a.PUT("/namespaces/:namespace/teams", namespaceTeamHandler.CreateWeb) a.DELETE("/namespaces/:namespace/teams/:team", namespaceTeamHandler.DeleteWeb) + a.POST("/namespaces/:namespace/teams/:team", namespaceTeamHandler.UpdateWeb) namespaceUserHandler := &crud.WebHandler{ CObject: &models.NamespaceUser{},