3

In my plugin, I am checking to see if the current logged in user has permission to save a commerce product.

$user = craft()->userSession->getUser();
if (craft()->userPermissions->doesUserHavePermission($user->id, 'ManageClothingProducts')) {
    // yes permission
} else {
  // no permission
};

However, it seems I'm always getting into the else.

The commerce docs mention this:

Manage [type] products

Maybe I'm not using the correct syntax? I'm only using the clothing products as an example. Ultimately I'll be checking for my own products.

Thank you for any suggestions!

5

The correct permission key is:

commerce-manageProductType:ID_OF_PRODUCT_TYPE_HERE

for example:

commerce-manageProductType:2

Your code would therefore be:

$user = craft()->userSession->getUser();
if (craft()->userPermissions->doesUserHavePermission($user->id, 'commerce-manageProductType:2')) {
    // yes permission
} else {
  // no permission
};

Or you could use UserSessionService::checkPermission(), which is a shortcut for checking permissions for the currently logged-in user:

if (craft()->userSession->checkPermission('commerce-manageProductType:2')
1
  • Thanks Luke & Brandon! It's interesting though, when I am logged in as the site admin, this works: craft()->userSession->checkPermission('commerce-manageProductType:1'). However, this says I do not have permission craft()->userPermissions->doesUserHavePermission($user->id, 'commerce-manageProductType:1')
    – Damon
    Aug 29 '16 at 14:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.