【问题标题】:Creating a staff user and giving permissions in Django在 Django 中创建员工用户并授予权限
【发布时间】:2021-12-24 00:05:02
【问题描述】:

我从管理面板为 Django 应用程序创建了一个员工用户, 但尚未手动为该用户分配任何权限, 当我使用此员工用户凭据登录时,默认情况下我将获得所有权限。 我正在使用自定义用户,但没有自定义任何权限。

请帮我解决我哪里出错了,以及如何在默认情况下创建一个没有任何权限的员工用户。

【问题讨论】:

    标签: django django-admin user-permissions superuser


    【解决方案1】:

    抱歉,再次阅读您的问题时,我想我完全误解了您。我会在下面留下我的答案,以防它帮助其他人。当您在管理界面中将用户指定为员工用户时,默认情况下您的用户不应拥有任何权限。这是我在分配员工用户时在管理界面中看到的内容:

    您的管理界面中是否已显示任何组?

    我原来的不相关答案:

    您需要手动限制视图。如果您使用基于类的视图 (CBV),您可以使用 Mixin(例如 UserPassesTestMixin 或 LoginRequiredMixin),如果您使用基于函数的视图 (FBV),则必须使用装饰器,或在函数中编写限制。

    这是一个使用 FBV 的自定义装饰器的示例:

    from functools import wraps
    from django.contrib.auth.decorators import login_required
    from django.core.exceptions import PermissionDenied
    from django.shortcuts import get_object_or_404
    from django.template.response import TemplateResponse
    
    from .models import YourModelClass
    
    
    def user_permission_test(function):
        """
        Decorator to check if the requesting user is either a superuser
        or owns the object
        """
    
        @wraps(function)
        def decorator(request, **kwargs):
    
            object = None
            pk = kwargs.get("pk", None)
            object = get_object_or_404(YourModelClass, pk=pk)
            
            if (not request.user.is_superuser) and (request.user != object.owner):
                raise PermissionDenied()
    
            return function(request, **kwargs)
    
        return decorator
    
    
    @login_required # this decorator makes sure that only logged-in users can access this view
    @user_permission_test # this decorator makes sure that either the user is a superuser or owns the object
    def detail(request, **kwargs):
        """
        Detail view
        """
        pk = kwargs.get("pk", None)
        object = get_object_or_404(YourModelClass, pk=pk)
    
        context = {"object": object}
    
        return TemplateResponse(
            request,
            template="yourapp/detail.html",
            context=context,
        )
    

    或者,在这种情况下,在没有自定义装饰器的情况下在代码中编写测试也很简单。我只包含了上面的自定义装饰器,以向您展示如何为更复杂的权限执行此操作,而无需为多个视图编写相同的代码。

    from django.contrib.auth.decorators import login_required
    from django.core.exceptions import PermissionDenied
    from django.shortcuts import get_object_or_404
    from django.template.response import TemplateResponse
    
    from .models import YourModelClass
    
    
    @login_required # this decorator makes sure that only logged-in users can access this view
    def detail(request, **kwargs):
        """
        Detail view
        """
        pk = kwargs.get("pk", None)
        object = get_object_or_404(YourModelClass, pk=pk)
        
        # here is the test to check the permissions
        if (not request.user.is_superuser) and (request.user != object.owner):
            raise PermissionDenied()
    
        context = {"object": object}
    
        return TemplateResponse(
            request,
            template="yourapp/detail.html",
            context=context,
        )
    

    对于您的员工用户,您可以使用以下装饰器:

    from django.contrib.admin.views.decorators import staff_member_required
    
    @staff_member_required
    def yourview(request, pk):
        ....
    

    【讨论】:

      猜你喜欢
      • 2020-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      • 2012-08-08
      相关资源
      最近更新 更多