vikunja/pkg/models/project_rights.go

277 lines
7.6 KiB
Go
Raw Permalink Normal View History

2020-02-07 16:27:45 +00:00
// Vikunja is a to-do list application to facilitate your life.
// Copyright 2018-present Vikunja and contributors. All rights reserved.
2018-11-26 20:17:33 +00:00
//
// This program is free software: you can redistribute it and/or modify
2020-12-23 15:41:52 +00:00
// it under the terms of the GNU Affero General Public Licensee as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
2018-11-26 20:17:33 +00: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 15:41:52 +00:00
// GNU Affero General Public Licensee for more details.
2018-11-26 20:17:33 +00:00
//
2020-12-23 15:41:52 +00:00
// You should have received a copy of the GNU Affero General Public Licensee
// along with this program. If not, see <https://www.gnu.org/licenses/>.
2018-11-26 20:17:33 +00:00
package models
2018-10-31 12:42:38 +00:00
import (
2023-01-13 17:42:31 +00:00
"errors"
"code.vikunja.io/api/pkg/user"
2018-11-30 23:26:56 +00:00
"code.vikunja.io/web"
"xorm.io/builder"
Use db sessions everywere (#750) 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>
2020-12-23 15:32:28 +00:00
"xorm.io/xorm"
2018-10-31 12:42:38 +00:00
)
2022-11-13 16:07:01 +00:00
// CanWrite return whether the user can write on that project or not
2022-12-29 15:40:06 +00:00
func (p *Project) CanWrite(s *xorm.Session, a web.Auth) (bool, error) {
2018-11-30 23:26:56 +00:00
2022-11-13 16:07:01 +00:00
// The favorite project can't be edited
2022-12-29 15:40:06 +00:00
if p.ID == FavoritesPseudoProject.ID {
return false, nil
}
2022-11-13 16:07:01 +00:00
// Get the project and check the right
2022-12-29 15:40:06 +00:00
originalProject, err := GetProjectSimpleByID(s, p.ID)
if err != nil {
2019-03-24 12:35:50 +00:00
return false, err
}
// We put the result of the is archived check in a separate variable to be able to return it later without
// needing to recheck it again
2022-11-13 16:07:01 +00:00
errIsArchived := originalProject.CheckIsArchived(s)
var canWrite bool
// Check if we're dealing with a share auth
shareAuth, ok := a.(*LinkSharing)
if ok {
2022-11-13 16:07:01 +00:00
return originalProject.ID == shareAuth.ProjectID &&
(shareAuth.Right == RightWrite || shareAuth.Right == RightAdmin), errIsArchived
}
2022-11-13 16:07:01 +00:00
// Check if the user is either owner or can write to the project
if originalProject.isOwner(&user.User{ID: a.GetID()}) {
canWrite = true
}
if canWrite {
return canWrite, errIsArchived
2019-03-24 12:35:50 +00:00
}
2022-11-13 16:07:01 +00:00
canWrite, _, err = originalProject.checkRight(s, a, RightWrite, RightAdmin)
if err != nil {
return false, err
}
return canWrite, errIsArchived
}
2022-11-13 16:07:01 +00:00
// CanRead checks if a user has read access to a project
2022-12-29 15:40:06 +00:00
func (p *Project) CanRead(s *xorm.Session, a web.Auth) (bool, int, error) {
2022-11-13 16:07:01 +00:00
// The favorite project needs a special treatment
2022-12-29 15:40:06 +00:00
if p.ID == FavoritesPseudoProject.ID {
owner, err := user.GetFromAuth(a)
if err != nil {
return false, 0, err
}
2022-12-29 15:40:06 +00:00
*p = FavoritesPseudoProject
p.Owner = owner
return true, int(RightRead), nil
}
2022-11-13 16:07:01 +00:00
// Saved Filter Projects need a special case
2022-12-29 15:40:06 +00:00
if getSavedFilterIDFromProjectID(p.ID) > 0 {
sf := &SavedFilter{ID: getSavedFilterIDFromProjectID(p.ID)}
Use db sessions everywere (#750) 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>
2020-12-23 15:32:28 +00:00
return sf.CanRead(s, a)
}
// Check if the user is either owner or can read
Use db sessions everywere (#750) 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>
2020-12-23 15:32:28 +00:00
var err error
2022-12-29 15:40:06 +00:00
originalProject, err := GetProjectSimpleByID(s, p.ID)
Use db sessions everywere (#750) 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>
2020-12-23 15:32:28 +00:00
if err != nil {
return false, 0, err
}
2022-12-29 15:40:06 +00:00
*p = *originalProject
Use db sessions everywere (#750) 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>
2020-12-23 15:32:28 +00:00
// Check if we're dealing with a share auth
shareAuth, ok := a.(*LinkSharing)
if ok {
2022-12-29 15:40:06 +00:00
return p.ID == shareAuth.ProjectID &&
(shareAuth.Right == RightRead || shareAuth.Right == RightWrite || shareAuth.Right == RightAdmin), int(shareAuth.Right), nil
}
2022-12-29 15:40:06 +00:00
if p.isOwner(&user.User{ID: a.GetID()}) {
return true, int(RightAdmin), nil
2019-03-24 12:35:50 +00:00
}
2022-12-29 15:40:06 +00:00
return p.checkRight(s, a, RightRead, RightWrite, RightAdmin)
}
2018-12-29 14:29:50 +00:00
2022-11-13 16:07:01 +00:00
// CanUpdate checks if the user can update a project
2022-12-29 15:40:06 +00:00
func (p *Project) CanUpdate(s *xorm.Session, a web.Auth) (canUpdate bool, err error) {
2022-11-13 16:07:01 +00:00
// The favorite project can't be edited
2022-12-29 15:40:06 +00:00
if p.ID == FavoritesPseudoProject.ID {
return false, nil
}
2022-11-13 16:07:01 +00:00
// Get the project
2022-12-29 15:40:06 +00:00
ol, err := GetProjectSimpleByID(s, p.ID)
if err != nil {
return false, err
}
2022-12-29 15:40:06 +00:00
// Check if we're moving the project to a different parent project.
// If that is the case, we need to verify permissions to do so.
2022-12-29 15:40:06 +00:00
if p.ParentProjectID != 0 && p.ParentProjectID != ol.ParentProjectID {
newProject := &Project{ID: p.ParentProjectID}
can, err := newProject.CanWrite(s, a)
if err != nil {
return false, err
}
if !can {
return false, ErrGenericForbidden{}
}
}
2022-12-29 15:40:06 +00:00
fid := getSavedFilterIDFromProjectID(p.ID)
if fid > 0 {
sf, err := getSavedFilterSimpleByID(s, fid)
if err != nil {
return false, err
}
return sf.CanUpdate(s, a)
}
2022-12-29 15:40:06 +00:00
canUpdate, err = p.CanWrite(s, a)
2022-11-13 16:07:01 +00:00
// If the project is archived and the user tries to un-archive it, let the request through
2023-01-13 17:42:31 +00:00
archivedErr := ErrProjectIsArchived{}
is := errors.As(err, &archivedErr)
if is && !p.IsArchived && archivedErr.ProjectID == p.ID {
err = nil
}
return canUpdate, err
}
2022-11-13 16:07:01 +00:00
// CanDelete checks if the user can delete a project
2022-12-29 15:40:06 +00:00
func (p *Project) CanDelete(s *xorm.Session, a web.Auth) (bool, error) {
return p.IsAdmin(s, a)
2018-07-12 21:07:03 +00:00
}
2018-07-12 21:16:32 +00:00
2022-11-13 16:07:01 +00:00
// CanCreate checks if the user can create a project
2022-12-29 15:40:06 +00:00
func (p *Project) CanCreate(s *xorm.Session, a web.Auth) (bool, error) {
if p.ParentProjectID != 0 {
parent := &Project{ID: p.ParentProjectID}
return parent.CanWrite(s, a)
}
// Check if we're dealing with a share auth
_, is := a.(*LinkSharing)
if is {
return false, nil
}
return true, nil
2018-07-12 21:16:32 +00:00
}
2022-11-13 16:07:01 +00:00
// IsAdmin returns whether the user has admin rights on the project or not
2022-12-29 15:40:06 +00:00
func (p *Project) IsAdmin(s *xorm.Session, a web.Auth) (bool, error) {
2022-11-13 16:07:01 +00:00
// The favorite project can't be edited
2022-12-29 15:40:06 +00:00
if p.ID == FavoritesPseudoProject.ID {
return false, nil
}
2022-12-29 15:40:06 +00:00
originalProject, err := GetProjectSimpleByID(s, p.ID)
if err != nil {
2019-03-24 12:35:50 +00:00
return false, err
}
// Check if we're dealing with a share auth
shareAuth, ok := a.(*LinkSharing)
if ok {
2022-11-13 16:07:01 +00:00
return originalProject.ID == shareAuth.ProjectID && shareAuth.Right == RightAdmin, nil
}
// Check all the things
2022-11-13 16:07:01 +00:00
// Check if the user is either owner or can write to the project
// Owners are always admins
2022-11-13 16:07:01 +00:00
if originalProject.isOwner(&user.User{ID: a.GetID()}) {
2019-03-24 12:35:50 +00:00
return true, nil
}
2022-11-13 16:07:01 +00:00
is, _, err := originalProject.checkRight(s, a, RightAdmin)
return is, err
}
2022-11-13 16:07:01 +00:00
// Little helper function to check if a user is project owner
2022-12-29 15:40:06 +00:00
func (p *Project) isOwner(u *user.User) bool {
return p.OwnerID == u.ID
}
2018-09-06 06:42:18 +00:00
// Checks n different rights for any given user
2022-12-29 15:40:06 +00:00
func (p *Project) checkRight(s *xorm.Session, a web.Auth, rights ...Right) (bool, int, error) {
var conds []builder.Cond
for _, r := range rights {
// User conditions
2022-11-13 16:07:01 +00:00
// If the project was shared directly with the user and the user has the right
conds = append(conds, builder.And(
builder.Eq{"ul.user_id": a.GetID()},
builder.Eq{"ul.right": r},
))
// Team rights
2022-11-13 16:07:01 +00:00
// If the project was shared directly with the team and the team has the right
conds = append(conds, builder.And(
builder.Eq{"tm2.user_id": a.GetID()},
builder.Eq{"tl.right": r},
))
}
2022-11-13 16:07:01 +00:00
type allProjectRights struct {
2022-12-29 15:40:06 +00:00
UserProject *ProjectUser `xorm:"extends"`
TeamProject *TeamProject `xorm:"extends"`
}
2022-11-13 16:07:01 +00:00
r := &allProjectRights{}
var maxRight = 0
Use db sessions everywere (#750) 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>
2020-12-23 15:32:28 +00:00
exists, err := s.
Select("p.*, ul.right, tl.right").
2022-11-13 16:07:01 +00:00
Table("projects").
2022-12-29 15:40:06 +00:00
Alias("p").
// User stuff
2022-12-29 15:40:06 +00:00
Join("LEFT", []string{"users_projects", "ul"}, "ul.project_id = p.id").
// Team stuff
2022-12-29 15:40:06 +00:00
Join("LEFT", []string{"team_projects", "tl"}, "p.id = tl.project_id").
Join("LEFT", []string{"team_members", "tm2"}, "tm2.team_id = tl.team_id").
// The actual condition
Where(builder.And(
builder.Or(
conds...,
),
2022-12-29 15:40:06 +00:00
builder.Eq{"p.id": p.ID},
)).
Get(r)
// If there's noting shared for this project, and it has a parent, go up the tree
if !exists && p.ParentProjectID > 0 {
parent, err := GetProjectSimpleByID(s, p.ParentProjectID)
if err != nil {
return false, 0, err
}
return parent.checkRight(s, a, rights...)
}
// Figure out the max right and return it
2022-11-13 16:07:01 +00:00
if int(r.UserProject.Right) > maxRight {
maxRight = int(r.UserProject.Right)
}
2022-11-13 16:07:01 +00:00
if int(r.TeamProject.Right) > maxRight {
maxRight = int(r.TeamProject.Right)
}
return exists, maxRight, err
2018-09-06 06:42:18 +00:00
}