From edf9b6f2c76a82f5df6be5c3f489000dad142894 Mon Sep 17 00:00:00 2001 From: kolaente Date: Tue, 24 Jul 2018 17:29:13 +0200 Subject: [PATCH] Implemented creating a team <-> list relation --- Featurecreep.md | 6 +++--- models/error.go | 18 +++++++++++++++++- models/list_create_update.go | 1 + models/namespace_rights.go | 18 ------------------ models/team_list.go | 20 ++++++++++++++++++++ models/team_list_create.go | 26 ++++++++++++++++++++++++++ models/team_list_rights.go | 7 +++++++ models/team_namespace.go | 8 ++++---- models/team_namespace_create.go | 8 ++------ models/teams.go | 16 ---------------- models/teams_rights.go | 26 ++++++++++++++++++++++++++ routes/routes.go | 7 +++++++ 12 files changed, 113 insertions(+), 48 deletions(-) create mode 100644 models/team_list.go create mode 100644 models/team_list_create.go create mode 100644 models/team_list_rights.go diff --git a/Featurecreep.md b/Featurecreep.md index a3b814407e..bc7c8bb24c 100644 --- a/Featurecreep.md +++ b/Featurecreep.md @@ -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 diff --git a/models/error.go b/models/error.go index dd950bed20..b6fe3128d3 100644 --- a/models/error.go +++ b/models/error.go @@ -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. diff --git a/models/list_create_update.go b/models/list_create_update.go index ef5dfadad8..1031b4a04e 100644 --- a/models/list_create_update.go +++ b/models/list_create_update.go @@ -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 diff --git a/models/namespace_rights.go b/models/namespace_rights.go index 1cdcf070d3..66cc48d879 100644 --- a/models/namespace_rights.go +++ b/models/namespace_rights.go @@ -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 { diff --git a/models/team_list.go b/models/team_list.go new file mode 100644 index 0000000000..85b512dbbb --- /dev/null +++ b/models/team_list.go @@ -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" +} diff --git a/models/team_list_create.go b/models/team_list_create.go new file mode 100644 index 0000000000..3ed547f032 --- /dev/null +++ b/models/team_list_create.go @@ -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 +} diff --git a/models/team_list_rights.go b/models/team_list_rights.go new file mode 100644 index 0000000000..089af7a452 --- /dev/null +++ b/models/team_list_rights.go @@ -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) +} diff --git a/models/team_namespace.go b/models/team_namespace.go index b84cdf9753..a83286264c 100644 --- a/models/team_namespace.go +++ b/models/team_namespace.go @@ -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"` diff --git a/models/team_namespace_create.go b/models/team_namespace_create.go index 4c78d12dd0..3a365990de 100644 --- a/models/team_namespace_create.go +++ b/models/team_namespace_create.go @@ -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 { diff --git a/models/teams.go b/models/teams.go index fe9ec42177..a7bd121fc9 100644 --- a/models/teams.go +++ b/models/teams.go @@ -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"). diff --git a/models/teams_rights.go b/models/teams_rights.go index 1d6457871d..16f192dcbf 100644 --- a/models/teams_rights.go +++ b/models/teams_rights.go @@ -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. diff --git a/routes/routes.go b/routes/routes.go index ff0945674f..d7eef98b18 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -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{}, }