Check permissions in templates

django-authority provides a couple of template tags which allows you to get permissions for a user (and a related object).

ifhasperm

This function checks whether a permission is True or False for a user and (optional) a related object.

Syntax:

{% ifhasperm [permission_label].[check_name] [user] [*objs] %}
    lalala
{% else %}
    meh
{% endifhasperm %}

Example:

{% ifhasperm "poll_permission.change_poll" request.user %}
    lalala
{% else %}
    meh
{% endifhasperm %}

get_permissions

Retrieves all permissions associated with the given obj and user and assigns the result to a context variable.

Syntax and example:

{% get_permissions obj %}
{% for perm in permissions %}
    {{ perm }}
{% endfor %}

{% get_permissions obj as "my_permissions" %}
{% get_permissions obj for request.user as "my_permissions" %}

get_permission

Performs a permission check with the given signature, user and objects and assigns the result to a context variable.

Syntax:

{% get_permission [permission_label].[check_name] for [user] and [objs] as [varname] %}

Example:

{% get_permission "poll_permission.change_poll" for request.user and poll as "is_allowed" %}
{% get_permission "poll_permission.change_poll" for request.user and poll,second_poll as "is_allowed" %}

{% if is_allowed %}
    I've got ze power to change ze pollllllzzz. Muahahaa.
{% else %}
    Meh. No power for meeeee.
{% endif %}