2018-11-26 21:17:33 +01:00
|
|
|
// Vikunja is a todo-list application to facilitate your life.
|
|
|
|
// Copyright 2018 Vikunja and contributors. All rights reserved.
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
2018-07-12 21:07:59 +02:00
|
|
|
package models
|
|
|
|
|
2018-10-31 13:42:38 +01:00
|
|
|
import (
|
|
|
|
"code.vikunja.io/api/pkg/log"
|
2018-12-01 00:26:56 +01:00
|
|
|
"code.vikunja.io/web"
|
2018-10-31 13:42:38 +01:00
|
|
|
)
|
|
|
|
|
2018-07-12 21:07:59 +02:00
|
|
|
// IsAdmin returns whether the user has admin rights on the list or not
|
2018-12-01 00:26:56 +01:00
|
|
|
func (l *List) IsAdmin(a web.Auth) bool {
|
|
|
|
u := getUserForRights(a)
|
|
|
|
|
2018-07-12 21:07:59 +02:00
|
|
|
// Owners are always admins
|
2018-12-16 14:21:32 +01:00
|
|
|
if l.OwnerID == u.ID {
|
2018-07-12 21:07:59 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-09-04 19:54:15 +02:00
|
|
|
// Check individual rights
|
2018-10-31 13:42:38 +01:00
|
|
|
if l.checkListUserRight(u, UserRightAdmin) {
|
2018-09-06 08:42:18 +02:00
|
|
|
return true
|
|
|
|
}
|
2018-09-04 19:54:15 +02:00
|
|
|
|
2018-10-31 13:42:38 +01:00
|
|
|
return l.checkListTeamRight(u, TeamRightAdmin)
|
2018-07-12 21:07:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// CanWrite return whether the user can write on that list or not
|
2018-12-01 00:26:56 +01:00
|
|
|
func (l *List) CanWrite(a web.Auth) bool {
|
|
|
|
user := getUserForRights(a)
|
|
|
|
|
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
|
2018-12-01 00:26:56 +01:00
|
|
|
func (l *List) CanRead(a web.Auth) bool {
|
|
|
|
user := getUserForRights(a)
|
|
|
|
|
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-12-01 00:26:56 +01:00
|
|
|
func (l *List) CanDelete(a web.Auth) bool {
|
|
|
|
doer := getUserForRights(a)
|
|
|
|
|
2018-10-06 13:05:29 +02:00
|
|
|
if err := l.GetSimpleByID(); err != nil {
|
2018-10-31 13:42:38 +01:00
|
|
|
log.Log.Error("Error occurred during CanDelete for List: %s", err)
|
2018-10-06 13:05:29 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return l.IsAdmin(doer)
|
2018-07-12 23:07:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// CanUpdate checks if the user can update a list
|
2018-12-01 00:26:56 +01:00
|
|
|
func (l *List) CanUpdate(a web.Auth) bool {
|
|
|
|
doer := getUserForRights(a)
|
|
|
|
|
2018-10-06 13:05:29 +02:00
|
|
|
if err := l.GetSimpleByID(); err != nil {
|
2018-10-31 13:42:38 +01:00
|
|
|
log.Log.Error("Error occurred during CanUpdate for List: %s", err)
|
2018-10-06 13:05:29 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return l.CanWrite(doer)
|
2018-07-12 23:07:03 +02:00
|
|
|
}
|
2018-07-12 23:16:32 +02:00
|
|
|
|
|
|
|
// CanCreate checks if the user can update a list
|
2018-12-01 00:26:56 +01:00
|
|
|
func (l *List) CanCreate(a web.Auth) 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-12-01 00:26:56 +01:00
|
|
|
return n.CanWrite(a)
|
2018-07-12 23:16:32 +02:00
|
|
|
}
|
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").
|
2018-09-12 20:13:00 +02:00
|
|
|
Join("LEFT", []string{"team_namespaces", "tn"}, " l.namespace_id = tn.namespace_id").
|
2018-07-25 00:40:24 +02:00
|
|
|
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").
|
2018-10-06 22:26:17 +02:00
|
|
|
Where("((tm.user_id = ? AND tn.right = ?) OR (tm2.user_id = ? AND tl.right = ?)) AND l.id = ?",
|
2018-07-25 00:49:44 +02:00
|
|
|
user.ID, r, user.ID, r, l.ID).
|
2018-09-12 19:56:07 +02:00
|
|
|
Exist(&List{})
|
2018-07-25 00:40:24 +02:00
|
|
|
if err != nil {
|
2018-10-31 13:42:38 +01:00
|
|
|
log.Log.Error("Error occurred during checkListTeamRight for List: %s", err)
|
2018-07-25 00:40:24 +02:00
|
|
|
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").
|
2018-09-12 19:56:07 +02:00
|
|
|
Join("LEFT", []string{"namespaces", "n"}, "n.id = l.namespace_id").
|
2018-09-12 21:45:30 +02:00
|
|
|
Where("((ul.user_id = ? AND ul.right = ?) "+
|
|
|
|
"OR (un.user_id = ? AND un.right = ?) "+
|
|
|
|
"OR n.owner_id = ?)"+
|
2018-09-12 19:56:07 +02:00
|
|
|
"AND l.id = ?",
|
|
|
|
user.ID, r, user.ID, r, user.ID, l.ID).
|
|
|
|
Exist(&List{})
|
2018-09-06 08:42:18 +02:00
|
|
|
if err != nil {
|
2018-10-31 13:42:38 +01:00
|
|
|
log.Log.Error("Error occurred during checkListUserRight for List: %s", err)
|
2018-09-06 08:42:18 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return exists
|
|
|
|
}
|