Added method to show all namespaces a user has access to
This commit is contained in:
parent
6dfdcb9571
commit
124e4f4a5b
8 changed files with 88 additions and 35 deletions
|
@ -207,7 +207,6 @@ func (err ErrNeedToBeItemOwner) Error() string {
|
|||
return fmt.Sprintf("You need to be item owner to do that [ItemID: %d, UserID: %d]", err.ItemID, err.UserID)
|
||||
}
|
||||
|
||||
|
||||
// =================
|
||||
// Namespace errors
|
||||
// =================
|
||||
|
@ -230,7 +229,7 @@ func (err ErrNamespaceDoesNotExist) Error() string {
|
|||
// ErrNeedToBeNamespaceOwner represents an error, where the user is not the owner of that namespace (used i.e. when deleting a namespace)
|
||||
type ErrNeedToBeNamespaceOwner struct {
|
||||
NamespaceID int64
|
||||
UserID int64
|
||||
UserID int64
|
||||
}
|
||||
|
||||
// IsErrNamespaceDoesNotExist checks if an error is a ErrNamespaceDoesNotExist.
|
||||
|
@ -241,4 +240,4 @@ func IsErrNeedToBeNamespaceOwner(err error) bool {
|
|||
|
||||
func (err ErrNeedToBeNamespaceOwner) Error() string {
|
||||
return fmt.Sprintf("You need to be namespace owner to do that [NamespaceID: %d, UserID: %d]", err.NamespaceID, err.UserID)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -44,7 +44,6 @@ func (user *User) IsNamespaceAdmin(namespace *Namespace) (ok bool, err error) {
|
|||
|
||||
// Check if that user is in a team which has admin rights to that namespace
|
||||
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -56,7 +55,6 @@ func (user *User) HasNamespaceAccess(namespace *Namespace) (has bool, err error)
|
|||
|
||||
// Check if the user is in a team which has access to the namespace
|
||||
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -68,7 +66,7 @@ func GetNamespaceByID(id int64) (namespace *Namespace, err error) {
|
|||
}
|
||||
|
||||
if !exists {
|
||||
return namespace, ErrNamespaceDoesNotExist{ID:id}
|
||||
return namespace, ErrNamespaceDoesNotExist{ID: id}
|
||||
}
|
||||
|
||||
// Get the namespace Owner
|
||||
|
@ -79,28 +77,3 @@ func GetNamespaceByID(id int64) (namespace *Namespace, err error) {
|
|||
|
||||
return namespace, err
|
||||
}
|
||||
|
||||
// CreateOrUpdateNamespace does what it says
|
||||
func CreateOrUpdateNamespace(namespace *Namespace) (err error) {
|
||||
// Check if the User exists
|
||||
_, _, err = GetUserByID(namespace.Owner.ID)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
namespace.OwnerID = namespace.Owner.ID
|
||||
|
||||
if namespace.ID == 0 {
|
||||
_, err = x.Insert(namespace)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
} else {
|
||||
_, err = x.ID(namespace.ID).Update(namespace)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
|
1
models/namespaces_add_update.go
Normal file
1
models/namespaces_add_update.go
Normal file
|
@ -0,0 +1 @@
|
|||
package models
|
46
models/namespaces_list.go
Normal file
46
models/namespaces_list.go
Normal file
|
@ -0,0 +1,46 @@
|
|||
package models
|
||||
|
||||
// CreateOrUpdateNamespace does what it says
|
||||
func CreateOrUpdateNamespace(namespace *Namespace) (err error) {
|
||||
// Check if the User exists
|
||||
_, _, err = GetUserByID(namespace.Owner.ID)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
namespace.OwnerID = namespace.Owner.ID
|
||||
|
||||
if namespace.ID == 0 {
|
||||
_, err = x.Insert(namespace)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
} else {
|
||||
_, err = x.ID(namespace.ID).Update(namespace)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// GetAllNamespacesByUserID does what it says
|
||||
func GetAllNamespacesByUserID(userID int64) (namespaces []*Namespace, err error) {
|
||||
|
||||
// First, get all namespaces which that user owns
|
||||
err = x.Where("owner_id = ?", userID).Find(namespaces)
|
||||
if err != nil {
|
||||
return namespaces, err
|
||||
}
|
||||
|
||||
// Get all namespaces of teams that user is part of
|
||||
/*err = x.Table("namespaces").
|
||||
Join("INNER", ).
|
||||
Find(namespaces)*/
|
||||
|
||||
|
||||
|
||||
|
||||
return
|
||||
}
|
|
@ -64,7 +64,6 @@ func (TeamList) TableName() string {
|
|||
return "team_list"
|
||||
}
|
||||
|
||||
|
||||
func GetAllTeamsByNamespaceID(id int64) (teams []*Team, err error) {
|
||||
err = x.Table("teams").
|
||||
Join("INNER", "team_namespaces", "teams.id = team_id").
|
||||
|
@ -72,4 +71,4 @@ func GetAllTeamsByNamespaceID(id int64) (teams []*Team, err error) {
|
|||
Find(teams)
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
|
@ -51,7 +51,7 @@ func CreateUser(user User) (newUser User, err error) {
|
|||
}
|
||||
|
||||
// Create the user's namespace
|
||||
err = CreateOrUpdateNamespace(&Namespace{Name: newUserOut.Username, Description: newUserOut.Username + "'s namespace.", Owner:newUserOut})
|
||||
err = CreateOrUpdateNamespace(&Namespace{Name: newUserOut.Username, Description: newUserOut.Username + "'s namespace.", Owner: newUserOut})
|
||||
if err != nil {
|
||||
return User{}, err
|
||||
}
|
||||
|
|
35
routes/api/v1/namespaces_list.go
Normal file
35
routes/api/v1/namespaces_list.go
Normal file
|
@ -0,0 +1,35 @@
|
|||
package v1
|
||||
|
||||
import (
|
||||
"github.com/labstack/echo"
|
||||
"git.kolaente.de/konrad/list/models"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func GetAllNamespacesByCurrentUser(c echo.Context) error {
|
||||
// swagger:operation GET /namespaces namespaces getNamespaces
|
||||
// ---
|
||||
// summary: Get all namespaces the currently logged in user has at least read access
|
||||
// consumes:
|
||||
// - application/json
|
||||
// produces:
|
||||
// - application/json
|
||||
// responses:
|
||||
// "200":
|
||||
// "$ref": "#/responses/Namespace"
|
||||
// "500":
|
||||
// "$ref": "#/responses/Message"
|
||||
|
||||
|
||||
user, err := models.GetCurrentUser(c)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, models.Message{"Could not get the current user."})
|
||||
}
|
||||
|
||||
namespaces, err := models.GetAllNamespacesByUserID(user.ID)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, models.Message{"Could not get namespaces."})
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, namespaces)
|
||||
}
|
|
@ -93,7 +93,7 @@ func RegisterRoutes(e *echo.Echo) {
|
|||
a.DELETE("/item/:id", apiv1.DeleteListItemByIDtemByID)
|
||||
a.POST("/item/:id", apiv1.UpdateListItem)
|
||||
|
||||
a.GET("/namespaces")
|
||||
a.GET("/namespaces", apiv1.GetAllNamespacesByCurrentUser)
|
||||
a.PUT("/namespaces", apiv1.AddNamespace)
|
||||
a.GET("/namespaces/:id")
|
||||
a.POST("/namespaces/:id", apiv1.UpdateNamespace)
|
||||
|
|
Loading…
Reference in a new issue