vikunja-api/pkg/models/namespace_rights.go

113 lines
3.1 KiB
Go
Raw Normal View History

package models
2018-10-31 13:42:38 +01: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 13:42:38 +01:00
func (n *Namespace) IsAdmin(u *User) bool {
// 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-10-31 13:42:38 +01:00
func (n *Namespace) CanWrite(u *User) bool {
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-10-31 13:42:38 +01:00
func (n *Namespace) CanRead(u *User) bool {
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-10-31 13:42:38 +01:00
func (n *Namespace) CanUpdate(u *User) bool {
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-10-31 13:42:38 +01:00
func (n *Namespace) CanDelete(u *User) bool {
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-10-31 13:42:38 +01:00
func (n *Namespace) CanCreate(u *User) 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-09-06 08:46:47 +02:00
Where("namespaces.id = ? "+
2018-10-06 22:26:17 +02:00
"AND (team_members.user_id = ? AND team_namespaces.right = ?) "+
2018-10-31 13:42:38 +01:00
"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 ("+
"namespaces.owner_id = ? "+
2018-10-31 13:42:38 +01: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 13:42:38 +01:00
log.Log.Error("Error occurred during checkUserRights for Namespace: %s, UserRight: %d", err, r)
return false
}
return exists
}