Skip to content

Commit 7df7977

Browse files
committed
Fix permissions check for product ceate and delete
Fixes #3838
1 parent 4a60f49 commit 7df7977

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Release Notes for Craft Commerce
22

3+
## Unreleased
4+
5+
- Fixed a bug where products could be duplicated without the “Create products” permissions. ([#3838](https://github.com/craftcms/commerce/issues/3838))
6+
37
## 5.2.11 - 2025-01-02
48

59
- Fixed an error that occurred when rendering a Link field with a product selected on the front end. ([#3833](https://github.com/craftcms/commerce/issues/3833))

src/elements/Product.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -858,7 +858,7 @@ public function canDuplicate(User $user): bool
858858
return false;
859859
}
860860

861-
return $user->can('commerce-editProductType:' . $productType->uid);
861+
return Plugin::getInstance()->getProductTypes()->hasPermission($user, $productType, 'commerce-createProducts');
862862
}
863863

864864
/**
@@ -876,7 +876,7 @@ public function canDelete(User $user): bool
876876
return false;
877877
}
878878

879-
return $user->can('commerce-deleteProducts:' . $productType->uid);
879+
return Plugin::getInstance()->getProductTypes()->hasPermission($user, $productType, 'commerce-deleteProducts');
880880
}
881881

882882
/**

src/services/ProductTypes.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -994,14 +994,17 @@ public function hasPermission(User $user, ProductType $productType, ?string $che
994994

995995
$suffix = ':' . $productType->uid;
996996

997-
// Required for create and delete permission.
998-
$editProductType = strtolower('commerce-editProductType' . $suffix);
999-
1000997
if ($checkPermissionName !== null) {
1001998
$checkPermissionName = strtolower($checkPermissionName . $suffix);
999+
if (!in_array(strtolower($checkPermissionName), $permissions)) {
1000+
return false;
1001+
}
10021002
}
10031003

1004-
if (!in_array($editProductType, $permissions) || ($checkPermissionName !== null && !in_array(strtolower($checkPermissionName), $permissions))) {
1004+
// Required for create and delete permission.
1005+
$editProductType = strtolower('commerce-editProductType' . $suffix);
1006+
1007+
if (!in_array($editProductType, $permissions)) {
10051008
return false;
10061009
}
10071010

0 commit comments

Comments
 (0)