1

I need to use requirePermission tag to check if one or another permission is active.

I want to reproduce something like this:

{% if currentUser.can('perm1') or currentUser.can('perm2')  %}
   <!-- My page stuff -->
{% else %}
   {% exit 403 %}
{% endif %}

How can I make this possible {% requirePermission "perm1_or_perm2" %} ?

1
  • Why do you need to use requirePermission and can't just do it using can?
    – carlcs
    Apr 16 '15 at 13:11
4

The {% requirePermission 'perm1' %} tag only supports a single permission. Behind the scenes it's effectively the same as:

{% if not currentUser.can('perm1') %}
    {% exit 403 %}
{% endif %}

So I think either what you already have, or this:

{% if not currentUser.can('perm1') and not currentUser.can('perm2') %}
    {% exit 403 %}
{% endif %}

<!-- Your page stuff -->

Is probably your best bet at the moment. Checking that the user doesn't have both permissions, is the same as checking they do have one or the other.

3

{% requirePermission %} can only take on parameter. To make your code a little easier to manage, you might want to check for false in your conditional and exit. That way you wont have to worry about closing the if statement later in your template code. You could even stick the code below in a partial _checkPermissions.html template to include on your pages that require it.

{% if currentUser %}
    {% if not currentUser.can('perm1') and not currentUser.can('perm2')  %}
       {% exit 403 %}
    {% endif %}
{% else  %}
    {% exit 403 %}
{% endif %}

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.