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"` Created int64 `xorm:"created" json:"created"`
Updated int64 `xorm:"updated" json:"updated"` Updated int64 `xorm:"updated" json:"updated"`
Owner User `xorm:"-" json:"owner"` Owner User `xorm:"-" json:"owner"`
Items []*ListItem `xorm:"-" json:"items"` Items []*ListItem `xorm:"-" json:"items"`
} }
@ -70,4 +70,4 @@ func GetListsByNamespaceID(nID int64) (lists []*List, err error) {
} }
return return
} }

View file

@ -26,10 +26,10 @@ func CreateOrUpdateNamespace(namespace *Namespace) (err error) {
} }
// GetAllNamespacesByUserID does what it says // 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 // 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 { if err != nil {
return namespaces, err return namespaces, err
} }
@ -39,5 +39,14 @@ func GetAllNamespacesByUserID(userID int64) (namespaces []*Namespace, err error)
Join("INNER", ). Join("INNER", ).
Find(namespaces)*/ 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 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": { "/register": {
"post": { "post": {
"consumes": [ "consumes": [
@ -653,6 +688,39 @@
}, },
"x-go-package": "git.kolaente.de/konrad/list/models" "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": { "User": {
"description": "User holds information about an user", "description": "User holds information about an user",
"type": "object", "type": "object",
@ -708,6 +776,12 @@
"$ref": "#/definitions/Message" "$ref": "#/definitions/Message"
} }
}, },
"Namespace": {
"description": "Namespace",
"schema": {
"$ref": "#/definitions/Namespace"
}
},
"Token": { "Token": {
"description": "Token", "description": "Token",
"schema": { "schema": {
@ -733,7 +807,7 @@
"parameterBodies": { "parameterBodies": {
"description": "parameterBodies", "description": "parameterBodies",
"schema": { "schema": {
"$ref": "#/definitions/ListItem" "$ref": "#/definitions/Namespace"
} }
} }
}, },

View file

@ -1,14 +1,14 @@
package v1 package v1
import ( import (
"github.com/labstack/echo"
"strconv"
"net/http"
"git.kolaente.de/konrad/list/models" "git.kolaente.de/konrad/list/models"
"github.com/labstack/echo"
"net/http"
"strconv"
) )
func GetListsByNamespaceID(c echo.Context) error { 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 // summary: gets all lists belonging to that namespace
// consumes: // consumes:
@ -29,8 +29,6 @@ func GetListsByNamespaceID(c echo.Context) error {
// "500": // "500":
// "$ref": "#/responses/Message" // "$ref": "#/responses/Message"
// Check if we have our ID // Check if we have our ID
id := c.Param("id") id := c.Param("id")
// Make int // Make int
@ -45,7 +43,7 @@ func GetListsByNamespaceID(c echo.Context) error {
if err != nil { if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"An error occured."}) 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 { if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"An error occured."}) 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) 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."}) return c.JSON(http.StatusInternalServerError, models.Message{"An error occured."})
} }
} else { } else {
// Check if the user owns the namespace // Check if the user has admin access to the namespace
oldNamespace, err := models.GetNamespaceByID(namespace.ID) oldNamespace, err := models.GetNamespaceByID(namespace.ID)
if err != nil { if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"An error occured."}) return c.JSON(http.StatusInternalServerError, models.Message{"An error occured."})
} }
if user.ID != oldNamespace.Owner.ID { has, err := user.IsNamespaceAdmin(oldNamespace)
return c.JSON(http.StatusForbidden, models.Message{"You cannot edit a namespace you don't own."}) 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) err = models.CreateOrUpdateNamespace(namespace)

View file

@ -43,7 +43,7 @@ func ShowNamespace(c echo.Context) error {
if err != nil { if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"An error occured."}) 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 { if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"An error occured."}) return c.JSON(http.StatusInternalServerError, models.Message{"An error occured."})
} }

View file

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

View file

@ -50,3 +50,14 @@ type swaggerResponseLIstItem struct {
// in:body // in:body
Body models.ListItem `json:"body"` Body models.ListItem `json:"body"`
} }
// ================
// Namespace definitions
// ================
// Namespace
// swagger:response Namespace
type swaggerResponseNamespace struct {
// in:body
Body models.Namespace `json:"body"`
}