<p>I decided to try a different approach.</p>

<p>I made two dropdown fields "studentlevel", one for the user where the admin can set the level from 1 to 12 and one in a matrix block with the video's, also from 1 to 12.</p>

<p>I've set a conditional:</p>

<pre><code>{% for block in entry.videos %}
   {% if currentUser.studentlevel &lt;= block.studentlevel %} 
      video here
   {% endif %}
{% endfor %}
</code></pre>

<p>Since the studentgroup has no access to the CP, they can't change it and they'll only see the video up to their skill level.</p>

CC BY-SA 3.0
2015-01-05T17:18:55.407Z
That's a great solution. Much simpler.
Douglas McDonald
CC BY-SA 3.0
2014-12-23T14:42:08.340Z
2014-12-23T14:42:08.340Z
noregt
<p>As far as I know there is no way to assign a 'group' using an entries <code>fieldType</code>. What I would likely do is create a category group called 'User Groups' (userGroups) that mirror my groups, with fields for 'title' and 'groupId'.</p>

<ul>
<li>Group Title 1 -> 1,</li>
<li>Group Title 2 -> 2,</li>
<li>Group Title 3 -> 3, </li>
<li>etc.</li>
</ul>

<p>(Unfortunately, I think the only way to get the groupId is to look in the database <code>craft_usergroups</code> table. Although the code below could probably be adapted to use the group handle if needed.)</p>

<p>Then create a categories fieldType called 'authorizedGroups', and add it to the video channel's <code>fieldLayout</code> (or wherever else you need it). The client would then be able to select groups via the <code>authorizedUserGroups</code> field that have authorization to the video.</p>

<p>In your template(s) you can then check for videos related to the <code>authorizedGroups</code> that match the <code>currentUser.group</code> and display content accordingly.</p>

<p>First, on your videos index page you would only want to only show links to the videos that the user has authorization to view.</p>

