2020-02-07 17:27:45 +01:00
|
|
|
// Vikunja is a to-do list application to facilitate your life.
|
2020-01-09 18:33:22 +01:00
|
|
|
// Copyright 2018-2020 Vikunja and contributors. All rights reserved.
|
2018-11-26 21:17:33 +01:00
|
|
|
//
|
2019-12-04 20:39:56 +01:00
|
|
|
// This program is free software: you can redistribute it and/or modify
|
2020-12-23 16:41:52 +01:00
|
|
|
// it under the terms of the GNU Affero General Public Licensee as published by
|
2019-12-04 20:39:56 +01:00
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
2018-11-26 21:17:33 +01:00
|
|
|
//
|
2019-12-04 20:39:56 +01:00
|
|
|
// 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
|
2020-12-23 16:41:52 +01:00
|
|
|
// GNU Affero General Public Licensee for more details.
|
2018-11-26 21:17:33 +01:00
|
|
|
//
|
2020-12-23 16:41:52 +01:00
|
|
|
// You should have received a copy of the GNU Affero General Public Licensee
|
2019-12-04 20:39:56 +01:00
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2018-11-26 21:17:33 +01:00
|
|
|
|
2018-07-12 21:07:59 +02:00
|
|
|
package models
|
|
|
|
|
2018-10-31 13:42:38 +01:00
|
|
|
import (
|
2018-12-01 00:26:56 +01:00
|
|
|
"code.vikunja.io/web"
|
2020-02-14 17:34:25 +01:00
|
|
|
"xorm.io/builder"
|
2020-12-23 16:32:28 +01:00
|
|
|
"xorm.io/xorm"
|
2018-10-31 13:42:38 +01:00
|
|
|
)
|
|
|
|
|
2018-07-12 21:07:59 +02:00
|
|
|
// CanWrite checks if a user has write access to a namespace
|
2020-12-23 16:32:28 +01:00
|
|
|
func (n *Namespace) CanWrite(s *xorm.Session, a web.Auth) (bool, error) {
|
|
|
|
can, _, err := n.checkRight(s, a, RightWrite, RightAdmin)
|
2020-08-10 14:11:43 +02:00
|
|
|
return can, err
|
2019-03-08 22:31:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsAdmin returns true or false if the user is admin on that namespace or not
|
2020-12-23 16:32:28 +01:00
|
|
|
func (n *Namespace) IsAdmin(s *xorm.Session, a web.Auth) (bool, error) {
|
|
|
|
is, _, err := n.checkRight(s, a, RightAdmin)
|
2020-08-10 14:11:43 +02:00
|
|
|
return is, err
|
2018-07-12 21:07:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// CanRead checks if a user has read access to that namespace
|
2020-12-23 16:32:28 +01:00
|
|
|
func (n *Namespace) CanRead(s *xorm.Session, a web.Auth) (bool, int, error) {
|
|
|
|
return n.checkRight(s, a, RightRead, RightWrite, RightAdmin)
|
2018-07-12 21:07:59 +02:00
|
|
|
}
|
2018-07-12 23:07:03 +02:00
|
|
|
|
|
|
|
// CanUpdate checks if the user can update the namespace
|
2020-12-23 16:32:28 +01:00
|
|
|
func (n *Namespace) CanUpdate(s *xorm.Session, a web.Auth) (bool, error) {
|
|
|
|
return n.IsAdmin(s, a)
|
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
|
2020-12-23 16:32:28 +01:00
|
|
|
func (n *Namespace) CanDelete(s *xorm.Session, a web.Auth) (bool, error) {
|
|
|
|
return n.IsAdmin(s, a)
|
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
|
2020-12-23 16:32:28 +01:00
|
|
|
func (n *Namespace) CanCreate(s *xorm.Session, a web.Auth) (bool, error) {
|
2019-08-31 22:56:41 +02:00
|
|
|
if _, is := a.(*LinkSharing); is {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2018-07-12 23:16:32 +02:00
|
|
|
// This is currently a dummy function, later on we could imagine global limits etc.
|
2019-03-24 13:35:50 +01:00
|
|
|
return true, nil
|
2018-07-12 23:16:32 +02:00
|
|
|
}
|
2018-07-25 00:49:44 +02:00
|
|
|
|
2020-12-23 16:32:28 +01:00
|
|
|
func (n *Namespace) checkRight(s *xorm.Session, a web.Auth, rights ...Right) (bool, int, error) {
|
2018-09-06 08:46:34 +02:00
|
|
|
|
2019-08-31 22:56:41 +02:00
|
|
|
// If the auth is a link share, don't do anything
|
|
|
|
if _, is := a.(*LinkSharing); is {
|
2020-08-10 14:11:43 +02:00
|
|
|
return false, 0, nil
|
2019-08-31 22:56:41 +02:00
|
|
|
}
|
|
|
|
|
2019-04-01 21:48:48 +02:00
|
|
|
// Get the namespace and check the right
|
2020-12-23 16:32:28 +01:00
|
|
|
nn, err := getNamespaceSimpleByID(s, n.ID)
|
2019-04-01 21:48:48 +02:00
|
|
|
if err != nil {
|
2020-08-10 14:11:43 +02:00
|
|
|
return false, 0, err
|
2019-04-01 21:48:48 +02:00
|
|
|
}
|
|
|
|
|
2020-12-23 16:32:28 +01:00
|
|
|
if a.GetID() == nn.OwnerID {
|
2020-08-10 14:11:43 +02:00
|
|
|
return true, int(RightAdmin), nil
|
2019-04-01 21:48:48 +02:00
|
|
|
}
|
2019-01-21 23:08:04 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
The following loop creates an sql condition like this one:
|
|
|
|
|
|
|
|
namespaces.owner_id = 1 OR
|
|
|
|
(users_namespace.user_id = 1 AND users_namespace.right = 1) OR
|
|
|
|
(team_members.user_id = 1 AND team_namespaces.right = 1) OR
|
|
|
|
|
|
|
|
|
|
|
|
for each passed right. That way, we can check with a single sql query (instead if 8)
|
|
|
|
if the user has the right to see the list or not.
|
|
|
|
*/
|
|
|
|
|
|
|
|
var conds []builder.Cond
|
2019-06-28 10:21:48 +02:00
|
|
|
conds = append(conds, builder.Eq{"namespaces.owner_id": a.GetID()})
|
2019-01-21 23:08:04 +01:00
|
|
|
for _, r := range rights {
|
|
|
|
// User conditions
|
|
|
|
// If the namespace was shared directly with the user and the user has the right
|
|
|
|
conds = append(conds, builder.And(
|
2019-06-28 10:21:48 +02:00
|
|
|
builder.Eq{"users_namespace.user_id": a.GetID()},
|
2019-01-21 23:08:04 +01:00
|
|
|
builder.Eq{"users_namespace.right": r},
|
|
|
|
))
|
|
|
|
|
|
|
|
// Team rights
|
|
|
|
// If the namespace was shared directly with the team and the team has the right
|
|
|
|
conds = append(conds, builder.And(
|
2019-06-28 10:21:48 +02:00
|
|
|
builder.Eq{"team_members.user_id": a.GetID()},
|
2019-01-21 23:08:04 +01:00
|
|
|
builder.Eq{"team_namespaces.right": r},
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2020-08-10 14:11:43 +02:00
|
|
|
type allRights struct {
|
|
|
|
UserNamespace NamespaceUser `xorm:"extends"`
|
|
|
|
TeamNamespace TeamNamespace `xorm:"extends"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var maxRights = 0
|
|
|
|
r := &allRights{}
|
2020-12-23 16:32:28 +01:00
|
|
|
exists, err := s.
|
|
|
|
Select("*").
|
2018-09-06 08:46:34 +02:00
|
|
|
Table("namespaces").
|
2019-01-21 23:08:04 +01:00
|
|
|
// User stuff
|
2018-09-06 08:46:34 +02:00
|
|
|
Join("LEFT", "users_namespace", "users_namespace.namespace_id = namespaces.id").
|
2019-01-21 23:08:04 +01:00
|
|
|
// Teams stuff
|
|
|
|
Join("LEFT", "team_namespaces", "namespaces.id = team_namespaces.namespace_id").
|
|
|
|
Join("LEFT", "team_members", "team_members.team_id = team_namespaces.team_id").
|
|
|
|
// The actual condition
|
|
|
|
Where(builder.And(
|
|
|
|
builder.Or(
|
|
|
|
conds...,
|
|
|
|
),
|
|
|
|
builder.Eq{"namespaces.id": n.ID},
|
|
|
|
)).
|
2020-08-10 14:11:43 +02:00
|
|
|
Exist(r)
|
|
|
|
|
|
|
|
// Figure out the max right and return it
|
|
|
|
if int(r.UserNamespace.Right) > maxRights {
|
|
|
|
maxRights = int(r.UserNamespace.Right)
|
|
|
|
}
|
|
|
|
if int(r.TeamNamespace.Right) > maxRights {
|
|
|
|
maxRights = int(r.TeamNamespace.Right)
|
|
|
|
}
|
|
|
|
|
|
|
|
return exists, maxRights, err
|
2018-07-25 00:49:44 +02:00
|
|
|
}
|