| 1 |
--- |
| 2 |
title: Removing Object Permissions |
| 3 |
description: Removing object permissions from users and objects with django-guardian. |
| 4 |
--- |
| 5 |
|
| 6 |
# Removing Object Permissions |
| 7 |
|
| 8 |
Removing object permissions works just like assigning them. The |
| 9 |
`guardian.shortcuts.remove_perm` function accepts the same arguments as |
| 10 |
`guardian.shortcuts.assign_perm`, including support for bulk operations on |
| 11 |
multiple objects or multiple users/groups. |
| 12 |
|
| 13 |
## Using shortcuts |
| 14 |
|
| 15 |
### For a single object |
| 16 |
|
| 17 |
Assuming Joe has been granted the `change_task` permission on a task: |
| 18 |
|
| 19 |
``` python |
| 20 |
>>> from guardian.shortcuts import remove_perm |
| 21 |
>>> joe.has_perm('change_task', task) |
| 22 |
True |
| 23 |
>>> remove_perm('change_task', joe, task) |
| 24 |
>>> joe = User.objects.get(username='joe') |
| 25 |
>>> joe.has_perm('change_task', task) |
| 26 |
False |
| 27 |
``` |
| 28 |
|
| 29 |
Removing a group permission works the same way — pass a `Group` instance |
| 30 |
instead of a `User`: |
| 31 |
|
| 32 |
``` python |
| 33 |
>>> from django.contrib.auth.models import Group |
| 34 |
>>> group = Group.objects.get(name='employees') |
| 35 |
>>> remove_perm('change_task', group, task) |
| 36 |
``` |
| 37 |
|
| 38 |
### For multiple objects |
| 39 |
|
| 40 |
You can remove a permission from a single user or group across many objects |
| 41 |
at once by passing a QuerySet or list as the `obj` argument: |
| 42 |
|
| 43 |
``` python |
| 44 |
>>> from guardian.shortcuts import remove_perm |
| 45 |
>>> tasks = Task.objects.filter(reported_by=boss) |
| 46 |
>>> remove_perm('change_task', joe, tasks) |
| 47 |
``` |
| 48 |
|
| 49 |
A plain list of objects works too: |
| 50 |
|
| 51 |
``` python |
| 52 |
>>> task_list = [task1, task2, task3] |
| 53 |
>>> remove_perm('change_task', joe, task_list) |
| 54 |
``` |
| 55 |
|
| 56 |
The same applies to groups: |
| 57 |
|
| 58 |
``` python |
| 59 |
>>> remove_perm('change_task', group, tasks) |
| 60 |
``` |
| 61 |
|
| 62 |
### For multiple users or groups |
| 63 |
|
| 64 |
You can also remove a permission on a single object from many users or groups |
| 65 |
at once by passing a QuerySet or list as the `user_or_group` argument: |
| 66 |
|
| 67 |
``` python |
| 68 |
>>> users = User.objects.filter(groups__name='employees') |
| 69 |
>>> remove_perm('change_task', users, task) |
| 70 |
``` |
| 71 |
|
| 72 |
A plain list works as well: |
| 73 |
|
| 74 |
``` python |
| 75 |
>>> user_list = [joe, jane] |
| 76 |
>>> remove_perm('change_task', user_list, task) |
| 77 |
``` |
| 78 |
|
| 79 |
And for groups: |
| 80 |
|
| 81 |
``` python |
| 82 |
>>> groups = Group.objects.filter(name__startswith='company') |
| 83 |
>>> remove_perm('change_task', groups, task) |
| 84 |
``` |
| 85 |
|
| 86 |
## Using model managers |
| 87 |
|
| 88 |
For more direct control you can call methods on the object-permission model |
| 89 |
manager instead of going through the shortcut. First, obtain the permission |
| 90 |
model for your object: |
| 91 |
|
| 92 |
``` python |
| 93 |
>>> from guardian.utils import get_user_obj_perms_model, get_group_obj_perms_model |
| 94 |
>>> UserObjectPermission = get_user_obj_perms_model(task) |
| 95 |
>>> GroupObjectPermission = get_group_obj_perms_model(task) |
| 96 |
``` |
| 97 |
|
| 98 |
### For a single object |
| 99 |
|
| 100 |
``` python |
| 101 |
>>> UserObjectPermission.objects.remove_perm('change_task', joe, task) |
| 102 |
``` |
| 103 |
|
| 104 |
For a group: |
| 105 |
|
| 106 |
``` python |
| 107 |
>>> GroupObjectPermission.objects.remove_perm('change_task', group, task) |
| 108 |
``` |
| 109 |
|
| 110 |
### For multiple objects |
| 111 |
|
| 112 |
Use `bulk_remove_perm` to remove a permission from one user or group across a |
| 113 |
QuerySet of objects: |
| 114 |
|
| 115 |
``` python |
| 116 |
>>> tasks = Task.objects.filter(reported_by=boss) |
| 117 |
>>> UserObjectPermission.objects.bulk_remove_perm('change_task', joe, tasks) |
| 118 |
``` |
| 119 |
|
| 120 |
### For multiple users or groups |
| 121 |
|
| 122 |
Use `remove_perm_from_many` to remove a permission on one object from many |
| 123 |
users or groups at once: |
| 124 |
|
| 125 |
``` python |
| 126 |
>>> users = User.objects.filter(groups__name='employees') |
| 127 |
>>> UserObjectPermission.objects.remove_perm_from_many('change_task', users, task) |
| 128 |
``` |
| 129 |
|
| 130 |
## Limitations |
| 131 |
|
| 132 |
!!! warning |
| 133 |
|
| 134 |
**No bulk global permission removal.** Passing a list or QuerySet of |
| 135 |
users/groups without an object raises |
| 136 |
`guardian.exceptions.MultipleIdentityAndObjectError`. Remove global |
| 137 |
permissions one user/group at a time. |
| 138 |
|
| 139 |
!!! warning |
| 140 |
|
| 141 |
**No dual-bulk operations.** You cannot pass lists for both |
| 142 |
`user_or_group` *and* `obj` at the same time. One side must be a single |
| 143 |
instance. Passing lists for both raises |
| 144 |
`guardian.exceptions.MultipleIdentityAndObjectError`. |
| 145 |
|
| 146 |
!!! note |
| 147 |
|
| 148 |
**Bulk operations do not fire `post_delete` signals.** Bulk removals use |
| 149 |
`QuerySet.delete()` internally, so per-object `post_delete` signals are |
| 150 |
not sent. If you rely on signals, remove permissions individually instead. |