<pre><code>{% if currentUser %}
    {# create an array of the currentUser.groups Ids #}
    {% set currentUserGroupIds = [] %}
    {% for group in currentUser.groups %}
        {% set currentUserGroupIds = currentUserGroupIds|merge([group.id]) %}
    {% endfor %}

    {# find all userGroupCategories that match currentUserGroupIds #}
    {% set userGroupCategories = craft.categories.group('userGroups').find({
        groupId: currentUserGroupIds
    }) %}

    {# find all the videos related to userGroupCategories #}
    {% set entries = craft.entries.section('videos').relatedTo(userGroupCategories) %}
    &lt;ul&gt;
    {% for entry in entries %}
        &lt;li&gt;&lt;a href={{ entry.url }}&gt;{{ entry.title }}&lt;/a&gt;&lt;/li&gt;
    {% endfor %}
    &lt;/ul&gt;
{% endif %}
</code></pre>

<p>Then on the video detail page you would use a conditional that redirects any unauthorized users to a 401 page (or to any other page that you choose).</p>

<pre><code>{% set userIsAuthorized = false %}
{% for group in entry.authorizedUserGroups %}
    {% if currentUser.isInGroup( group.groupId ) %}
        {% set userIsAuthorized == true %}
    {% endif %}
{% endfor %}

{% if not userIsAuthorized %}
    {% redirect 401 %}
{% endif %}

{# display video #}
...
</code></pre>

<p><strong>Update</strong>: There was a significant code error that did not account for the fact that user.groups returns an array of groups rather than a single group. I rewrote the code to handle this. Untested, and probably still contains errors, but should be closer at least.</p>

CC BY-SA 3.0
2014-12-18T22:19:57.373Z
There was a major code error. `curentUser.groups` returns an array of groups, rather than a single group. I have updated the code to (hopefully) deal with multiple groups. Let me know how it goes.
Douglas McDonald
CC BY-SA 3.0
2014-12-18T22:35:09.137Z
Found another error. 'currentUserGroupIds|merge(group.id)' should be an array 'currentUserGroupIds|merge([group.id])'.
Douglas McDonald
CC BY-SA 3.0
2014-12-19T06:54:12.543Z
You're a machine, Douglas.
Brad Bell
CC BY-SA 3.0
2014-12-18T20:50:33.373Z
2014-12-18T22:29:23.423Z
2014-12-18T22:29:23.423Z
Douglas McDonald
<p>I'm working on a website for a school. Each group has a specific page that has video's for that group only. Members of that group do not need access to the CP. First I noticed that I can only assign specific members to an entry, using the Users field. I'm not able to assign a group at once.</p>

<p>How can I make it possible for the teacher to assign a group to a video through the CP (not the code), and that only that group can view the page after login?</p>

<users><permissions>
Assign a user group to a structure entry. Restrict permission front end page to that same group
# Assign a user group to a structure entry. Restrict permission front end page to that same group

(Asked by: username_0 on 2014-12-18)

I'm working on a website for a school. Each group has a specific page that has video's for that group only. Members of that group do not need access to the CP. First I noticed that I can only assign specific members to an entry, using the Users field. I'm not able to assign a group at once.

How can I make it possible for the teacher to assign a group to a video through the CP (not the code), and that only that group can view the page after login?

**Tags:** users, permissions

---

## 2 answers

### Answer by username_0 on 2014-12-23. Score: 2 | Accepted answer

I decided to try a different approach.
I made two dropdown fields "studentlevel", one for the user where the admin can set the level from 1 to 12 and one in a matrix block with the video's, also from 1 to 12.
I've set a conditional:
{% for block in entry.videos %}
   {% if currentUser.studentlevel <= block.studentlevel %} 
      video here
   {% endif %}
{% endfor %}

Since the studentgroup has no access to the CP, they can't change it and they'll only see the video up to their skill level.

> **Comments:**
> **username_1**: That's a great solution. Much simpler.  

---

### Answer by username_1 on 2014-12-18. Score: 3

As far as I know there is no way to assign a 'group' using an entries fieldType. What I would likely do is create a category group called 'User Groups' (userGroups) that mirror my groups, with fields for 'title' and 'groupId'.

Group Title 1 -> 1,
Group Title 2 -> 2,
Group Title 3 -> 3, 
etc.

(Unfortunately, I think the only way to get the groupId is to look in the database craft_usergroups table. Although the code below could probably be adapted to use the group handle if needed.)
Then create a categories fieldType called 'authorizedGroups', and add it to the video channel's fieldLayout (or wherever else you need it). The client would then be able to select groups via the authorizedUserGroups field that have authorization to the video.
In your template(s) you can then check for videos related to the authorizedGroups that match the currentUser.group and display content accordingly.
First, on your videos index page you would only want to only show links to the videos that the user has authorization to view.
{% if currentUser %}
    {# create an array of the currentUser.groups Ids #}
    {% set currentUserGroupIds = [] %}
    {% for group in currentUser.groups %}
        {% set currentUserGroupIds = currentUserGroupIds|merge([group.id]) %}
    {% endfor %}

    {# find all userGroupCategories that match currentUserGroupIds #}
    {% set userGroupCategories = craft.categories.group('userGroups').find({
        groupId: currentUserGroupIds
    }) %}

    {# find all the videos related to userGroupCategories #}
    {% set entries = craft.entries.section('videos').relatedTo(userGroupCategories) %}
    
    {% for entry in entries %}
        {{ entry.title }}
    {% endfor %}
    
{% endif %}

Then on the video detail page you would use a conditional that redirects any unauthorized users to a 401 page (or to any other page that you choose).
{% set userIsAuthorized = false %}
{% for group in entry.authorizedUserGroups %}
    {% if currentUser.isInGroup( group.groupId ) %}
        {% set userIsAuthorized == true %}
    {% endif %}
{% endfor %}

{% if not userIsAuthorized %}
    {% redirect 401 %}
{% endif %}

{# display video #}
...

Update: There was a significant code error that did not account for the fact that user.groups returns an array of groups rather than a single group. I rewrote the code to handle this. Untested, and probably still contains errors, but should be closer at least.

> **Comments:**
> **username_1**: There was a major code error. `curentUser.groups` returns an array of groups, rather than a single group. I have updated the code to (hopefully) deal with multiple groups. Let me know how it goes.  
> **username_1**: Found another error. 'currentUserGroupIds|merge(group.id)' should be an array 'currentUserGroupIds|merge([group.id])'.  
> **username_2**: You're a machine, Douglas.  

---

