Ticket #11010: object_permissions_r11712_#11010.diff
File object_permissions_r11712_#11010.diff, 12.8 KB (added by , 15 years ago) |
---|
-
django/contrib/auth/__init__.py
diff --git a/django/contrib/auth/__init__.py b/django/contrib/auth/__init__.py index b89aee1..c17f65e 100644
a b 1 1 import datetime 2 3 from warnings import warn as warn_ 4 2 5 from django.core.exceptions import ImproperlyConfigured 3 6 from django.utils.importlib import import_module 4 7 … … def load_backend(path): 19 22 cls = getattr(mod, attr) 20 23 except AttributeError: 21 24 raise ImproperlyConfigured, 'Module "%s" does not define a "%s" authentication backend' % (module, attr) 25 26 try: 27 getattr(cls, 'supports_object_perms') 28 except AttributeError: 29 warn_("%s should define `supports_object_perms`." % cls, 30 PendingDeprecationWarning, stacklevel=3) 31 cls.supports_object_perms = False 32 22 33 return cls() 23 34 24 35 def get_backends(): -
django/contrib/auth/backends.py
diff --git a/django/contrib/auth/backends.py b/django/contrib/auth/backends.py index 05f9835..df73a29 100644
a b class ModelBackend(object): 11 11 """ 12 12 Authenticates against django.contrib.auth.models.User. 13 13 """ 14 supports_object_perms = False 15 14 16 # TODO: Model, login attribute name and password attribute name should be 15 17 # configurable. 16 18 def authenticate(self, username=None, password=None): -
django/contrib/auth/models.py
diff --git a/django/contrib/auth/models.py b/django/contrib/auth/models.py index e337bec..29c32a0 100644
a b class User(models.Model): 194 194 def has_usable_password(self): 195 195 return self.password != UNUSABLE_PASSWORD 196 196 197 def get_group_permissions(self ):197 def get_group_permissions(self, obj=None): 198 198 """ 199 199 Returns a list of permission strings that this user has through 200 200 his/her groups. This method queries all available auth backends. 201 If an object is passed in, only permissions matching this object 202 are returned. 201 203 """ 202 204 permissions = set() 203 205 for backend in auth.get_backends(): 204 206 if hasattr(backend, "get_group_permissions"): 205 permissions.update(backend.get_group_permissions(self)) 207 if obj: 208 if backend.supports_object_perms: 209 permissions.update(backend.get_group_permissions(self, obj)) 210 else: 211 permissions.update(backend.get_group_permissions(self)) 206 212 return permissions 207 213 208 def get_all_permissions(self ):214 def get_all_permissions(self, obj=None): 209 215 permissions = set() 210 216 for backend in auth.get_backends(): 211 217 if hasattr(backend, "get_all_permissions"): 212 permissions.update(backend.get_all_permissions(self)) 218 if obj: 219 if backend.supports_object_perms: 220 permissions.update(backend.get_all_permissions(self, obj)) 221 else: 222 permissions.update(backend.get_all_permissions(self)) 213 223 return permissions 214 224 215 def has_perm(self, perm ):225 def has_perm(self, perm, obj=None): 216 226 """ 217 227 Returns True if the user has the specified permission. This method 218 228 queries all available auth backends, but returns immediately if any 219 229 backend returns True. Thus, a user who has permission from a single 220 auth backend is assumed to have permission in general. 230 auth backend is assumed to have permission in general. If an object 231 is provided, permissions for this specific object are checked. 221 232 """ 222 233 # Inactive users have no permissions. 223 234 if not self.is_active: … … class User(models.Model): 230 241 # Otherwise we need to check the backends. 231 242 for backend in auth.get_backends(): 232 243 if hasattr(backend, "has_perm"): 233 if backend.has_perm(self, perm): 234 return True 244 if obj: 245 if backend.supports_object_perms: 246 if backend.has_perm(self, perm, obj): 247 return True 248 else: 249 if backend.has_perm(self, perm): 250 return True 235 251 return False 236 252 237 def has_perms(self, perm_list): 238 """Returns True if the user has each of the specified permissions.""" 253 def has_perms(self, perm_list, obj=None): 254 """Returns True if the user has each of the specified permissions. 255 If object is passed, it checks if the user has all required perms 256 for this object. 257 """ 239 258 for perm in perm_list: 240 if not self.has_perm(perm ):259 if not self.has_perm(perm, obj): 241 260 return False 242 261 return True 243 262 -
django/contrib/auth/tests/__init__.py
diff --git a/django/contrib/auth/tests/__init__.py b/django/contrib/auth/tests/__init__.py index 14428d0..9a078cf 100644
a b from django.contrib.auth.tests.views \ 4 4 from django.contrib.auth.tests.forms import FORM_TESTS 5 5 from django.contrib.auth.tests.remote_user \ 6 6 import RemoteUserTest, RemoteUserNoCreateTest, RemoteUserCustomTest 7 from django.contrib.auth.tests.auth_backends import BackendTest, RowlevelBackendTest 7 8 from django.contrib.auth.tests.tokens import TOKEN_GENERATOR_TESTS 8 9 9 10 # The password for the fixture data users is 'password' -
docs/internals/deprecation.txt
diff --git a/docs/internals/deprecation.txt b/docs/internals/deprecation.txt index 480b527..b10a575 100644
a b their deprecation, as per the :ref:`Django deprecation policy 13 13 hooking up admin URLs. This has been deprecated since the 1.1 14 14 release. 15 15 16 * Authentication backends need to define ``supports_object_perms``. The 17 old backend style got is deprecated since the 1.2 release. 18 16 19 * 1.4 17 20 * ``CsrfResponseMiddleware``. This has been deprecated since the 1.2 18 21 release, in favour of the template tag method for inserting the CSRF … … their deprecation, as per the :ref:`Django deprecation policy 28 31 * The many to many SQL generation functions on the database backends 29 32 will be removed. These have been deprecated since the 1.2 release. 30 33 34 * Authentication backends need to support the ``obj`` parameter for 35 permission checking. The ``supports_object_perms`` variable is not 36 checked anylonger and can be removed. 37 31 38 * 2.0 32 39 * ``django.views.defaults.shortcut()``. This function has been moved 33 40 to ``django.contrib.contenttypes.views.shortcut()`` as part of the -
docs/topics/auth.txt
diff --git a/docs/topics/auth.txt b/docs/topics/auth.txt index 33461a0..9baa732 100644
a b Methods 199 199 :meth:`~django.contrib.auth.models.User.set_unusable_password()` has 200 200 been called for this user. 201 201 202 .. method:: models.User.get_group_permissions( )202 .. method:: models.User.get_group_permissions(obj=None) 203 203 204 204 Returns a list of permission strings that the user has, through his/her 205 groups. 205 groups. If ``obj`` is passed in, only returns the group permissions for 206 this specific object. 206 207 207 .. method:: models.User.get_all_permissions( )208 .. method:: models.User.get_all_permissions(obj=None) 208 209 209 210 Returns a list of permission strings that the user has, both through 210 group and user permissions. 211 group and user permissions. If ``obj`` is passed in, only returns the 212 permissions for this specific object. 211 213 212 .. method:: models.User.has_perm(perm) 214 215 .. method:: models.User.has_perm(perm, obj=None) 213 216 214 217 Returns ``True`` if the user has the specified permission, where perm is 215 218 in the format ``"<app label>.<permission codename>"``. 216 If the user is inactive, this method will always return ``False``. 219 If the user is inactive, this method will always return ``False``. If 220 ``obj`` is passed in this method won't check the permissions for the model, 221 but the object. 217 222 218 .. method:: models.User.has_perms(perm_list )223 .. method:: models.User.has_perms(perm_list, obj=None) 219 224 220 225 Returns ``True`` if the user has each of the specified permissions, 221 226 where each perm is in the format 222 227 ``"<app label>.<permission codename>"``. If the user is inactive, 223 this method will always return ``False``. 228 this method will always return ``False``. If ``obj`` is passed in 229 this method won't check the permissions for the model, but the object. 224 230 225 231 .. method:: models.User.has_module_perms(package_name) 226 232 … … A full authorization implementation can be found in 1510 1516 the ``auth_permission`` table most of the time. 1511 1517 1512 1518 .. _django/contrib/auth/backends.py: http://code.djangoproject.com/browser/django/trunk/django/contrib/auth/backends.py 1519 1520 Handling object permissions 1521 --------------------------- 1522 1523 Django's permission framework has a foundation for object permissions, though 1524 there is no implementation for it in the core. This means, that checking for 1525 object permissions will always return ``False`` or an empty list (depending on 1526 the check performed). To enable object permissions you will have to write your 1527 own Backend which does support them, the only change you have to do is to add 1528 an ``obj`` parameter to the permission functions and set ``supports_objects_perms`` 1529 to ``True``. In Django 1.2 a non existing ``supports_objects_perms`` will raise a 1530 ``PendingDeprecationWarning``. In 1.3 a non existant ``support_object_perms`` 1531 attribute will raise an error and setting it to ``False`` will raise a 1532 ``DeprecationWarning``. In 1.4 it's assumed that every backend supports object 1533 permissions and no checking is performed, which means not supporting ``obj`` as 1534 parameter will raise a ``TypeError``. -
deleted file tests/regressiontests/auth_backends/tests.py
diff --git a/tests/regressiontests/auth_backends/__init__.py b/tests/regressiontests/auth_backends/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/tests/regressiontests/auth_backends/models.py b/tests/regressiontests/auth_backends/models.py deleted file mode 100644 index e69de29..0000000 diff --git a/tests/regressiontests/auth_backends/tests.py b/tests/regressiontests/auth_backends/tests.py deleted file mode 100644 index d22f0bf..0000000
+ - 1 try:2 set3 except NameError:4 from sets import Set as set # Python 2.3 fallback5 6 __test__ = {'API_TESTS': """7 >>> from django.contrib.auth.models import User, Group, Permission, AnonymousUser8 >>> from django.contrib.contenttypes.models import ContentType9 10 # No Permissions assigned yet, should return False except for superuser11 12 >>> user = User.objects.create_user('test', 'test@example.com', 'test')13 >>> user.has_perm("auth.test")14 False15 >>> user.is_staff=True16 >>> user.save()17 >>> user.has_perm("auth.test")18 False19 >>> user.is_superuser=True20 >>> user.save()21 >>> user.has_perm("auth.test")22 True23 >>> user.is_staff = False24 >>> user.is_superuser = False25 >>> user.save()26 >>> user.has_perm("auth.test")27 False28 >>> content_type=ContentType.objects.get_for_model(Group)29 >>> perm = Permission.objects.create(name="test", content_type=content_type, codename="test")30 >>> user.user_permissions.add(perm)31 >>> user.save()32 33 # reloading user to purge the _perm_cache34 35 >>> user = User.objects.get(username="test")36 >>> user.get_all_permissions() == set([u'auth.test'])37 True38 >>> user.get_group_permissions() == set([])39 True40 >>> user.has_module_perms("Group")41 False42 >>> user.has_module_perms("auth")43 True44 >>> perm = Permission.objects.create(name="test2", content_type=content_type, codename="test2")45 >>> user.user_permissions.add(perm)46 >>> user.save()47 >>> perm = Permission.objects.create(name="test3", content_type=content_type, codename="test3")48 >>> user.user_permissions.add(perm)49 >>> user.save()50 >>> user = User.objects.get(username="test")51 >>> user.get_all_permissions() == set([u'auth.test2', u'auth.test', u'auth.test3'])52 True53 >>> user.has_perm('test')54 False55 >>> user.has_perm('auth.test')56 True57 >>> user.has_perms(['auth.test2', 'auth.test3'])58 True59 >>> perm = Permission.objects.create(name="test_group", content_type=content_type, codename="test_group")60 >>> group = Group.objects.create(name='test_group')61 >>> group.permissions.add(perm)62 >>> group.save()63 >>> user.groups.add(group)64 >>> user = User.objects.get(username="test")65 >>> exp = set([u'auth.test2', u'auth.test', u'auth.test3', u'auth.test_group'])66 >>> user.get_all_permissions() == exp67 True68 >>> user.get_group_permissions() == set([u'auth.test_group'])69 True70 >>> user.has_perms(['auth.test3', 'auth.test_group'])71 True72 73 >>> user = AnonymousUser()74 >>> user.has_perm('test')75 False76 >>> user.has_perms(['auth.test2', 'auth.test3'])77 False78 """}