| 1 |
--- |
| 2 |
title: Checking |
| 3 |
description: How to check object-level permissions using django-guardian. |
| 4 |
--- |
| 5 |
|
| 6 |
# Check object permissions |
| 7 |
|
| 8 |
Once we have `assigned some permissions <assign>` we can get into detail |
| 9 |
about verifying permissions of a user or group. |
| 10 |
|
| 11 |
## Standard way |
| 12 |
|
| 13 |
Normally to check if Joe is permitted to change `Site` objects we call |
| 14 |
`has_perm` method on an `User` instance: |
| 15 |
|
| 16 |
```python |
| 17 |
>>> joe.has_perm('sites.change_site') |
| 18 |
False |
| 19 |
``` |
| 20 |
|
| 21 |
And for a specific `Site` instance we do the same but we pass `site` as |
| 22 |
additional argument: |
| 23 |
|
| 24 |
```python |
| 25 |
>>> site = Site.objects.get_current() |
| 26 |
>>> joe.has_perm('sites.change_site', site) |
| 27 |
False |
| 28 |
``` |
| 29 |
|
| 30 |
Let's assign permission and check again: |
| 31 |
|
| 32 |
```python |
| 33 |
>>> from guardian.shortcuts import assign_perm |
| 34 |
>>> assign_perm('sites.change_site', joe, site) |
| 35 |
<UserObjectPermission: example.com | joe | change_site> |
| 36 |
>>> joe = User.objects.get(username='joe') |
| 37 |
>>> joe.has_perm('sites.change_site', site) |
| 38 |
True |
| 39 |
``` |
| 40 |
|
| 41 |
This uses the backend we have specified at settings module (see`configuration`). |
| 42 |
More on the backend can be found at |
| 43 |
`Backend's API <guardian.backends.ObjectPermissionBackend>`. |
| 44 |
|
| 45 |
## Inside views |
| 46 |
|
| 47 |
Aside from the standard `has_perm` method, `django-guardian` provides |
| 48 |
some useful helpers for object permission checks. |
| 49 |
|
| 50 |
### get_perms |
| 51 |
|
| 52 |
To check permissions we can use a quick-and-dirty shortcut: |
| 53 |
|
| 54 |
```python |
| 55 |
>>> from guardian.shortcuts import get_perms |
| 56 |
>>> |
| 57 |
>>> joe = User.objects.get(username='joe') |
| 58 |
>>> site = Site.objects.get_current() |
| 59 |
>>> |
| 60 |
>>> 'change_site' in get_perms(joe, site) |
| 61 |
True |
| 62 |
``` |
| 63 |
|
| 64 |
It is probably better to use standard `has_perm` method. But for `Group` |
| 65 |
instances it is not as easy and `get_perms` could be handy here as it |
| 66 |
accepts both `User` and `Group` instances. If we need to do some more |
| 67 |
work, we can use lower level `ObjectPermissionChecker` class which is |
| 68 |
described in the next section. |
| 69 |
|
| 70 |
There is also `get_user_perms` to get permissions assigned directly to |
| 71 |
the user (and not inherited from its superuser status or group |
| 72 |
membership). Similarly, `get_group_perms` returns only permissions which |
| 73 |
are inferred through user's group membership. `get_user_perms` and |
| 74 |
`get_group_perms` are useful when you care what permissions user has |
| 75 |
assigned, while `has_perm` is useful when you care about user's |
| 76 |
effective permissions. |
| 77 |
|
| 78 |
### get_objects_for_user |
| 79 |
|
| 80 |
Sometimes there is a need to extract list of objects based on particular |
| 81 |
user, type of the object and provided permissions. For instance, lets |
| 82 |
say there is a `Project` model at `projects` application with custom |
| 83 |
`view_project` permission. We want to show our users projects they can |
| 84 |
actually *view*. This could be easily achieved using |
| 85 |
`get_objects_for_user`: |
| 86 |
|
| 87 |
``` python |
| 88 |
from django.shortcuts import render |
| 89 |
from django.template import RequestContext |
| 90 |
from projects.models import Project |
| 91 |
from guardian.shortcuts import get_objects_for_user |
| 92 |
|
| 93 |
def user_dashboard(request, template_name='projects/dashboard.html'): |
| 94 |
projects = get_objects_for_user(request.user, 'projects.view_project') |
| 95 |
return render(request, template_name, {'projects': projects}, |
| 96 |
RequestContext(request)) |
| 97 |
``` |
| 98 |
|
| 99 |
It is also possible to provide list of permissions rather than single |
| 100 |
string, own queryset (as `klass` argument) or control if result should |
| 101 |
be computed with (default) or without user's groups permissions. |
| 102 |
|
| 103 |
!!! note "Non-standard primary keys" |
| 104 |
|
| 105 |
`get_objects_for_user` supports models with any primary key type, including |
| 106 |
non-standard types like `TextField` or PostgreSQL-specific fields. |
| 107 |
See [Primary key types](caveats.md#primary-key-types) for details and |
| 108 |
performance considerations. |
| 109 |
|
| 110 |
!!! tip "See Also" |
| 111 |
Documentation for `get_objects_for_user` |
| 112 |
|
| 113 |
### ObjectPermissionChecker |
| 114 |
|
| 115 |
At the `core` module of `django-guardian`, there is a |
| 116 |
`guardian.core.ObjectPermissionChecker` |
| 117 |
which checks permission of user/group for specific object. It caches |
| 118 |
results so it may be used at part of codes where we check permissions |
| 119 |
more than once. |
| 120 |
|
| 121 |
Let's see it in action: |
| 122 |
|
| 123 |
```python |
| 124 |
>>> joe = User.objects.get(username='joe') |
| 125 |
>>> site = Site.objects.get_current() |
| 126 |
>>> from guardian.core import ObjectPermissionChecker |
| 127 |
>>> checker = ObjectPermissionChecker(joe) # we can pass user or group |
| 128 |
>>> checker.has_perm('change_site', site) |
| 129 |
True |
| 130 |
>>> checker.has_perm('add_site', site) # no additional query made |
| 131 |
False |
| 132 |
>>> checker.get_perms(site) |
| 133 |
[u'change_site'] |
| 134 |
``` |
| 135 |
|
| 136 |
### Using decorators |
| 137 |
|
| 138 |
Standard `permission_required` decorator doesn't allow to check for |
| 139 |
object permissions. `django-guardian` is shipped with two decorators |
| 140 |
which may be helpful for simple object permission checks but remember |
| 141 |
that those decorators hits database before decorated view is called - |
| 142 |
this means that if there is similar lookup made within a view then most |
| 143 |
probably one (or more, depending on lookups) extra database query would |
| 144 |
occur. |
| 145 |
|
| 146 |
Let's assume we pass `'group_name'` argument to our view function which |
| 147 |
returns form to edit the group. Moreover, we want to return 403 code if |
| 148 |
check fails. This can be simply achieved using |
| 149 |
`permission_required_or_403` decorator: |
| 150 |
|
| 151 |
```python |
| 152 |
>>> joe = User.objects.get(username='joe') |
| 153 |
>>> foobars = Group.objects.create(name='foobars') |
| 154 |
>>> |
| 155 |
>>> from guardian.decorators import permission_required_or_403 |
| 156 |
>>> from django.http import HttpResponse |
| 157 |
>>> |
| 158 |
>>> @permission_required_or_403('auth.change_group', |
| 159 |
>>> (Group, 'name', 'group_name')) |
| 160 |
>>> def edit_group(request, group_name): |
| 161 |
>>> return HttpResponse('some form') |
| 162 |
>>> |
| 163 |
>>> from django.http import HttpRequest |
| 164 |
>>> request = HttpRequest() |
| 165 |
>>> request.user = joe |
| 166 |
>>> edit_group(request, group_name='foobars') |
| 167 |
<django.http.HttpResponseForbidden object at 0x102b43dd0> |
| 168 |
>>> |
| 169 |
>>> joe.groups.add(foobars) |
| 170 |
>>> edit_group(request, group_name='foobars') |
| 171 |
<django.http.HttpResponseForbidden object at 0x102b43e50> |
| 172 |
>>> |
| 173 |
>>> from guardian.shortcuts import assign_perm |
| 174 |
>>> assign_perm('auth.change_group', joe, foobars) |
| 175 |
<UserObjectPermission: foobars | joe | change_group> |
| 176 |
>>> |
| 177 |
>>> edit_group(request, group_name='foobars') |
| 178 |
<django.http.HttpResponse object at 0x102b8c8d0> |
| 179 |
>>> # Note that we now get normal HttpResponse, not forbidden |
| 180 |
``` |
| 181 |
|
| 182 |
More on decorators can be read at corresponding |
| 183 |
`API page <api-decorators>`. |
| 184 |
|
| 185 |
!!! note |
| 186 |
|
| 187 |
The General idea of decorators' lookups was taken from |
| 188 |
[django-authority](https://github.com/jazzband/django-authority) and all |
| 189 |
credits go to it's creator, Jannis Leidel. |
| 190 |
|
| 191 |
## Inside templates |
| 192 |
|
| 193 |
`django-guardian` comes with special template tag |
| 194 |
`guardian.templatetags.guardian_tags.get_obj_perms` |
| 195 |
which can store object permissions for a given user/group |
| 196 |
and instance pair. To use it we need to put following inside a |
| 197 |
template: |
| 198 |
|
| 199 |
```python |
| 200 |
{% load guardian_tags %} |
| 201 |
``` |
| 202 |
|
| 203 |
### get_obj_perms |
| 204 |
|
| 205 |
```python |
| 206 |
guardian.templatetags.guardian_tags.get_obj_perms |
| 207 |
``` |