Answers.Body: <p>If you're looking to check groups and/or permissions from your plugin controller with PHP, you can access the same methods.</p>
  
  <p><strong>Craft 3.0.x:</strong> </p>
  
  <pre class="lang-php prettyprint-override"><code>//get the current user from craft
  $user = Craft::$app-&gt;getUser();
  
  //check if the current user has permission to edit certain entries
  $user-&gt;getIdentity()-&gt;can('editEntries: 27')
  
  //check if user is in group
  $user-&gt;getIdentity()-&gt;isInGroup('specialGroupHandle'));
  </code></pre>
  
  <p><strong>Craft 3.1.x</strong></p>
  
  <p>Since Craft 3.1.x not uses ids to check permissions anymore but uids you need to pass the uid of your section</p>
  
  <pre class="lang-php prettyprint-override"><code>$user-&gt;getIdentity()-&gt;can('editEntries: 86f6afac-1d8c-43ba-8624-12d925ba57f5')
  </code></pre>
  
  <p><strong>Craft 2:</strong></p>
  
  <pre class="lang-php prettyprint-override"><code>//get the current user from craft
  $user = craft()-&gt;userSession-&gt;getUser();
  
  //check if the current user has permission to edit certain entries
  $checkPerms = $user-&gt;can('editEntries: 27');
  </code></pre>
  
  <p>$checkPerms will return a boolean</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-04-23T02:17:07.210Z
Answers.Text: awsm - thx Robin and David
Answers.UserDisplayName: bhu Boue vidya
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-05-06T21:40:31.693Z
Answers.Text: Just a note that it's probably more clear that "get the current user" should be `$user = Craft::$app->getUser()->getIdentity();` as `Craft::$app->getUser()` technically returns the User service/component (as noted by Robin here: https://craftcms.stackexchange.com/a/25917/6275).
Answers.UserDisplayName: Nate Beaty
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2015-04-08T16:43:18.307Z
Answers.LastActivityDate: 2019-02-23T18:34:31.410Z
Answers.LastEditDate: 2019-02-23T18:34:31.410Z
Answers.UserDisplayName: David Rampersad
Answers.Body: <p>I had a similar situation:<br>
  I wanted to restrict some users from certain entries. </p>
  
  <p>I've created a checkboxes field called "cf_userRights_checkboxes" where the label is the user-groups name and the value is the user-groups id. Then added that field to a section. </p>
  
  <p>In a section entry you will be able to set the user-groups that have permission and then check if the currentUser is in one of these groups:</p>
  
  <pre><code>{% set condition = 0 %}
  {% for userGroup in currentUser.getGroups %}
      {% if userGroup.id in entry.cf_userRights_checkboxes %}
          {% set condition = 1 %}
      {% endif %}
  {% endfor %}
  
  {% if condition %}
      please enter
  {% else %}
      sorry, not you
  {% endif %}
  </code></pre>
  
  <p>Note: you have to loop through user-groups because a user can be in more than one group. </p>
  
  <p>Let me know if there's a better solution.</p>
  
  <p>Cheers<br>
  Stefan</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-11-01T10:50:50.670Z
Answers.LastActivityDate: 2019-11-01T10:50:50.670Z
Answers.UserDisplayName: outline4
Answers.Body: <p>Like in answer before of Robin Schambach and David Rampersad I tried to solved it this way:</p>
  <p><code>$user-&gt;getIdentity()-&gt;can('editEntries: 86f6afac-1d8c-43ba-8624-12d925ba57f5')</code></p>
  <p>Anyway I had to remove the space between the uuid and the permission string:</p>
  <p><code>$user-&gt;getIdentity()-&gt;can('editEntries:86f6afac-1d8c-43ba-8624-12d925ba57f5')</code></p>
  <p>Inside my module I ended up with that:</p>
  <p><code>$user-&gt;checkPermission('editEntries:'.$entry-&gt;section-&gt;uid)</code></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-01-24T10:22:45.257Z
