
Fix lint Fix lint Fix loading tasks with search Fix loading lists Fix loading task Fix loading lists and namespaces Fix tests Fix user commands Fix upload Fix migration handlers Fix all manual root handlers Fix session in avatar Fix session in list duplication & routes Use sessions in migration code Make sure the openid stuff uses a session Add alias for db type in db package Use sessions for file Use a session for everything in users Use a session for everything in users Make sure to use a session everywhere in models Create new session from db Add session handling for user list Add session handling for unsplash Add session handling for teams and related Add session handling for tasks and related entities Add session handling for task reminders Add session handling for task relations Add session handling for task comments Add session handling for task collections Add session handling for task attachments Add session handling for task assignees Add session handling for saved filters Add session handling for namespace and related types Add session handling for namespace and related types Add session handling for list users Add session handling for list tests Add session handling to list teams and related entities Add session handling for link shares and related entities Add session handling for labels and related entities Add session handling for kanban and related entities Add session handling for bulk task and related entities Add session handling for lists and related entities Add session configuration for web handler Update web handler Co-authored-by: kolaente <k@knt.li> Reviewed-on: https://kolaente.dev/vikunja/api/pulls/750 Co-Authored-By: konrad <konrad@kola-entertainments.de> Co-Committed-By: konrad <konrad@kola-entertainments.de>
143 lines
4.3 KiB
Go
143 lines
4.3 KiB
Go
// Vikunja is a to-do list application to facilitate your life.
|
|
// Copyright 2018-2020 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
|
|
|
|
import (
|
|
"code.vikunja.io/web"
|
|
"xorm.io/builder"
|
|
"xorm.io/xorm"
|
|
)
|
|
|
|
// CanWrite checks if a user has write access to a namespace
|
|
func (n *Namespace) CanWrite(s *xorm.Session, a web.Auth) (bool, error) {
|
|
can, _, err := n.checkRight(s, a, RightWrite, RightAdmin)
|
|
return can, err
|
|
}
|
|
|
|
// IsAdmin returns true or false if the user is admin on that namespace or not
|
|
func (n *Namespace) IsAdmin(s *xorm.Session, a web.Auth) (bool, error) {
|
|
is, _, err := n.checkRight(s, a, RightAdmin)
|
|
return is, err
|
|
}
|
|
|
|
// CanRead checks if a user has read access to that namespace
|
|
func (n *Namespace) CanRead(s *xorm.Session, a web.Auth) (bool, int, error) {
|
|
return n.checkRight(s, a, RightRead, RightWrite, RightAdmin)
|
|
}
|
|
|
|
// CanUpdate checks if the user can update the namespace
|
|
func (n *Namespace) CanUpdate(s *xorm.Session, a web.Auth) (bool, error) {
|
|
return n.IsAdmin(s, a)
|
|
}
|
|
|
|
// CanDelete checks if the user can delete a namespace
|
|
func (n *Namespace) CanDelete(s *xorm.Session, a web.Auth) (bool, error) {
|
|
return n.IsAdmin(s, a)
|
|
}
|
|
|
|
// CanCreate checks if the user can create a new namespace
|
|
func (n *Namespace) CanCreate(s *xorm.Session, a web.Auth) (bool, error) {
|
|
if _, is := a.(*LinkSharing); is {
|
|
return false, nil
|
|
}
|
|
|
|
// This is currently a dummy function, later on we could imagine global limits etc.
|
|
return true, nil
|
|
}
|
|
|
|
func (n *Namespace) checkRight(s *xorm.Session, a web.Auth, rights ...Right) (bool, int, error) {
|
|
|
|
// If the auth is a link share, don't do anything
|
|
if _, is := a.(*LinkSharing); is {
|
|
return false, 0, nil
|
|
}
|
|
|
|
// Get the namespace and check the right
|
|
nn, err := getNamespaceSimpleByID(s, n.ID)
|
|
if err != nil {
|
|
return false, 0, err
|
|
}
|
|
|
|
if a.GetID() == nn.OwnerID {
|
|
return true, int(RightAdmin), nil
|
|
}
|
|
|
|
/*
|
|
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
|
|
conds = append(conds, builder.Eq{"namespaces.owner_id": a.GetID()})
|
|
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(
|
|
builder.Eq{"users_namespace.user_id": a.GetID()},
|
|
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(
|
|
builder.Eq{"team_members.user_id": a.GetID()},
|
|
builder.Eq{"team_namespaces.right": r},
|
|
))
|
|
}
|
|
|
|
type allRights struct {
|
|
UserNamespace NamespaceUser `xorm:"extends"`
|
|
TeamNamespace TeamNamespace `xorm:"extends"`
|
|
}
|
|
|
|
var maxRights = 0
|
|
r := &allRights{}
|
|
exists, err := s.
|
|
Select("*").
|
|
Table("namespaces").
|
|
// User stuff
|
|
Join("LEFT", "users_namespace", "users_namespace.namespace_id = namespaces.id").
|
|
// 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},
|
|
)).
|
|
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
|
|
}
|