From 9fb49b339d671b76f7196cf7afae238caa074a2c Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Thu, 5 Sep 2024 15:47:28 +0300 Subject: [PATCH] prevent non empty category removal --- .../Espo/Resources/i18n/en_US/Global.json | 4 ++- application/Espo/Services/RecordTree.php | 27 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/application/Espo/Resources/i18n/en_US/Global.json b/application/Espo/Resources/i18n/en_US/Global.json index fc3a28a3fe..0d7b66bee5 100644 --- a/application/Espo/Resources/i18n/en_US/Global.json +++ b/application/Espo/Resources/i18n/en_US/Global.json @@ -415,7 +415,9 @@ "select2OrMoreRecords": "Select 2 or more records", "selectNotMoreThanNumberRecords": "Select not more than {number} records", "selectAtLeastOneRecord": "Select at least one record", - "duplicateConflict": "A record already exists." + "duplicateConflict": "A record already exists.", + "cannotRemoveCategoryWithChildCategory": "Cannot remove a category that has a child category.", + "cannotRemoveNotEmptyCategory": "Cannot remove a non-empty category." }, "boolFilters": { "onlyMy": "Only My", diff --git a/application/Espo/Services/RecordTree.php b/application/Espo/Services/RecordTree.php index 0049bb37da..3f21042112 100644 --- a/application/Espo/Services/RecordTree.php +++ b/application/Espo/Services/RecordTree.php @@ -313,6 +313,33 @@ class RecordTree extends Record } } + /** + * @throws Forbidden + * @throws BadRequest + */ + protected function beforeDeleteEntity(Entity $entity) + { + parent::beforeDeleteEntity($entity); + + $childCategory = $this->entityManager + ->getRelation($entity, 'children') + ->findOne(); + + if ($childCategory) { + throw Forbidden::createWithBody( + 'cannotRemoveCategoryWithChildCategory', + Error\Body::create()->withMessageTranslation('cannotRemoveCategoryWithChildCategory') + ); + } + + if (!$this->checkItemIsEmpty($entity)) { + throw Forbidden::createWithBody( + 'cannotRemoveNotEmptyCategory', + Error\Body::create()->withMessageTranslation('cannotRemoveNotEmptyCategory') + ); + } + } + public function update(string $id, stdClass $data, UpdateParams $params): Entity { if (!empty($data->parentId) && $data->parentId === $id) {