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
No known key found for this signature in database
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* *Routen*
* [ ] `namespaces/:id/teams` * [x] `namespaces/:id/teams`
* [x] Create * [x] Create
* [ ] ReadAll * [x] ReadAll
* [ ] Delete * [x] Delete
* [ ] `lists/:id/teams` * [ ] `lists/:id/teams`
* [ ] Create * [ ] Create
* [ ] ReadAll * [ ] 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) 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 // List item errors
// ================ // ================
@ -406,7 +422,7 @@ func (err ErrTeamDoesNotExist) Error() string {
// ErrInvalidTeamRight represents an error where a team right is invalid // ErrInvalidTeamRight represents an error where a team right is invalid
type ErrInvalidTeamRight struct { type ErrInvalidTeamRight struct {
Right NamespaceRight Right TeamRight
} }
// IsErrInvalidTeamRight checks if an error is ErrInvalidTeamRight. // IsErrInvalidTeamRight checks if an error is ErrInvalidTeamRight.

View file

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

View file

@ -1,23 +1,5 @@
package models 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 // IsAdmin returns true or false if the user is admin on that namespace or not
func (n *Namespace) IsAdmin(user *User) bool { 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

@ -5,7 +5,7 @@ type TeamNamespace struct {
ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id"` ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id"`
TeamID int64 `xorm:"int(11) not null" json:"team_id" param:"team"` TeamID int64 `xorm:"int(11) not null" json:"team_id" param:"team"`
NamespaceID int64 `xorm:"int(11) not null" json:"namespace_id" param:"namespace"` NamespaceID int64 `xorm:"int(11) not null" json:"namespace_id" param:"namespace"`
Right NamespaceRight `xorm:"int(11)" json:"right"` Right TeamRight `xorm:"int(11)" json:"right"`
Created int64 `xorm:"created" json:"created"` Created int64 `xorm:"created" json:"created"`
Updated int64 `xorm:"updated" json:"updated"` Updated int64 `xorm:"updated" json:"updated"`

View file

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

View file

@ -60,22 +60,6 @@ type TeamUser struct {
IsAdmin bool `json:"is_admin"` 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 // GetAllTeamsByNamespaceID returns all teams for a namespace
func GetAllTeamsByNamespaceID(id int64) (teams []*Team, err error) { func GetAllTeamsByNamespaceID(id int64) (teams []*Team, err error) {
err = x.Table("teams"). err = x.Table("teams").

View file

@ -1,5 +1,31 @@
package models 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 // CanCreate checks if the user can create a new team
func (t *Team) CanCreate(user *User) bool { func (t *Team) CanCreate(user *User) bool {
// This is currently a dummy function, later on we could imagine global limits etc. // 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.DELETE("/items/:listitem", itemHandler.DeleteWeb)
a.POST("/items/:listitem", itemHandler.UpdateWeb) 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{ namespaceHandler := &crud.WebHandler{
CObject: &models.Namespace{}, CObject: &models.Namespace{},
} }