vikunja-api/pkg/models/label_rights.go

98 lines
2.7 KiB
Go
Raw Normal View History

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-12-31 02:18:41 +01:00
//
// 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.
2018-12-31 02:18:41 +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
// GNU General Public License for more details.
2018-12-31 02:18:41 +01:00
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
2018-12-31 02:18:41 +01:00
package models
import (
"code.vikunja.io/api/pkg/user"
2018-12-31 02:18:41 +01:00
"code.vikunja.io/web"
"xorm.io/builder"
2018-12-31 02:18:41 +01:00
)
// CanUpdate checks if a user can update a label
2019-03-24 13:35:50 +01:00
func (l *Label) CanUpdate(a web.Auth) (bool, error) {
2018-12-31 02:18:41 +01:00
return l.isLabelOwner(a) // Only owners should be allowed to update a label
}
// CanDelete checks if a user can delete a label
2019-03-24 13:35:50 +01:00
func (l *Label) CanDelete(a web.Auth) (bool, error) {
2018-12-31 02:18:41 +01:00
return l.isLabelOwner(a) // Only owners should be allowed to delete a label
}
// CanRead checks if a user can read a label
func (l *Label) CanRead(a web.Auth) (bool, int, error) {
2018-12-31 02:18:41 +01:00
return l.hasAccessToLabel(a)
}
// CanCreate checks if the user can create a label
// Currently a dummy.
2019-03-24 13:35:50 +01:00
func (l *Label) CanCreate(a web.Auth) (bool, error) {
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
}
2019-03-24 13:35:50 +01:00
func (l *Label) isLabelOwner(a web.Auth) (bool, error) {
if _, is := a.(*LinkSharing); is {
return false, nil
}
2018-12-31 02:18:41 +01:00
lorig, err := getLabelByIDSimple(l.ID)
if err != nil {
2019-03-24 13:35:50 +01:00
return false, err
2018-12-31 02:18:41 +01: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
func (l *Label) hasAccessToLabel(a web.Auth) (has bool, maxRight int, err error) {
// TODO: add an extra check for link share handling
2018-12-31 02:18:41 +01:00
// Get all tasks
taskIDs, err := getUserTaskIDs(&user.User{ID: a.GetID()})
2018-12-31 02:18:41 +01:00
if err != nil {
return false, 0, err
2018-12-31 02:18:41 +01:00
}
// Get all labels associated with these tasks
ll := &LabelTask{}
has, err = x.Table("labels").
Select("label_task.*").
2018-12-31 02:18:41 +01:00
Join("LEFT", "label_task", "label_task.label_id = labels.id").
Where("label_task.label_id is not null OR labels.created_by_id = ?", a.GetID()).
2018-12-31 02:18:41 +01:00
Or(builder.In("label_task.task_id", taskIDs)).
And("labels.id = ?", l.ID).
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}
_, maxRight, err = t.CanRead(a)
if err != nil {
return
}
}
return
2018-12-31 02:18:41 +01:00
}