1

I would like to restrict entries to specific users on the frontend via a User Field called userSelector. It should be possible to have more than 1 user. I understand groups might work, but I feel like it's easier to just select the user(s) directly on the entry.

I know the code below won't work, but hopefully, it shows my intentions a bit more.

{% set users = entry.userSelector.all() %}
{% if currentUser.email == users.email %}
    Show the page
{% else %}
    Permission Denied
{% endif %}

Very open to other suggestions.

3

If you are faced with such a task, then I think it's better to use user IDs, getting from the method a ids from the field in which users are stored, and check the received array using the in method. Another option is a plugin https://plugins.craftcms.com/user-group-field.

 {% set usersIds = entry.usersField.ids %} 

    {% if currentUser.id in usersIds %} 
        Show the page
    {% else %}
        Permission Denied
    {% endif %}
| improve this answer | |
  • Perfect! Exactly what I was looking for. Appreciate it. – tommysvr Jul 16 at 8:11

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.