33
33
from __future__ import with_statement
34
34
from django .contrib .auth .models import Permission
35
35
from permission .utils .converters import permission_to_perm
36
+ from permission .utils .converters import perm_to_permission
36
37
37
38
38
39
class permissions (object ):
39
40
"""
40
41
Context manager for adding extra user permissions
41
- to the ``user_obj`` instance
42
+ to the ``user_obj`` instance. This doesn't add extra
43
+ permissions on db.
42
44
43
45
Usage::
44
46
45
47
# Create user for testing
46
48
>>> from django.contrib.auth.models import User
47
49
>>> james = User.objects.create_user(
48
- ... username='permission_test_user_james ',
49
- ... email='permission_test_user_james @test.com',
50
+ ... username='permission_test_user_james1 ',
51
+ ... email='permission_test_user_james1 @test.com',
50
52
... password='password',
51
53
... )
52
54
>>> alice = User.objects.create_user(
53
- ... username='permission_test_user_alice ',
54
- ... email='permission_test_user_alice @test.com',
55
+ ... username='permission_test_user_alice1 ',
56
+ ... email='permission_test_user_alice1 @test.com',
55
57
... password='password',
56
58
... )
57
59
@@ -67,6 +69,11 @@ class permissions(object):
67
69
... assert not alice.has_perm('auth.add_user')
68
70
... assert james.has_perm('auth.change_user')
69
71
... assert not alice.has_perm('auth.change_user')
72
+ ... # But actually james doesn't have permission in db
73
+ ... assert not james.user_permissions.filter(
74
+ ... content_type__app_label='auth',
75
+ ... codename='add_user',
76
+ ... ).exists()
70
77
71
78
# They does not have 'auth.add_user' and 'auth.change_usr' permission
72
79
>>> assert not james.has_perm('auth.add_user')
@@ -102,3 +109,76 @@ def __exit__(self, exc_type, exc_val, exc_tb):
102
109
self .user_obj .get_all_permissions = self .original_get_all_permissions
103
110
delattr (self .user_obj , '_perm_cache' )
104
111
112
+ class real_permissions (object ):
113
+ """
114
+ Context manager for adding extra user permissions
115
+ to the ``user_obj`` instance. This add extra permissions
116
+ on db.
117
+
118
+ Usage::
119
+
120
+ # Create user for testing
121
+ >>> from django.contrib.auth.models import User
122
+ >>> james = User.objects.create_user(
123
+ ... username='permission_test_user_james2',
124
+ ... email='permission_test_user_james2@test.com',
125
+ ... password='password',
126
+ ... )
127
+ >>> alice = User.objects.create_user(
128
+ ... username='permission_test_user_alice2',
129
+ ... email='permission_test_user_alice2@test.com',
130
+ ... password='password',
131
+ ... )
132
+
133
+ # They does not have 'auth.add_user' and 'auth.change_usr' permission
134
+ >>> assert not james.has_perm('auth.add_user')
135
+ >>> assert not alice.has_perm('auth.add_user')
136
+ >>> assert not james.has_perm('auth.change_user')
137
+ >>> assert not alice.has_perm('auth.change_user')
138
+
139
+ # james have the permissions
140
+ >>> with real_permissions(james, 'auth.add_user', 'auth.change_user'):
141
+ ... assert james.has_perm('auth.add_user')
142
+ ... assert not alice.has_perm('auth.add_user')
143
+ ... assert james.has_perm('auth.change_user')
144
+ ... assert not alice.has_perm('auth.change_user')
145
+ ... # james DOES have permission in db
146
+ ... assert james.user_permissions.filter(
147
+ ... content_type__app_label='auth',
148
+ ... codename='add_user',
149
+ ... ).exists()
150
+
151
+ # They does not have 'auth.add_user' and 'auth.change_usr' permission
152
+ >>> assert not james.has_perm('auth.add_user')
153
+ >>> assert not alice.has_perm('auth.add_user')
154
+ >>> assert not james.has_perm('auth.change_user')
155
+ >>> assert not alice.has_perm('auth.change_user')
156
+
157
+
158
+ """
159
+ def __init__ (self , user_obj , * perms ):
160
+ self .user_obj = user_obj
161
+ self .perms = perms
162
+ # convert permission instance if necessary
163
+ def convert_if_required (perm ):
164
+ if isinstance (perm , basestring ):
165
+ perm = perm_to_permission (perm )
166
+ return perm
167
+ self .perms = set ([convert_if_required (p ) for p in self .perms ])
168
+ self ._added_permissions = []
169
+
170
+ def __enter__ (self ):
171
+ for perm in self .perms :
172
+ if perm not in self .user_obj .user_permissions .all ():
173
+ self ._added_permissions .append (perm )
174
+ self .user_obj .user_permissions .add (perm )
175
+ if hasattr (self .user_obj , '_perm_cache' ):
176
+ delattr (self .user_obj , '_perm_cache' )
177
+ return self .user_obj
178
+
179
+ def __exit__ (self , exc_type , exc_val , exc_tb ):
180
+ self .user_obj .user_permissions .remove (* self ._added_permissions )
181
+ if hasattr (self .user_obj , '_perm_cache' ):
182
+ delattr (self .user_obj , '_perm_cache' )
183
+
184
+
0 commit comments