【问题标题】:django multiple admin instances and locking down access to a particular instancedjango 多个管理实例并锁定对特定实例的访问
【发布时间】:2011-12-08 16:20:22
【问题描述】:

我在一个站点上运行了多个管理实例 - 每个国家/地区一个,站点支持。

但是,如果用户登录到一个管理员,他们将自动能够访问其他实例。

我需要让身份验证代码知道用户登录了哪个管理员并阻止访问其他管理员系统。

有什么想法可以做到这一点吗?

【问题讨论】:

    标签: django admin authentication


    【解决方案1】:

    您可以使用middleware 来检查用户访问管理站点某些区域的权限。结帐此snippet。 (您可能想了解有关处理custom permissions in Django 的更多信息。)

    如果您需要更通用的东西,可以使用下面的代码示例。这个想法很简单:它使用自定义函数来了解用户权限并给出适当的响应:

    #coding: utf-8
    # Note that RESTRICTED_URLS tuple takes three parameters: url regex, function to check
    # whether user has certain permission, and a function to redirect the user to a certain 
    # page if he doesn't have sufficient rights.
    import re
    from django.core.urlresolvers import reverse
    from django.utils.translation import ugettext_lazy as _
    from django.http import HttpResponseRedirect
    from django.contrib import messages
    from backend.models import Professional
    from django.contrib.auth.decorators import permission_required
    
    def calculate_forbidden_response(request, view_func,view_args,view_kwargs):
        if not request.user.is_authenticated():
            return permission_required('')(view_func)(request,*view_args,**view_kwargs)
        elif request.user.has_perm('backend.p_add_professional'):
            messages.error(request, _('You need permission Spam to enter this cabinet.'))
            return HttpResponseRedirect('/some_help_page_about_permissions.html')
    
    def check_professional_permission(request):
        return request.user.has_perm('backend.p_access_professional_cabinet')
    
    RESTRICTED_URLS = (
                        (r'/professional/(.*)$', check_professional_permission, calculate_forbidden_response),
                      )
    RESTRICTED_URLS_EXCEPTIONS = ()
    
    class CheckPermissionMiddleware(object):
        def __init__(self):
            self.restricted = tuple([(re.compile(url[0]), url[1], url[2]) for url in RESTRICTED_URLS])
            self.exceptions = tuple([re.compile(url) for url in RESTRICTED_URLS_EXCEPTIONS])
    
        def process_view(self,request,view_func,view_args,view_kwargs):
            if request.user.is_superuser:
                return None
            for path in self.exceptions:
                if path.match(request.path): return None
            for rule in self.restricted:
                url, permission = rule[0], rule[1]
                calculated_response = rule[2]
                if url.match(request.path):
                    if not permission(request):
                        return calculated_response(request, view_func,view_args,view_kwargs)
                    else:
                        return None
            return None
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-09
      • 1970-01-01
      • 2017-10-01
      • 2021-10-08
      • 2021-12-05
      • 2017-08-22
      • 1970-01-01
      相关资源
      最近更新 更多