Answers.Body: <p>The correct permission key is:</p>
  
  <p><code>commerce-manageProductType:ID_OF_PRODUCT_TYPE_HERE</code></p>
  
  <p>for example:</p>
  
  <p><code>commerce-manageProductType:2</code></p>
  
  <p>Your code would therefore be:</p>
  
  
  
  <pre class="lang-php prettyprint-override"><code>$user = craft()-&gt;userSession-&gt;getUser();
  if (craft()-&gt;userPermissions-&gt;doesUserHavePermission($user-&gt;id, 'commerce-manageProductType:2')) {
      // yes permission
  } else {
    // no permission
  };
  </code></pre>
  
  <p>Or you could use <a href="https://craftcms.com/classreference/services/UserSessionService#checkPermission-detail" rel="nofollow">UserSessionService::checkPermission()</a>, which is a shortcut for checking permissions for the currently logged-in user:</p>
  
  <pre class="lang-php prettyprint-override"><code>if (craft()-&gt;userSession-&gt;checkPermission('commerce-manageProductType:2')
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-08-29T14:45:15.107Z
Answers.Text: 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')`
Answers.UserDisplayName: Damon
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-08-29T05:02:08.800Z
Answers.LastActivityDate: 2016-08-29T14:40:17.550Z
Answers.LastEditDate: 2016-08-29T14:40:17.550Z
Answers.UserDisplayName: Luke Holder
Body: <p>In my plugin, I am checking to see if the current logged in user has permission to save a commerce product.</p>
  
  <pre><code>$user = craft()-&gt;userSession-&gt;getUser();
  if (craft()-&gt;userPermissions-&gt;doesUserHavePermission($user-&gt;id, 'ManageClothingProducts')) {
      // yes permission
  } else {
    // no permission
  };
  </code></pre>
  
  <p>However, it seems I'm always getting into the <code>else</code>.</p>
  
  <p>The commerce docs mention this:</p>
  
  <blockquote>
    <p>Manage [type] products</p>
  </blockquote>
  
  <p>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.</p>
  
  <p>Thank you for any suggestions!</p>
  
Tags: <plugin-development><plugin-craftcommerce>
Title: What is the correct syntax to check if a user has permission?
ThreadText: # What is the correct syntax to check if a user has permission?
  
  (Asked by: username_0 on 2016-08-29)
  
  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!
  
  **Tags:** plugin-development, plugin-craftcommerce
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-08-29. Score: 5 | Accepted answer
  
  The correct permission key is:
  
  `commerce-manageProductType:ID_OF_PRODUCT_TYPE_HERE`
  
  for example:
  
  `commerce-manageProductType:2`
  
  Your code would therefore be:
  
  ```php
  $user = craft()->userSession->getUser();
  if (craft()->userPermissions->doesUserHavePermission($user->id, 'commerce-manageProductType:2')) {
      // yes permission
  } else {
    // no permission
  };
  
  ```
  
  Or you could use [UserSessionService::checkPermission()](https://craftcms.com/classreference/services/UserSessionService#checkPermission-detail), which is a shortcut for checking permissions for the currently logged-in user:
  
  ```php
  if (craft()->userSession->checkPermission('commerce-manageProductType:2')
  
  ```
  
  > **Comments:**
  > **username_0**: Thanks username_1 & 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')`  
  
  ---
  
  