<p>I assume you have basic knowledge in creating plugins, if not I suggest visiting the docs.</p>

<p>It's actually pretty easy to achieve your goal.</p>

<ol>
<li><p>Create a Plugin and store an array of permission's in it's settings</p>

<pre><code>&lt;!-- first permission --&gt;
&lt;input class="text" type="text" name="permissions[]" value="" autofocus="" autocomplete="off"&gt;

&lt;!-- 2nd permission --&gt;
&lt;input class="text" type="text" name="permissions[]" value="" autofocus="" autocomplete="off"&gt;

&lt;!-- 3rd permission --&gt;
&lt;input class="text" type="text" name="permissions[]" value="" autofocus="" autocomplete="off"&gt;

&lt;!-- 4th permission --&gt;
&lt;input class="text" type="text" name="permissions[]" value="" autofocus="" autocomplete="off"&gt;

&lt;!-- just include some javascript logic to add fields if needed to create them dynamically --&gt;
</code></pre></li>
<li><p>Register those permission from your plugin</p>

<pre><code>public function registerUserPermissions()
{
    $permissions = array();
    foreach($this-&gt;getSettings()['permissions'] as $rule){
        $permissions[$rule] = array('label' =&gt; $rule);
    }

    return $permissions;
}
</code></pre></li>
<li><p>Create a custom fieldType that displays all usergroups dynamically or (for testing) create a simple dropdown field that stores user groups
<a href="https://i.sstatic.net/viHNH.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/viHNH.png" alt="enter image description here"></a></p></li>
<li><p>Add this field to a matrix element -> you can store a required group for each element</p></li>
<li><p>Go to your usergroups and add the permissions to those groups then you can check if the currentuser is in the group you selected in your dropdown</p></li>
<li><p>After you understood the process you can create a custom field type to store/link usergroups (so you can store multiple and select them based on all existing usergroups dynamically)</p></li>
<li><p>Make your plugin settings more complex.. in my example you can only store the handle not the label of your permission so if you type "Permission to do that" instead of "permissionToDoSomething" it will break</p></li>
</ol>

CC BY-SA 3.0
2017-10-24T09:21:59.370Z
Nice answer Robin, I just wanted to leave a link to a existing plugin which provides a usergroups field type (pt 6), and point out that instead of registering user permissions, the OP could as well simply handle permissions on a per entry basis `{% if currentUser.groups|intersect(myentry.myusergroupfield) %}`.
carlcs
CC BY-SA 3.0
2017-10-24T09:22:10.467Z
https://github.com/quebecstudio/usergroupfield
carlcs
CC BY-SA 3.0
2017-10-25T09:34:26.690Z
That's awesome. Thank you both. I'm looking forward to trying those suggestions out.
Jonathan Schofield
CC BY-SA 3.0
2017-10-24T06:50:38.090Z
2017-10-24T06:50:38.090Z
Robin Schambach
<p>I’d like be able to restrict access to the content of entries, served by a common template, subject to the <strong>user group</strong> of the logged in user.</p>

<p>For example:</p>

<ul>
<li>There is a template T</li>
<li>User U is a member of group G</li>
<li>Users V and W are members of group H</li>
<li>Entry A, served by template T, is accessible by group G, and therefore only by user U</li>
<li>Entry B, also served by template T, is accessible by group H, and therefore only by users V and W</li>
</ul>

<p>Is this possible with Craft out of the box? Or does it require custom plugins?</p>

<p>I’ve done this kind of thing in ExpressionEngine using its ZooVisitor add-on, but this is my first foray into user restricted access to content with Craft.</p>

<p>If a sticking point is the single template, could it be done with a template <em>per user group</em>?</p>

<p>I'd be grateful if someone could point me in the right direction, as I can’t quite see the wood for the trees on this scope in the Docs.</p>

<hr>

<p><strong>Update:</strong> </p>

<p>I infer from <a href="https://craftcms.com/docs/templating/usermodel" rel="nofollow noreferrer">https://craftcms.com/docs/templating/usermodel</a> I can…</p>

<pre><code>{% if currentUser.isInGroup(group) %} {# do something… #} {% endif %}
</code></pre>

<p>So I guess I just need a way to feed entry specific stuff into the conditional. </p>

<p>Or more ideally, dynamically write the conditional as an outcome of creating a new entry that has user group requirements assigned to it. Though that sounds a bit tricky.</p>

<templating><user-groups>
User restricted access to content via a common template
# User restricted access to content via a common template

(Asked by: username_0 on 2017-10-23)

I’d like be able to restrict access to the content of entries, served by a common template, subject to the user group of the logged in user.
For example:

There is a template T
User U is a member of group G
Users V and W are members of group H
Entry A, served by template T, is accessible by group G, and therefore only by user U
Entry B, also served by template T, is accessible by group H, and therefore only by users V and W

Is this possible with Craft out of the box? Or does it require custom plugins?
I’ve done this kind of thing in ExpressionEngine using its ZooVisitor add-on, but this is my first foray into user restricted access to content with Craft.
If a sticking point is the single template, could it be done with a template per user group?
I'd be grateful if someone could point me in the right direction, as I can’t quite see the wood for the trees on this scope in the Docs.

Update: 
I infer from https://craftcms.com/docs/templating/usermodel I can…
{% if currentUser.isInGroup(group) %} {# do something… #} {% endif %}

So I guess I just need a way to feed entry specific stuff into the conditional. 
Or more ideally, dynamically write the conditional as an outcome of creating a new entry that has user group requirements assigned to it. Though that sounds a bit tricky.

**Tags:** templating, user-groups


---

## 1 answers

### Answer by username_1 on 2017-10-24. Score: 3

I assume you have basic knowledge in creating plugins, if not I suggest visiting the docs.
It's actually pretty easy to achieve your goal.

Create a Plugin and store an array of permission's in it's settings














Register those permission from your plugin
public function registerUserPermissions()
{
    $permissions = array();
    foreach($this->getSettings()['permissions'] as $rule){
        $permissions[$rule] = array('label' => $rule);
    }

    return $permissions;
}

Create a custom fieldType that displays all usergroups dynamically or (for testing) create a simple dropdown field that stores user groups

Add this field to a matrix element -> you can store a required group for each element
Go to your usergroups and add the permissions to those groups then you can check if the currentuser is in the group you selected in your dropdown
After you understood the process you can create a custom field type to store/link usergroups (so you can store multiple and select them based on all existing usergroups dynamically)
Make your plugin settings more complex.. in my example you can only store the handle not the label of your permission so if you type "Permission to do that" instead of "permissionToDoSomething" it will break

> **Comments:**
> **username_2**: Nice answer Robin, I just wanted to leave a link to a existing plugin which provides a usergroups field type (pt 6), and point out that instead of registering user permissions, the OP could as well simply handle permissions on a per entry basis `{% if currentUser.groups|intersect(myentry.myusergroupfield) %}`.  
> **username_2**: https://github.com/quebecstudio/usergroupfield  
> **username_0**: That's awesome. Thank you both. I'm looking forward to trying those suggestions out.  

---

