Small fixes to manage namespaces

This commit is contained in:
kolaente 2018-07-03 08:48:28 +02:00
parent e0244e28d7
commit 064e1cd3b7
No known key found for this signature in database
GPG key ID: F40E70337AB24C9B
8 changed files with 116 additions and 17 deletions

View file

@ -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
}
}

View file

@ -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
}

View file

@ -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"
}
}
},

View file

@ -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)
}
}

View file

@ -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)

View file

@ -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."})
}

View file

@ -19,4 +19,7 @@ type swaggerParameterBodies struct {
// in:body
ListItem models.ListItem
// in:body
Namespace models.Namespace
}

View file

@ -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"`
}