Answers.LastActivityDate: 2021-01-24T10:22:45.257Z
Answers.UserDisplayName: Slowwie
Answers.Body: <p>I believe what you're looking for is <a href="http://buildwithcraft.com/docs/templating/usermodel#isInGroup">'isInGroup'</a></p>
  
  <pre><code>{% if currentUser.isInGroup('groupHandle') %}
  </code></pre>
  
  <p>It will also accept an actual group object or a group ID as well.</p>
  
  <p>There is also <a href="http://buildwithcraft.com/docs/templating/usermodel#getGroups">getGroups</a>:</p>
  
  <pre><code>{{ currentUser.getGroups() }}
  </code></pre>
  
  <p>Will return an array of <a href="http://buildwithcraft.com/docs/templating/usergroupmodel">UserGroupModels</a> all of the groups the user belongs to.</p>
  
  <p>And finally, <a href="http://buildwithcraft.com/docs/templating/usermodel#can">can</a>:</p>
  
  <pre><code>{{ currentUser.can('permissionName') }}
  </code></pre>
  
  <p>Will check if the current user has the given permissions, whether that was indirectly assigned to them in a group, or directly assigned to them as a user.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-17T19:07:42.580Z
Answers.Text: Can isInGroup be accessed from within a plugin?
Answers.UserDisplayName: David A McInnis
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-04-25T04:29:10.370Z
Answers.Text: @DavidAaronMcInnis `craft()->userGroups->getGroupsByUserId($user->id)`
Answers.UserDisplayName: Elliot Cameron
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-04-15T11:17:45.350Z
Answers.Text: I presume this is only about viewing the actual page for the entry rather than being able to restrict edit/creation by a user group?
Answers.UserDisplayName: Matt Fletcher
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-11-06T14:38:03.260Z
Answers.Text: You can also do `currentUser.isInGroup(['groupHandle1','groupHandle2'])` for an OR, which is pretty neat.
Answers.UserDisplayName: iamkeir
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-20T00:34:23.477Z
Answers.Text: This is perfect, isInGroup works perfectly.
Answers.UserDisplayName: Darren
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-23T22:48:05.563Z
Answers.Text: Any chance that we can get something like this to work? Sometimes I just need to make sure that a user is NOT a member of a group. Right now I don't see a simple way to make this determination.
  
  {% if !currentUser.isInGroup('groupHandle') %}
Answers.UserDisplayName: David A McInnis
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-24T03:14:35.917Z
Answers.Text: @DavidAaronMcInnis Use `{% if not currentUser.isInGroup('groupHandle') %}`
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-14T21:53:32.277Z
Answers.LastActivityDate: 2014-07-14T21:53:32.277Z
Answers.UserDisplayName: Brad Bell
Body: <p>I'm trying to restrict access to items in a channel using some form of custom user permissions however I don't appear to be able to create or set custom read permissions for a channel using crafts permissions as they currently are, I'm therefore trying to do something using either user groups or custom fields on the user and validate if a user has access that way. </p>
  
  <p>I'm hoping this is along the right lines, although it doesn't seem ideal, so is it possible to check if a user is in a certain user group elegantly using twig?</p>
  
  <p>I can do <code>{{ if currentUser.admin }}</code> and I'm guessing I could therefore do something like <code>{{if 'groupnamewithaccess' in currentUser.groups }}</code></p>
  
