Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #9036: unified the permission checking in `AdminSite`, pushing …

…it down to the `ModelAdmin` where it belongs.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@10451 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 7d1b4295b9779788b4cbd4b7bb99240807c62aa3 1 parent 28f6b71
Jacob Kaplan-Moss authored April 08, 2009
12  django/contrib/admin/options.py
@@ -291,6 +291,18 @@ def has_delete_permission(self, request, obj=None):
291 291
         opts = self.opts
292 292
         return request.user.has_perm(opts.app_label + '.' + opts.get_delete_permission())
293 293
 
  294
+    def get_model_perms(self, request):
  295
+        """
  296
+        Returns a dict of all perms for this model. This dict has the keys
  297
+        ``add``, ``change``, and ``delete`` mapping to the True/False for each
  298
+        of those actions.
  299
+        """
  300
+        return {
  301
+            'add': self.has_add_permission(request),
  302
+            'change': self.has_change_permission(request),
  303
+            'delete': self.has_delete_permission(request),
  304
+        }
  305
+
294 306
     def queryset(self, request):
295 307
         """
296 308
         Returns a QuerySet of all model instances that can be edited by the
13  django/contrib/admin/sites.py
@@ -328,11 +328,7 @@ def index(self, request, extra_context=None):
328 328
             has_module_perms = user.has_module_perms(app_label)
329 329
 
330 330
             if has_module_perms:
331  
-                perms = {
332  
-                    'add': model_admin.has_add_permission(request),
333  
-                    'change': model_admin.has_change_permission(request),
334  
-                    'delete': model_admin.has_delete_permission(request),
335  
-                }
  331
+                perms = model_admin.get_model_perms(request)
336 332
 
337 333
                 # Check whether user has any perm for this module.
338 334
                 # If so, add the module to the model_list.
@@ -391,11 +387,8 @@ def app_index(self, request, app_label, extra_context=None):
391 387
         for model, model_admin in self._registry.items():
392 388
             if app_label == model._meta.app_label:
393 389
                 if has_module_perms:
394  
-                    perms = {
395  
-                        'add': user.has_perm("%s.%s" % (app_label, model._meta.get_add_permission())),
396  
-                        'change': user.has_perm("%s.%s" % (app_label, model._meta.get_change_permission())),
397  
-                        'delete': user.has_perm("%s.%s" % (app_label, model._meta.get_delete_permission())),
398  
-                    }
  390
+                    perms = model_admin.get_model_perms(request)
  391
+
399 392
                     # Check whether user has any perm for this module.
400 393
                     # If so, add the module to the model_list.
401 394
                     if True in perms.values():

0 notes on commit 7d1b429

Please sign in to comment.
Something went wrong with that request. Please try again.