TransWikia.com

Restrict entries to specific users

Craft CMS Asked on September 4, 2021

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.

One Answer

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 %}

Correct answer by Pavel Pavlovich on September 4, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP