diff --git a/models/lists.go b/models/lists.go index 31ae74c6955..bdf1297c872 100644 --- a/models/lists.go +++ b/models/lists.go @@ -10,7 +10,7 @@ type List struct { Created int64 `xorm:"created" json:"created"` Updated int64 `xorm:"updated" json:"updated"` - Owner User `xorm:"-" json:"owner"` + Owner User `xorm:"-" json:"owner"` Items []*ListItem `xorm:"-" json:"items"` } @@ -70,4 +70,4 @@ func GetListsByNamespaceID(nID int64) (lists []*List, err error) { } return -} \ No newline at end of file +} diff --git a/models/namespaces_list.go b/models/namespaces_list.go index ed32902d69d..990791ad4f7 100644 --- a/models/namespaces_list.go +++ b/models/namespaces_list.go @@ -26,10 +26,10 @@ func CreateOrUpdateNamespace(namespace *Namespace) (err error) { } // GetAllNamespacesByUserID does what it says -func GetAllNamespacesByUserID(userID int64) (namespaces []*Namespace, err error) { +func GetAllNamespacesByUserID(userID int64) (namespaces []Namespace, err error) { // First, get all namespaces which that user owns - err = x.Where("owner_id = ?", userID).Find(namespaces) + err = x.Where("owner_id = ?", userID).Find(&namespaces) if err != nil { return namespaces, err } @@ -39,5 +39,14 @@ func GetAllNamespacesByUserID(userID int64) (namespaces []*Namespace, err error) Join("INNER", ). Find(namespaces)*/ + // Get user objects + // I couldn't come up with a more performant way to do this... + for in, n := range namespaces { + namespaces[in].Owner, _, err = GetUserByID(n.OwnerID) + if err != nil { + return nil, err + } + } + return } diff --git a/public/swagger/swagger.v1.json b/public/swagger/swagger.v1.json index f8c16e36db0..e02b95f84a0 100644 --- a/public/swagger/swagger.v1.json +++ b/public/swagger/swagger.v1.json @@ -489,6 +489,41 @@ } } }, + "/namespaces/{namespaceID}/lists": { + "get": { + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "namespaces" + ], + "summary": "gets all lists belonging to that namespace", + "operationId": "getListsByNamespace", + "parameters": [ + { + "type": "string", + "description": "ID of the namespace", + "name": "namespaceID", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "$ref": "#/responses/Namespace" + }, + "400": { + "$ref": "#/responses/Message" + }, + "500": { + "$ref": "#/responses/Message" + } + } + } + }, "/register": { "post": { "consumes": [ @@ -653,6 +688,39 @@ }, "x-go-package": "git.kolaente.de/konrad/list/models" }, + "Namespace": { + "description": "Namespace holds informations about a namespace", + "type": "object", + "properties": { + "created": { + "type": "integer", + "format": "int64", + "x-go-name": "Created" + }, + "description": { + "type": "string", + "x-go-name": "Description" + }, + "id": { + "type": "integer", + "format": "int64", + "x-go-name": "ID" + }, + "name": { + "type": "string", + "x-go-name": "Name" + }, + "owner": { + "$ref": "#/definitions/User" + }, + "updated": { + "type": "integer", + "format": "int64", + "x-go-name": "Updated" + } + }, + "x-go-package": "git.kolaente.de/konrad/list/models" + }, "User": { "description": "User holds information about an user", "type": "object", @@ -708,6 +776,12 @@ "$ref": "#/definitions/Message" } }, + "Namespace": { + "description": "Namespace", + "schema": { + "$ref": "#/definitions/Namespace" + } + }, "Token": { "description": "Token", "schema": { @@ -733,7 +807,7 @@ "parameterBodies": { "description": "parameterBodies", "schema": { - "$ref": "#/definitions/ListItem" + "$ref": "#/definitions/Namespace" } } }, diff --git a/routes/api/v1/list_by_namespace.go b/routes/api/v1/list_by_namespace.go index a63811dd2b2..5a70cbec44b 100644 --- a/routes/api/v1/list_by_namespace.go +++ b/routes/api/v1/list_by_namespace.go @@ -1,14 +1,14 @@ package v1 import ( - "github.com/labstack/echo" - "strconv" - "net/http" "git.kolaente.de/konrad/list/models" + "github.com/labstack/echo" + "net/http" + "strconv" ) func GetListsByNamespaceID(c echo.Context) error { - // swagger:operation GET /namespaces/{namespaceID}/lists namespaces getNamespaces + // swagger:operation GET /namespaces/{namespaceID}/lists namespaces getListsByNamespace // --- // summary: gets all lists belonging to that namespace // consumes: @@ -29,8 +29,6 @@ func GetListsByNamespaceID(c echo.Context) error { // "500": // "$ref": "#/responses/Message" - - // Check if we have our ID id := c.Param("id") // Make int @@ -45,7 +43,7 @@ func GetListsByNamespaceID(c echo.Context) error { if err != nil { return c.JSON(http.StatusInternalServerError, models.Message{"An error occured."}) } - has, err := user.HasNamespaceAccess(&models.Namespace{ID:namespaceID}) + has, err := user.HasNamespaceAccess(&models.Namespace{ID: namespaceID}) if err != nil { return c.JSON(http.StatusInternalServerError, models.Message{"An error occured."}) } @@ -65,4 +63,4 @@ func GetListsByNamespaceID(c echo.Context) error { } return c.JSON(http.StatusOK, lists) -} \ No newline at end of file +} diff --git a/routes/api/v1/namespace_add_update.go b/routes/api/v1/namespace_add_update.go index f9dd4f8d76b..5009e2aefd9 100644 --- a/routes/api/v1/namespace_add_update.go +++ b/routes/api/v1/namespace_add_update.go @@ -112,13 +112,17 @@ func addOrUpdateNamespace(c echo.Context) error { return c.JSON(http.StatusInternalServerError, models.Message{"An error occured."}) } } else { - // Check if the user owns the namespace + // Check if the user has admin access to the namespace oldNamespace, err := models.GetNamespaceByID(namespace.ID) if err != nil { return c.JSON(http.StatusInternalServerError, models.Message{"An error occured."}) } - if user.ID != oldNamespace.Owner.ID { - return c.JSON(http.StatusForbidden, models.Message{"You cannot edit a namespace you don't own."}) + has, err := user.IsNamespaceAdmin(oldNamespace) + if err != nil { + return c.JSON(http.StatusInternalServerError, models.Message{"An error occured."}) + } + if !has { + return c.JSON(http.StatusForbidden, models.Message{"You need to be namespace admin to edit a namespace."}) } err = models.CreateOrUpdateNamespace(namespace) diff --git a/routes/api/v1/namespace_show.go b/routes/api/v1/namespace_show.go index 0d70be5b982..95ad363b160 100644 --- a/routes/api/v1/namespace_show.go +++ b/routes/api/v1/namespace_show.go @@ -43,7 +43,7 @@ func ShowNamespace(c echo.Context) error { if err != nil { return c.JSON(http.StatusInternalServerError, models.Message{"An error occured."}) } - has, err := user.HasNamespaceAccess(&models.Namespace{ID:namespaceID}) + has, err := user.HasNamespaceAccess(&models.Namespace{ID: namespaceID}) if err != nil { return c.JSON(http.StatusInternalServerError, models.Message{"An error occured."}) } diff --git a/routes/api/v1/swagger/options.go b/routes/api/v1/swagger/options.go index 6542f58afb3..e5c9de7cde0 100644 --- a/routes/api/v1/swagger/options.go +++ b/routes/api/v1/swagger/options.go @@ -19,4 +19,7 @@ type swaggerParameterBodies struct { // in:body ListItem models.ListItem + + // in:body + Namespace models.Namespace } diff --git a/routes/api/v1/swagger/responses.go b/routes/api/v1/swagger/responses.go index 36cd0f8e9c1..abdf674905b 100644 --- a/routes/api/v1/swagger/responses.go +++ b/routes/api/v1/swagger/responses.go @@ -50,3 +50,14 @@ type swaggerResponseLIstItem struct { // in:body Body models.ListItem `json:"body"` } + +// ================ +// Namespace definitions +// ================ + +// Namespace +// swagger:response Namespace +type swaggerResponseNamespace struct { + // in:body + Body models.Namespace `json:"body"` +}