2020-02-07 17:27:45 +01:00
|
|
|
// Vikunja is a to-do list application to facilitate your life.
|
2021-02-02 20:19:13 +01:00
|
|
|
// Copyright 2018-2021 Vikunja and contributors. All rights reserved.
|
2018-12-31 02:18:41 +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-12-31 02:18:41 +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-12-31 02:18:41 +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-12-31 02:18:41 +01:00
|
|
|
|
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
2020-01-26 18:08:06 +01:00
|
|
|
"code.vikunja.io/api/pkg/user"
|
2018-12-31 02:18:41 +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-12-31 02:18:41 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// CanUpdate checks if a user can update a label
|
2020-12-23 16:32:28 +01:00
|
|
|
func (l *Label) CanUpdate(s *xorm.Session, a web.Auth) (bool, error) {
|
|
|
|
return l.isLabelOwner(s, a) // Only owners should be allowed to update a label
|
2018-12-31 02:18:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// CanDelete checks if a user can delete a label
|
2020-12-23 16:32:28 +01:00
|
|
|
func (l *Label) CanDelete(s *xorm.Session, a web.Auth) (bool, error) {
|
|
|
|
return l.isLabelOwner(s, a) // Only owners should be allowed to delete a label
|
2018-12-31 02:18:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// CanRead checks if a user can read a label
|
2020-12-23 16:32:28 +01:00
|
|
|
func (l *Label) CanRead(s *xorm.Session, a web.Auth) (bool, int, error) {
|
|
|
|
return l.hasAccessToLabel(s, a)
|
2018-12-31 02:18:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// CanCreate checks if the user can create a label
|
|
|
|
// Currently a dummy.
|
2020-12-23 16:32:28 +01:00
|
|
|
func (l *Label) 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
|
|
|
|
}
|
|
|
|
|
2019-03-24 13:35:50 +01:00
|
|
|
return true, nil
|
2018-12-31 02:18:41 +01:00
|
|
|
}
|
|
|
|
|
2020-12-23 16:32:28 +01:00
|
|
|
func (l *Label) isLabelOwner(s *xorm.Session, a web.Auth) (bool, error) {
|
2019-08-31 22:56:41 +02:00
|
|
|
|
|
|
|
if _, is := a.(*LinkSharing); is {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2020-12-23 16:32:28 +01:00
|
|
|
lorig, err := getLabelByIDSimple(s, l.ID)
|
2018-12-31 02:18:41 +01:00
|
|
|
if err != nil {
|
2019-03-24 13:35:50 +01:00
|
|
|
return false, err
|
2018-12-31 02:18:41 +01:00
|
|
|
}
|
2019-06-28 10:21:48 +02:00
|
|
|
return lorig.CreatedByID == a.GetID(), nil
|
2018-12-31 02:18:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Helper method to check if a user can see a specific label
|
2020-12-23 16:32:28 +01:00
|
|
|
func (l *Label) hasAccessToLabel(s *xorm.Session, a web.Auth) (has bool, maxRight int, err error) {
|
2019-08-31 22:56:41 +02:00
|
|
|
|
2021-03-24 22:46:20 +01:00
|
|
|
if _, is := a.(*LinkSharing); is {
|
|
|
|
return false, 0, nil
|
|
|
|
}
|
2019-08-31 22:56:41 +02:00
|
|
|
|
2021-03-24 22:46:20 +01:00
|
|
|
u, err := user.GetUserByID(s, a.GetID())
|
2018-12-31 02:18:41 +01:00
|
|
|
if err != nil {
|
2020-08-10 14:11:43 +02:00
|
|
|
return false, 0, err
|
2018-12-31 02:18:41 +01:00
|
|
|
}
|
|
|
|
|
2021-03-28 20:17:35 +02:00
|
|
|
cond := builder.In("label_tasks.task_id",
|
2021-03-24 22:46:20 +01:00
|
|
|
builder.
|
|
|
|
Select("id").
|
|
|
|
From("tasks").
|
|
|
|
Where(builder.In("list_id", getUserListsStatement(u.ID).Select("l.id"))),
|
|
|
|
)
|
|
|
|
|
2020-08-10 14:11:43 +02:00
|
|
|
ll := &LabelTask{}
|
2020-12-23 16:32:28 +01:00
|
|
|
has, err = s.Table("labels").
|
2021-03-28 20:17:35 +02:00
|
|
|
Select("label_tasks.*").
|
|
|
|
Join("LEFT", "label_tasks", "label_tasks.label_id = labels.id").
|
|
|
|
Where("label_tasks.label_id is not null OR labels.created_by_id = ?", u.ID).
|
2021-03-24 22:46:20 +01:00
|
|
|
Or(cond).
|
2018-12-31 02:18:41 +01:00
|
|
|
And("labels.id = ?", l.ID).
|
2020-08-10 14:11:43 +02:00
|
|
|
Exist(ll)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Since the right depends on the task the label is associated with, we need to check that too.
|
|
|
|
if ll.TaskID > 0 {
|
|
|
|
t := &Task{ID: ll.TaskID}
|
2020-12-23 16:32:28 +01:00
|
|
|
_, maxRight, err = t.CanRead(s, a)
|
2020-08-10 14:11:43 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
2018-12-31 02:18:41 +01:00
|
|
|
}
|