2018-07-14 17:34:59 +02:00
|
|
|
package models
|
|
|
|
|
2018-07-24 17:29:13 +02:00
|
|
|
// 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
|
2018-08-30 08:09:17 +02:00
|
|
|
// Can write tasks in a Team like lists and todo tasks. Cannot create new lists.
|
2018-07-24 17:29:13 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-07-14 17:34:59 +02:00
|
|
|
// CanCreate checks if the user can create a new team
|
2018-07-18 08:56:19 +02:00
|
|
|
func (t *Team) CanCreate(user *User) bool {
|
2018-07-14 17:34:59 +02:00
|
|
|
// This is currently a dummy function, later on we could imagine global limits etc.
|
|
|
|
return true
|
2018-07-14 17:37:46 +02:00
|
|
|
}
|
2018-07-14 18:29:24 +02:00
|
|
|
|
|
|
|
// CanUpdate checks if the user can update a team
|
2018-07-21 15:17:02 +02:00
|
|
|
func (t *Team) CanUpdate(user *User) bool {
|
2018-07-14 18:29:24 +02:00
|
|
|
|
|
|
|
// Check if the current user is in the team and has admin rights in it
|
2018-07-21 15:17:02 +02:00
|
|
|
exists, _ := x.Where("team_id = ?", t.ID).
|
2018-07-14 18:29:24 +02:00
|
|
|
And("user_id = ?", user.ID).
|
2018-07-26 09:53:32 +02:00
|
|
|
And("admin = ?", true).
|
2018-07-14 18:29:24 +02:00
|
|
|
Get(&TeamMember{})
|
|
|
|
|
|
|
|
return exists
|
|
|
|
}
|
2018-07-16 08:18:15 +02:00
|
|
|
|
2018-07-16 08:45:38 +02:00
|
|
|
// CanDelete checks if a user can delete a team
|
2018-07-18 08:15:38 +02:00
|
|
|
func (t *Team) CanDelete(user *User) bool {
|
|
|
|
//t.ID = id
|
2018-07-16 08:18:15 +02:00
|
|
|
return t.IsAdmin(user)
|
|
|
|
}
|
|
|
|
|
2018-07-16 08:45:38 +02:00
|
|
|
// IsAdmin returns true when the user is admin of a team
|
2018-07-16 08:18:15 +02:00
|
|
|
func (t *Team) IsAdmin(user *User) bool {
|
|
|
|
exists, _ := x.Where("team_id = ?", t.ID).
|
|
|
|
And("user_id = ?", user.ID).
|
2018-07-26 09:53:32 +02:00
|
|
|
And("admin = ?", true).
|
2018-07-16 08:18:15 +02:00
|
|
|
Get(&TeamMember{})
|
|
|
|
return exists
|
2018-07-16 08:43:47 +02:00
|
|
|
}
|
|
|
|
|
2018-07-16 08:45:38 +02:00
|
|
|
// CanRead returns true if the user has read access to the team
|
2018-07-16 08:43:47 +02:00
|
|
|
func (t *Team) CanRead(user *User) bool {
|
|
|
|
// Check if the user is in the team
|
|
|
|
exists, _ := x.Where("team_id = ?", t.ID).
|
|
|
|
And("user_id = ?", user.ID).
|
|
|
|
Get(&TeamMember{})
|
|
|
|
return exists
|
|
|
|
}
|