Code

Ticket #12557: anonymous_permissions.diff

File anonymous_permissions.diff, 2.5 KB (added by hvdklauw, 4 years ago)
Line 
1Index: django/contrib/auth/backends.py
2===================================================================
3--- django/contrib/auth/backends.py     (revision 12222)
4+++ django/contrib/auth/backends.py     (working copy)
5@@ -28,6 +28,8 @@
6         Returns a set of permission strings that this user has through his/her
7         groups.
8         """
9+        if user_obj.is_anonymous():
10+            return set([])
11         if not hasattr(user_obj, '_group_perm_cache'):
12             cursor = connection.cursor()
13             # The SQL below works out to the following, after DB quoting:
14@@ -58,6 +60,8 @@
15         return user_obj._group_perm_cache
16 
17     def get_all_permissions(self, user_obj):
18+        if user_obj.is_anonymous():
19+            return set([])
20         if not hasattr(user_obj, '_perm_cache'):
21             user_obj._perm_cache = set([u"%s.%s" % (p.content_type.app_label, p.codename) for p in user_obj.user_permissions.select_related()])
22             user_obj._perm_cache.update(self.get_group_permissions(user_obj))
23Index: django/contrib/auth/models.py
24===================================================================
25--- django/contrib/auth/models.py       (revision 12222)
26+++ django/contrib/auth/models.py       (working copy)
27@@ -397,12 +397,33 @@
28     user_permissions = property(_get_user_permissions)
29 
30     def has_perm(self, perm, obj=None):
31+        for backend in auth.get_backends():
32+            if hasattr(backend, "has_perm"):
33+                if obj is not None:
34+                    if (backend.supports_object_permissions and
35+                        backend.has_perm(self, perm, obj)):
36+                            return True
37+                else:
38+                    if backend.has_perm(self, perm):
39+                        return True
40         return False
41 
42     def has_perms(self, perm_list, obj=None):
43-        return False
44+        """
45+        Returns True if the user has each of the specified permissions.
46+        If object is passed, it checks if the user has all required perms
47+        for this object.
48+        """
49+        for perm in perm_list:
50+            if not self.has_perm(perm, obj):
51+                return False
52+        return True
53 
54-    def has_module_perms(self, module):
55+    def has_module_perms(self, app_label):
56+        for backend in auth.get_backends():
57+            if hasattr(backend, "has_module_perms"):
58+                if backend.has_module_perms(self, app_label):
59+                    return True
60         return False
61 
62     def get_and_delete_messages(self):