2018-07-12 21:07:59 +02:00
|
|
|
package models
|
|
|
|
|
|
|
|
// IsAdmin returns whether the user has admin rights on the list or not
|
|
|
|
func (l *List) IsAdmin(user *User) bool {
|
|
|
|
// Owners are always admins
|
|
|
|
if l.Owner.ID == user.ID {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-09-04 19:54:15 +02:00
|
|
|
// Check individual rights
|
2018-09-06 08:42:18 +02:00
|
|
|
if l.checkListUserRight(user, UserRightAdmin) {
|
|
|
|
return true
|
|
|
|
}
|
2018-09-04 19:54:15 +02:00
|
|
|
|
2018-07-25 00:40:24 +02:00
|
|
|
return l.checkListTeamRight(user, TeamRightAdmin)
|
2018-07-12 21:07:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// CanWrite return whether the user can write on that list or not
|
|
|
|
func (l *List) CanWrite(user *User) bool {
|
2018-09-06 08:42:18 +02:00
|
|
|
// Admins always have write access
|
|
|
|
if l.IsAdmin(user) {
|
2018-07-12 21:07:59 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-09-04 19:54:15 +02:00
|
|
|
// Check individual rights
|
2018-09-06 08:42:18 +02:00
|
|
|
if l.checkListUserRight(user, UserRightWrite) {
|
2018-07-12 21:07:59 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-07-25 00:40:24 +02:00
|
|
|
return l.checkListTeamRight(user, TeamRightWrite)
|
2018-07-12 21:07:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// CanRead checks if a user has read access to a list
|
|
|
|
func (l *List) CanRead(user *User) bool {
|
2018-09-06 08:42:18 +02:00
|
|
|
// Admins always have read access
|
|
|
|
if l.IsAdmin(user) {
|
2018-07-12 21:07:59 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-09-04 19:54:15 +02:00
|
|
|
// Check individual rights
|
2018-09-06 08:42:18 +02:00
|
|
|
if l.checkListUserRight(user, UserRightRead) {
|
2018-07-12 21:07:59 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-07-25 00:40:24 +02:00
|
|
|
return l.checkListTeamRight(user, TeamRightRead)
|
2018-07-12 21:07:59 +02:00
|
|
|
}
|
2018-07-12 21:20:24 +02:00
|
|
|
|
|
|
|
// CanDelete checks if the user can delete a list
|
2018-07-18 08:15:38 +02:00
|
|
|
func (l *List) CanDelete(doer *User) bool {
|
|
|
|
list, _ := GetListByID(l.ID)
|
2018-07-12 23:33:21 +02:00
|
|
|
return list.IsAdmin(doer)
|
2018-07-12 23:07:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// CanUpdate checks if the user can update a list
|
2018-07-21 15:17:02 +02:00
|
|
|
func (l *List) CanUpdate(doer *User) bool {
|
|
|
|
list, _ := GetListByID(l.ID)
|
2018-07-12 23:07:03 +02:00
|
|
|
return list.CanWrite(doer)
|
|
|
|
}
|
2018-07-12 23:16:32 +02:00
|
|
|
|
|
|
|
// CanCreate checks if the user can update a list
|
2018-07-18 08:56:19 +02:00
|
|
|
func (l *List) CanCreate(doer *User) bool {
|
2018-07-12 23:16:32 +02:00
|
|
|
// A user can create a list if he has write access to the namespace
|
2018-07-18 08:56:19 +02:00
|
|
|
n, _ := GetNamespaceByID(l.NamespaceID)
|
2018-07-12 23:16:32 +02:00
|
|
|
return n.CanWrite(doer)
|
|
|
|
}
|
2018-07-25 00:40:24 +02:00
|
|
|
|
|
|
|
func (l *List) checkListTeamRight(user *User, r TeamRight) bool {
|
|
|
|
exists, err := x.Select("l.*").
|
|
|
|
Table("list").
|
|
|
|
Alias("l").
|
|
|
|
Join("LEFT", []string{"team_namespaces", "tn"}, "tn.namespace_id = tn.id").
|
|
|
|
Join("LEFT", []string{"team_members", "tm"}, "tm.team_id = tn.team_id").
|
|
|
|
Join("LEFT", []string{"team_list", "tl"}, "l.id = tl.list_id").
|
|
|
|
Join("LEFT", []string{"team_members", "tm2"}, "tm2.team_id = tl.team_id").
|
|
|
|
Where("((tm.user_id = ? AND tn.right = ?) OR (tm2.user_id = ? AND tl.rights = ?)) AND l.id = ?",
|
2018-07-25 00:49:44 +02:00
|
|
|
user.ID, r, user.ID, r, l.ID).
|
2018-07-25 00:40:24 +02:00
|
|
|
Get(&List{})
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return exists
|
|
|
|
}
|
2018-09-06 08:42:18 +02:00
|
|
|
|
|
|
|
func (l *List) checkListUserRight(user *User, r UserRight) bool {
|
|
|
|
exists, err := x.Select("l.*").
|
|
|
|
Table("list").
|
|
|
|
Alias("l").
|
|
|
|
Join("LEFT", []string{"users_namespace", "un"}, "un.namespace_id = l.namespace_id").
|
|
|
|
Join("LEFT", []string{"users_list", "ul"}, "ul.list_id = l.id").
|
|
|
|
Where("(ul.user_id = ? AND ul.right = ?) AND l.id = ?",
|
|
|
|
user.ID, r, l.ID).
|
|
|
|
Get(&List{})
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return exists
|
|
|
|
}
|