feat: add proper checks and errors to see if an attachment belongs to the task it's being used as cover image in

This commit is contained in:
kolaente 2022-10-02 13:56:37 +02:00
parent e113fe34d0
commit 631a265d2d
Signed by untrusted user: konrad
GPG Key ID: F40E70337AB24C9B
3 changed files with 51 additions and 5 deletions

View File

@ -82,10 +82,11 @@ This document describes the different errors Vikunja can return.
| 4013 | 400 | The task sort param is invalid. |
| 4014 | 400 | The task sort order is invalid. |
| 4015 | 404 | The task comment does not exist. |
| 4016 | 403 | Invalid task field. |
| 4017 | 403 | Invalid task filter comparator. |
| 4018 | 403 | Invalid task filter concatinator. |
| 4019 | 403 | Invalid task filter value. |
| 4016 | 400 | Invalid task field. |
| 4017 | 400 | Invalid task filter comparator. |
| 4018 | 400 | Invalid task filter concatinator. |
| 4019 | 400 | Invalid task filter value. |
| 4020 | 400 | The provided attachment does not belong to that task. |
## Namespace

View File

@ -819,6 +819,34 @@ func (err ErrInvalidTaskFilterValue) HTTPError() web.HTTPError {
}
}
// ErrAttachmentDoesNotBelongToTask represents an error where the provided task cover attachment does not belong to the same task
type ErrAttachmentDoesNotBelongToTask struct {
TaskID int64
AttachmentID int64
}
// IsErrAttachmentAndCoverMustBelongToTheSameTask checks if an error is ErrAttachmentDoesNotBelongToTask.
func IsErrAttachmentAndCoverMustBelongToTheSameTask(err error) bool {
_, ok := err.(ErrAttachmentDoesNotBelongToTask)
return ok
}
func (err ErrAttachmentDoesNotBelongToTask) Error() string {
return fmt.Sprintf("Task attachment and cover image do not belong to the same task [TaskID: %d, AttachmentID: %d]", err.TaskID, err.AttachmentID)
}
// ErrCodeAttachmentDoesNotBelongToTask holds the unique world-error code of this error
const ErrCodeAttachmentDoesNotBelongToTask = 4020
// HTTPError holds the http error description
func (err ErrAttachmentDoesNotBelongToTask) HTTPError() web.HTTPError {
return web.HTTPError{
HTTPCode: http.StatusBadRequest,
Code: ErrCodeAttachmentDoesNotBelongToTask,
Message: "This attachment does not belong to that task.",
}
}
// =================
// Namespace errors
// =================

View File

@ -1042,7 +1042,7 @@ func (t *Task) Update(s *xorm.Session, a web.Auth) (err error) {
"position",
"repeat_mode",
"kanban_position",
"cover_image_attachment_id", // TODO: check if the attachment belongs to the task
"cover_image_attachment_id",
}
// If the task is being moved between lists, make sure to move the bucket + index as well
@ -1057,6 +1057,23 @@ func (t *Task) Update(s *xorm.Session, a web.Auth) (err error) {
colsToUpdate = append(colsToUpdate, "index")
}
// If a task attachment is being set as cover image, check if the attachment actually belongs to the task
if t.CoverImageAttachmentID != 0 {
is, err := s.Exist(&TaskAttachment{
TaskID: t.ID,
ID: t.CoverImageAttachmentID,
})
if err != nil {
return err
}
if !is {
return &ErrAttachmentDoesNotBelongToTask{
AttachmentID: t.CoverImageAttachmentID,
TaskID: t.ID,
}
}
}
wasFavorite, err := isFavorite(s, t.ID, a, FavoriteKindTask)
if err != nil {
return