4

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?

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) %}
    <ul>
    {% for entry in entries %}
        <li><a href={{ entry.url }}>{{ entry.title }}</a></li>
    {% endfor %}
    </ul>
{% 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.

  • 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 Dec 18 '14 at 22:19
  • Found another error. 'currentUserGroupIds|merge(group.id)' should be an array 'currentUserGroupIds|merge([group.id])'. – Douglas McDonald Dec 18 '14 at 22:35
  • You're a machine, Douglas. – Brad Bell Dec 19 '14 at 6:54
2

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.

  • That's a great solution. Much simpler. – Douglas McDonald Jan 5 '15 at 17:18

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.