I would like to protect the viewing of an _entry.html page so that only a user related to that entry can view it.


You could loop through the users and compare them.

{% set authorized = false %}
{% for user in entry.authorizedUsers %}
    {% if user == currentUser %}
        {% set authorized = true %}
    {% endif %}
{% endfor %}

{% if not authorized %}
    {% redirect 403 %}
{% endif %}
  • Ahha! I didn't know about redirect tag, will give that a shot, thanks – GlennJ Jan 10 '15 at 23:39
  • Glad to help. Let me know if that first option works. I'm curious myself. Also, you can redirect anywhere with the redirect method (I just chose 403 as an example), or display alternate content if you like. You may also have to check if there is a currentUser at all, using {% if currentUser %}... {% endif %} – Douglas McDonald Jan 10 '15 at 23:52
  • Pretty sure Twig doesn't have an in_array. It does have an in, so you could do {% if not currentUser in entry.authorizedUsers %}, but I doubt that will work when comparing whole objects to each other. Would probably need to just do the comparison on authorId, but honestly your 2nd solution seems better. – Brad Bell Jan 11 '15 at 0:20
  • 1
    @Brad.... thanks. I was looking at a post on the twig stack exchange and misread it. As you say, still may not work. – Douglas McDonald Jan 11 '15 at 18:56

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.