Skip to content

Commit

Permalink
Merge pull request #3831 from craftcms/feature/pt-2355-4x-duplicate-a…
Browse files Browse the repository at this point in the history
…ction-for-edit-product-does-check-permission

Fixed #3819 product duplication not checking permissions
  • Loading branch information
nfourtythree authored Dec 30, 2024
2 parents 1b40aeb + 6723a9d commit 35e765d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release Notes for Craft Commerce

## Unreleased

- Fixed a bug where Edit Product pages would allow duplication for users that didn’t have permission to duplicate the product. ([#3819](https://github.com/craftcms/commerce/issues/3819))

## 4.7.2 - 2024-12-18

- Fixed a bug where the Edit Order page wasn’t showing order errors.
Expand Down
5 changes: 5 additions & 0 deletions src/controllers/ProductsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,11 @@ public function actionSaveProduct(bool $duplicate = false): ?Response
*/
public function actionDuplicateProduct(): ?Response
{
$product = ProductHelper::productFromPost($this->request);
if (!Craft::$app->getElements()->canDuplicate($product)) {
throw new ForbiddenHttpException('User is not permitted to duplicate this product');
}

return $this->runAction('save-product', ['duplicate' => true]);
}

Expand Down
12 changes: 7 additions & 5 deletions src/templates/products/_edit.twig
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@
}
] %}
{% if product.id %}
{% set formActions = formActions|push({
label: 'Save as a new {type}'|t('app', { type: product.lowerDisplayName() }),
redirect: '{cpEditUrl}'|hash,
action: 'commerce/products/duplicate-product'
}) %}
{% if canDuplicate(product) %}
{% set formActions = formActions|push({
label: 'Save as a new {type}'|t('app', { type: product.lowerDisplayName() }),
redirect: '{cpEditUrl}'|hash,
action: 'commerce/products/duplicate-product'
}) %}
{% endif %}
{% if canDelete(product) %}
{% set formActions = formActions|push({
action: 'commerce/products/delete-product',
Expand Down

0 comments on commit 35e765d

Please sign in to comment.