2

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.

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.

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

    <!-- first permission -->
    <input class="text" type="text" name="permissions[]" value="" autofocus="" autocomplete="off">
    
    <!-- 2nd permission -->
    <input class="text" type="text" name="permissions[]" value="" autofocus="" autocomplete="off">
    
    <!-- 3rd permission -->
    <input class="text" type="text" name="permissions[]" value="" autofocus="" autocomplete="off">
    
    <!-- 4th permission -->
    <input class="text" type="text" name="permissions[]" value="" autofocus="" autocomplete="off">
    
    <!-- just include some javascript logic to add fields if needed to create them dynamically -->
    
  2. Register those permission from your plugin

    public function registerUserPermissions()
    {
        $permissions = array();
        foreach($this->getSettings()['permissions'] as $rule){
            $permissions[$rule] = array('label' => $rule);
        }
    
        return $permissions;
    }
    
  3. Create a custom fieldType that displays all usergroups dynamically or (for testing) create a simple dropdown field that stores user groups enter image description here

  4. Add this field to a matrix element -> you can store a required group for each element

  5. 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

  6. 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)

  7. 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

  • 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 Oct 24 '17 at 9:21
  • That's awesome. Thank you both. I'm looking forward to trying those suggestions out. – Jonathan Schofield Oct 25 '17 at 9:34

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.