fix(projects): don't allow making a project child of itself

This commit is contained in:
kolaente 2023-03-27 12:56:01 +02:00
parent aafcb0bac4
commit b482664d82
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
3 changed files with 35 additions and 1 deletions

View File

@ -63,6 +63,7 @@ This document describes the different errors Vikunja can return.
| 3007 | 400 | A project with this identifier already exists. |
| 3008 | 412 | The project is archived and can therefore only be accessed read only. This is also true for all tasks associated with this project. |
| 3009 | 412 | The project cannot belong to a dynamically generated namespace like "Favorites". |
| 3010 | 412 | This project cannot be a child of itself.|
## Task

View File

@ -283,8 +283,35 @@ func (err *ErrProjectCannotBelongToAPseudoParentProject) HTTPError() web.HTTPErr
}
}
// ErrProjectCannotBeChildOfItself represents an error where a project cannot become a child of its own
type ErrProjectCannotBeChildOfItself struct {
ProjectID int64
}
// IsErrProjectCannotBeChildOfItsOwn checks if an error is a project is archived error.
func IsErrProjectCannotBeChildOfItsOwn(err error) bool {
_, ok := err.(*ErrProjectCannotBeChildOfItself)
return ok
}
func (err *ErrProjectCannotBeChildOfItself) Error() string {
return fmt.Sprintf("Project cannot be made a child of itself [ProjectID: %d]", err.ProjectID)
}
// ErrCodeProjectCannotBeChildOfItself holds the unique world-error code of this error
const ErrCodeProjectCannotBeChildOfItself = 3010
// HTTPError holds the http error description
func (err *ErrProjectCannotBeChildOfItself) HTTPError() web.HTTPError {
return web.HTTPError{
HTTPCode: http.StatusPreconditionFailed,
Code: ErrCodeProjectCannotBeChildOfItself,
Message: "This project cannot be a child of itself.",
}
}
// ==============
// Project errors
// Task errors
// ==============
// ErrTaskCannotBeEmpty represents a "ErrProjectDoesNotExist" kind of error. Used if the project does not exist.

View File

@ -641,6 +641,12 @@ func checkProjectBeforeUpdateOrDelete(s *xorm.Session, project *Project) error {
// Check if the parent project exists
if project.ParentProjectID > 0 {
if project.ParentProjectID == project.ID {
return &ErrProjectCannotBeChildOfItself{
ProjectID: project.ID,
}
}
_, err := GetProjectSimpleByID(s, project.ParentProjectID)
if err != nil {
return err