api/pkg/models/namespace_rights.go

129 lines
3.8 KiB
Go
Raw Normal View History

2018-11-26 20:17:33 +00: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/>.
package models
2018-10-31 12:42:38 +00:00
import (
"code.vikunja.io/api/pkg/log"
)
// IsAdmin returns true or false if the user is admin on that namespace or not
2018-10-31 12:42:38 +00:00
func (n *Namespace) IsAdmin(u *User) bool {
// Owners always have admin rights
2018-10-31 12:42:38 +00:00
if u.ID == n.Owner.ID {
return true
}
2018-09-06 06:46:34 +00:00
// Check user rights
2018-10-31 12:42:38 +00:00
if n.checkUserRights(u, UserRightAdmin) {
2018-09-06 06:46:34 +00:00
return true
}
// Check if that user is in a team which has admin rights to that namespace
2018-10-31 12:42:38 +00:00
return n.checkTeamRights(u, TeamRightAdmin)
}
// CanWrite checks if a user has write access to a namespace
2018-10-31 12:42:38 +00:00
func (n *Namespace) CanWrite(u *User) bool {
2018-09-06 06:46:34 +00:00
// Admins always have write access
2018-10-31 12:42:38 +00:00
if n.IsAdmin(u) {
return true
}
2018-09-06 06:46:34 +00:00
// Check user rights
2018-10-31 12:42:38 +00:00
if n.checkUserRights(u, UserRightWrite) {
return true
}
// Check if that user is in a team which has write rights to that namespace
2018-10-31 12:42:38 +00:00
return n.checkTeamRights(u, TeamRightWrite)
}
// CanRead checks if a user has read access to that namespace
2018-10-31 12:42:38 +00:00
func (n *Namespace) CanRead(u *User) bool {
2018-09-06 06:46:34 +00:00
// Admins always have read access
2018-10-31 12:42:38 +00:00
if n.IsAdmin(u) {
return true
}
2018-09-06 06:46:34 +00:00
// Check user rights
2018-10-31 12:42:38 +00:00
if n.checkUserRights(u, UserRightRead) {
return true
}
// Check if the user is in a team which has access to the namespace
2018-10-31 12:42:38 +00:00
return n.checkTeamRights(u, TeamRightRead)
}
2018-07-12 21:07:03 +00:00
// CanUpdate checks if the user can update the namespace
2018-10-31 12:42:38 +00:00
func (n *Namespace) CanUpdate(u *User) bool {
nn, err := GetNamespaceByID(n.ID)
if err != nil {
2018-10-31 12:42:38 +00:00
log.Log.Error("Error occurred during CanUpdate for Namespace: %s", err)
return false
}
2018-10-31 12:42:38 +00:00
return nn.IsAdmin(u)
2018-07-12 21:07:03 +00:00
}
2018-07-12 21:16:32 +00:00
2018-07-12 21:33:37 +00:00
// CanDelete checks if the user can delete a namespace
2018-10-31 12:42:38 +00:00
func (n *Namespace) CanDelete(u *User) bool {
nn, err := GetNamespaceByID(n.ID)
if err != nil {
2018-10-31 12:42:38 +00:00
log.Log.Error("Error occurred during CanDelete for Namespace: %s", err)
return false
}
2018-10-31 12:42:38 +00:00
return nn.IsAdmin(u)
2018-07-12 21:33:37 +00:00
}
2018-07-12 21:16:32 +00:00
// CanCreate checks if the user can create a new namespace
2018-10-31 12:42:38 +00:00
func (n *Namespace) CanCreate(u *User) bool {
2018-07-12 21:16:32 +00:00
// This is currently a dummy function, later on we could imagine global limits etc.
return true
}
2018-10-31 12:42:38 +00:00
func (n *Namespace) checkTeamRights(u *User, r TeamRight) bool {
exists, err := x.Select("namespaces.*").
Table("namespaces").
Join("LEFT", "team_namespaces", "namespaces.id = team_namespaces.namespace_id").
Join("LEFT", "team_members", "team_members.team_id = team_namespaces.team_id").
2018-09-06 06:46:47 +00:00
Where("namespaces.id = ? "+
2018-10-06 20:26:17 +00:00
"AND (team_members.user_id = ? AND team_namespaces.right = ?) "+
2018-10-31 12:42:38 +00:00
"OR namespaces.owner_id = ? ", n.ID, u.ID, r, u.ID).
2018-09-06 06:46:34 +00:00
Get(&Namespace{})
if err != nil {
2018-10-31 12:42:38 +00:00
log.Log.Error("Error occurred during checkTeamRights for Namespace: %s, TeamRight: %d", err, r)
2018-09-06 06:46:34 +00:00
return false
}
return exists
}
2018-10-31 12:42:38 +00:00
func (n *Namespace) checkUserRights(u *User, r UserRight) bool {
2018-09-06 06:46:34 +00:00
exists, err := x.Select("namespaces.*").
Table("namespaces").
Join("LEFT", "users_namespace", "users_namespace.namespace_id = namespaces.id").
Where("namespaces.id = ? AND ("+
"namespaces.owner_id = ? "+
2018-10-31 12:42:38 +00:00
"OR (users_namespace.user_id = ? AND users_namespace.right = ?))", n.ID, u.ID, u.ID, r).
Get(&Namespace{})
if err != nil {
2018-10-31 12:42:38 +00:00
log.Log.Error("Error occurred during checkUserRights for Namespace: %s, UserRight: %d", err, r)
return false
}
return exists
}