1

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

1 Answer 1

2

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 %}
4
  • Ahha! I didn't know about redirect tag, will give that a shot, thanks
    – GlennJ
    Commented Jan 10, 2015 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 %} Commented Jan 10, 2015 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
    Commented Jan 11, 2015 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. Commented Jan 11, 2015 at 18:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.