vikunja-api/pkg/models/namespace_rights.go

140 lines
4 KiB
Go
Raw Normal View History

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