Ticket #13664: add_permission_checks_to_admin_site.patch

File add_permission_checks_to_admin_site.patch, 1.3 KB (added by Sebastian Noack, 14 years ago)
  • django/django/contrib/admin/sites.py

    diff --git a/django/django/contrib/admin/sites.py b/django/django/contrib/admin/sites.py
    index 63bf4fe..031cf36 100644
    a b class AdminSite(object):  
    229229        return self.get_urls(), self.app_name, self.name
    230230    urls = property(urls)
    231231
     232    def _has_permission(self, model, perm, *args):
     233        if not isinstance(model, ModelBase):
     234            from django.db.models import get_model
     235
     236            app_label, model_name = model.split('.')
     237            model = get_model(app_label, model_name)
     238
     239        try:
     240            admin = self._registry[model]
     241        except KeyError:
     242            return False
     243
     244        return getattr(admin, 'has_%s_permission' % perm)(*args)
     245
     246    def has_add_permission(self, request, model):
     247        return self._has_permission(model, 'add', request)
     248
     249    def has_change_permission(self, request, model, obj=None):
     250        return self._has_permission(model, 'change', request, obj)
     251
     252    def has_delete_permission(self, request, model, obj=None):
     253        return self._has_permission(model, 'delete', request, obj)
     254
    232255    def password_change(self, request):
    233256        """
    234257        Handles the "change password" task -- both form display and validation.
Back to Top