Tags: <templating><users><current-user>
Title: Check user is in User Group / Custom User Permissions
ThreadText: # Check user is in User Group / Custom User Permissions
  
  (Asked by: username_0 on 2014-07-14)
  
  I'm trying to restrict access to items in a channel using some form of custom user permissions however I don't appear to be able to create or set custom read permissions for a channel using crafts permissions as they currently are, I'm therefore trying to do something using either user groups or custom fields on the user and validate if a user has access that way.
  
  I'm hoping this is along the right lines, although it doesn't seem ideal, so is it possible to check if a user is in a certain user group elegantly using twig?
  
  I can do `{{ if currentUser.admin }}` and I'm guessing I could therefore do something like `{{if 'groupnamewithaccess' in currentUser.groups }}`
  
  **Tags:** templating, users, current-user
  
  
  ---
  
  ## 4 answers
  
  ### Answer by username_6 on 2014-07-14. Score: 26 | Accepted answer
  
  I believe what you're looking for is ['isInGroup'](http://buildwithcraft.com/docs/templating/usermodel#isInGroup)
  
  ```
  {% if currentUser.isInGroup('groupHandle') %}
  
  ```
  
  It will also accept an actual group object or a group ID as well.
  
  There is also [getGroups](http://buildwithcraft.com/docs/templating/usermodel#getGroups):
  
  ```
  {{ currentUser.getGroups() }}
  
  ```
  
  Will return an array of [UserGroupModels](http://buildwithcraft.com/docs/templating/usergroupmodel) all of the groups the user belongs to.
  
  And finally, [can](http://buildwithcraft.com/docs/templating/usermodel#can):
  
  ```
  {{ currentUser.can('permissionName') }}
  
  ```
  
  Will check if the current user has the given permissions, whether that was indirectly assigned to them in a group, or directly assigned to them as a user.
  
  > **Comments:**
  > **username_0**: This is perfect, isInGroup works perfectly.  
  > **username_7**: Any chance that we can get something like this to work? Sometimes I just need to make sure that a user is NOT a member of a group. Right now I don't see a simple way to make this determination.  {% if !currentUser.isInGroup('groupHandle') %}  
  > **username_6**: @DavidAaronMcInnis Use `{% if not currentUser.isInGroup('groupHandle') %}`  
  > **username_7**: Can isInGroup be accessed from within a plugin?  
  > **username_8**: @DavidAaronMcInnis `craft()->userGroups->getGroupsByUserId($user->id)`  
  > **username_9**: I presume this is only about viewing the actual page for the entry rather than being able to restrict edit/creation by a user group?  
  > **username_10**: You can also do `currentUser.isInGroup(['groupHandle1','groupHandle2'])` for an OR, which is pretty neat.  
  
  ---
  
  ### Answer by username_1 on 2015-04-08. Score: 11
  
  If you're looking to check groups and/or permissions from your plugin controller with PHP, you can access the same methods.
  
  **Craft 3.0.x:**
  
  ```php
  //get the current user from craft
  $user = Craft::$app->getUser();
  
  //check if the current user has permission to edit certain entries
  $user->getIdentity()->can('editEntries: 27')
  
  //check if user is in group
  $user->getIdentity()->isInGroup('specialGroupHandle'));
  
  ```
  
  **Craft 3.1.x**
  
  Since Craft 3.1.x not uses ids to check permissions anymore but uids you need to pass the uid of your section
  
  ```php
  $user->getIdentity()->can('editEntries: 86f6afac-1d8c-43ba-8624-12d925ba57f5')
  
  ```
  
  **Craft 2:**
  
  ```php
  //get the current user from craft
  $user = craft()->userSession->getUser();
  
  //check if the current user has permission to edit certain entries
  $checkPerms = $user->can('editEntries: 27');
  
  ```
  
  $checkPerms will return a boolean
  
  > **Comments:**
  > **username_2**: awsm - thx Robin and username_1  
  > **username_3**: Just a note that it's probably more clear that "get the current user" should be `$user = Craft::$app->getUser()->getIdentity();` as `Craft::$app->getUser()` technically returns the User service/component (as noted by Robin here: https://craftcms.stackexchange.com/a/25917/6275).  
  
  ---
  
  ### Answer by username_4 on 2019-11-01. Score: 2
  
  I had a similar situation:  
  I wanted to restrict some users from certain entries.
  
  I've created a checkboxes field called "cf\_userRights\_checkboxes" where the label is the user-groups name and the value is the user-groups id. Then added that field to a section.
  
  In a section entry you will be able to set the user-groups that have permission and then check if the currentUser is in one of these groups:
  
  ```
  {% set condition = 0 %}
  {% for userGroup in currentUser.getGroups %}
      {% if userGroup.id in entry.cf_userRights_checkboxes %}
          {% set condition = 1 %}
      {% endif %}
  {% endfor %}
  
  {% if condition %}
      please enter
  {% else %}
      sorry, not you
  {% endif %}
  
  ```
  
  Note: you have to loop through user-groups because a user can be in more than one group.
  
  Let me know if there's a better solution.
  
  Cheers  
  Stefan
  
  
  ---
  
  ### Answer by username_5 on 2021-01-24. Score: 1
  
  Like in answer before of Robin Schambach and username_1 username_1 I tried to solved it this way:
  
  `$user->getIdentity()->can('editEntries: 86f6afac-1d8c-43ba-8624-12d925ba57f5')`
  
  Anyway I had to remove the space between the uuid and the permission string:
  
  `$user->getIdentity()->can('editEntries:86f6afac-1d8c-43ba-8624-12d925ba57f5')`
  
  Inside my module I ended up with that:
  
  `$user->checkPermission('editEntries:'.$entry->section->uid)`
  
  
  ---
  
  