Implemented creating a team <-> list relation

This commit is contained in:
kolaente 2018-07-24 17:29:13 +02:00 committed by konrad
parent 6ff10e6353
commit edf9b6f2c7
Signed by untrusted user: konrad
GPG Key ID: F40E70337AB24C9B
12 changed files with 113 additions and 48 deletions

View File

@ -132,10 +132,10 @@ Teams sind global, d.h. Ein Team kann mehrere Namespaces verwalten.
*Routen*
* [ ] `namespaces/:id/teams`
* [x] `namespaces/:id/teams`
* [x] Create
* [ ] ReadAll
* [ ] Delete
* [x] ReadAll
* [x] Delete
* [ ] `lists/:id/teams`
* [ ] Create
* [ ] ReadAll

View File

@ -175,6 +175,22 @@ func (err ErrNeedToBeListWriter) Error() string {
return fmt.Sprintf("You need to have write acces to the list to do that [ListID: %d, UserID: %d]", err.ListID, err.UserID)
}
// ErrNeedToHaveListReadAccess represents an error, where the user dont has read access to that List
type ErrNeedToHaveListReadAccess struct {
ListID int64
UserID int64
}
// IsErrNeedToHaveListReadAccess checks if an error is a ErrListDoesNotExist.
func IsErrNeedToHaveListReadAccess(err error) bool {
_, ok := err.(ErrNeedToHaveListReadAccess)
return ok
}
func (err ErrNeedToHaveListReadAccess) Error() string {
return fmt.Sprintf("You need to be List owner to do that [ListID: %d, UserID: %d]", err.ListID, err.UserID)
}
// ================
// List item errors
// ================
@ -406,7 +422,7 @@ func (err ErrTeamDoesNotExist) Error() string {
// ErrInvalidTeamRight represents an error where a team right is invalid
type ErrInvalidTeamRight struct {
Right NamespaceRight
Right TeamRight
}
// IsErrInvalidTeamRight checks if an error is ErrInvalidTeamRight.

View File

@ -38,6 +38,7 @@ func (l *List) Create(doer *User) (err error) {
return
}
l.OwnerID = user.ID
l.Owner.ID = user.ID
l.ID = 0 // Otherwise only the first time a new list would be created

View File

@ -1,23 +1,5 @@
package models
// NamespaceRight defines the rights teams can have for namespaces
type NamespaceRight int
// define unknown namespace right
const (
NamespaceRightUnknown = -1
)
// Enumerate all the namespace rights
const (
// Can read lists in a namespace
NamespaceRightRead NamespaceRight = iota
// Can write items in a namespace like lists and todo items. Cannot create new lists.
NamespaceRightWrite
// Can manage a namespace, can do everything
NamespaceRightAdmin
)
// IsAdmin returns true or false if the user is admin on that namespace or not
func (n *Namespace) IsAdmin(user *User) bool {

20
models/team_list.go Normal file
View File

@ -0,0 +1,20 @@
package models
// TeamList defines the relation between a team and a list
type TeamList struct {
ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id"`
TeamID int64 `xorm:"int(11) not null" json:"team_id" param:"team"`
ListID int64 `xorm:"int(11) not null" json:"list_id" param:"list"`
Right TeamRight `xorm:"int(11)" json:"right"`
Created int64 `xorm:"created" json:"created"`
Updated int64 `xorm:"updated" json:"updated"`
CRUDable `xorm:"-" json:"-"`
Rights `xorm:"-" json:"-"`
}
// TableName makes beautiful table names
func (TeamList) TableName() string {
return "team_list"
}

View File

@ -0,0 +1,26 @@
package models
// Create creates a new team <-> list relation
func (tl *TeamList) Create(doer *User) (err error) {
// Check if the rights are valid
if err = tl.Right.isValid(); err != nil {
return
}
// Check if the team exists
_, err = GetTeamByID(tl.TeamID)
if err != nil {
return
}
// Check if the list exists
_, err = GetListByID(tl.ListID)
if err != nil {
return
}
// Insert the new team
_, err = x.Insert(tl)
return
}

View File

@ -0,0 +1,7 @@
package models
// CanCreate checks if the use can create a team <-> list relation
func (tl *TeamList) CanCreate(user *User) bool {
l, _ := GetListByID(tl.ListID)
return l.IsAdmin(user)
}

View File

@ -2,10 +2,10 @@ package models
// TeamNamespace defines the relationship between a Team and a Namespace
type TeamNamespace struct {
ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id"`
TeamID int64 `xorm:"int(11) not null" json:"team_id" param:"team"`
NamespaceID int64 `xorm:"int(11) not null" json:"namespace_id" param:"namespace"`
Right NamespaceRight `xorm:"int(11)" json:"right"`
ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id"`
TeamID int64 `xorm:"int(11) not null" json:"team_id" param:"team"`
NamespaceID int64 `xorm:"int(11) not null" json:"namespace_id" param:"namespace"`
Right TeamRight `xorm:"int(11)" json:"right"`
Created int64 `xorm:"created" json:"created"`
Updated int64 `xorm:"updated" json:"updated"`

View File

@ -1,17 +1,13 @@
package models
import "fmt"
// Create creates a new team <-> namespace relation
func (tn *TeamNamespace) Create(doer *User) (err error) {
// Check if the rights are valid
if tn.Right != NamespaceRightAdmin && tn.Right != NamespaceRightRead && tn.Right != NamespaceRightWrite {
return ErrInvalidTeamRight{tn.Right}
if err = tn.Right.isValid(); err != nil {
return
}
fmt.Println(tn.NamespaceID)
// Check if the team exists
_, err = GetTeamByID(tn.TeamID)
if err != nil {

View File

@ -60,22 +60,6 @@ type TeamUser struct {
IsAdmin bool `json:"is_admin"`
}
// TeamList defines the relation between a team and a list
type TeamList struct {
ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id"`
TeamID int64 `xorm:"int(11) not null" json:"team_id"`
ListID int64 `xorm:"int(11) not null" json:"list_id"`
Rights int `xorm:"varchar(250)" json:"rights"`
Created int64 `xorm:"created" json:"created"`
Updated int64 `xorm:"updated" json:"updated"`
}
// TableName makes beautiful table names
func (TeamList) TableName() string {
return "team_list"
}
// GetAllTeamsByNamespaceID returns all teams for a namespace
func GetAllTeamsByNamespaceID(id int64) (teams []*Team, err error) {
err = x.Table("teams").

View File

@ -1,5 +1,31 @@
package models
// TeamRight defines the rights teams can have for lists/namespaces
type TeamRight int
// define unknown team right
const (
TeamRightUnknown = -1
)
// Enumerate all the team rights
const (
// Can read lists in a Team
TeamRightRead TeamRight = iota
// Can write items in a Team like lists and todo items. Cannot create new lists.
TeamRightWrite
// Can manage a list/namespace, can do everything
TeamRightAdmin
)
func (r TeamRight) isValid() error {
if r != TeamRightAdmin && r != TeamRightRead && r != TeamRightWrite {
return ErrInvalidTeamRight{r}
}
return nil
}
// CanCreate checks if the user can create a new team
func (t *Team) CanCreate(user *User) bool {
// This is currently a dummy function, later on we could imagine global limits etc.

View File

@ -125,6 +125,13 @@ func RegisterRoutes(e *echo.Echo) {
a.DELETE("/items/:listitem", itemHandler.DeleteWeb)
a.POST("/items/:listitem", itemHandler.UpdateWeb)
listTeamHandler := &crud.WebHandler{
CObject: &models.TeamList{},
}
a.GET("/lists/:list/teams", listTeamHandler.ReadAllWeb)
a.PUT("/lists/:list/teams", listTeamHandler.CreateWeb)
a.DELETE("/lists/:list/teams/:team", listTeamHandler.DeleteWeb)
namespaceHandler := &crud.WebHandler{
CObject: &models.Namespace{},
}