用于用户/帐户设置的 Django 应用程序

发布于 2024-10-18 03:13:06 字数 760 浏览 5 评论 0原文

我正在准备开发一个项目,并且正在制定我可以用于该项目的东西。该项目将需要基于不同组/用户的模型和模型。对象权限和设置。

我知道我可以使用 django-objectpermissions (https://github.com/washingtontimes/django-objectpermissions)和 djangos 自己的模型权限。但在某些情况下,您实际上并不需要设置对象/模型权限,而是需要设置 - 该帐户/用户是否可以执行与某些对象或模型无关的操作。有人为此开发了应用程序吗?或者说,当人们有这样的需求时,会采取什么样的做法呢?也许创建虚拟 django 模型并让 djangos 模型权限处理其余的事情?

我也知道,这会做类似的事情 - https://github.com/danielroseman/django-dbsettings。但是当我浏览代码时,我得到的印象是,这些设置都是基于用户的 - 我还需要基于组的设置。

编辑:人们不断向我提供权限应用程序。我正在寻找的不是权限应用程序,而是设置应用程序。这些设置/权限与任何对象都不相关。

基本上。在我的项目中,我需要回答问题 - 该用户可以看到/执行此操作吗?这个“东西”很可能是一个视图。所以答案之一几乎有效。但我在视图中所做的检查不是用户是否有权访问某些模型/对象。相反,如果用户打开了此设置,或者用户组是否打开了此设置。

艾伦

Im prepping for developing one project and am in process of mapping out stuff i can use for it. This project is going to need different group/user based model & object permissions and settings.

I know i can use django-objectpermissions (https://github.com/washingtontimes/django-objectpermissions) and djangos own model permissions. But there are some cases, when you dont really need to set object/model permissions, but rather a setting - can this account/user do something, which is not related to certain object or model. Has someone developed app for this? Or what kind of approach do people take when they have such need? Perhaps create dummy django model and let djangos model permissions handle the rest?

I also know, that this does something similar - https://github.com/danielroseman/django-dbsettings. But when i skimmed through the code i got impression, that those settings are all user based - i need also group based settings.

Edit: People keep offering me permissions apps. What i am looking for is not permissions app, but settings app. Those settings/permissions are not related to any objects.

Basically. In my project i need to answer questions - Can this user see/do this thing?. That "thing" is most likely a view. So one of the answers almost works. But the check i do at view, is not if user has permission to some model/object. Its rather, if the user has this setting turned on, of if users group has this setting turned on.

Alan

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甩你一脸翔 2024-10-25 03:13:06

您可能需要为此创建自己的系统。应该不是很困难。

class Setting(models.Model):
    name = models.CharField(..)
    value = models.CharField(..)
    users = models.ManyToManyField(User)
    groups = models.ManyToManyField(Group)

    @classmethod
    def has_setting(cls, name, user):
        user_settings = cls.objects.filter(name=name, users__in=[user]).count()
        group_settings = cls.objects.filter(name=name, groups__in=user.groups.all()).count()
        return user_settings or group_settings

    @classmethod
    def get_settings(cls, name, user, priority=User):
        user_settings = list(cls.objects.filter(name=name, users__in=[user]))
        group_settings = list(cls.objects.filter(name=name, groups__in=user.groups.all()))
        if user_settings and not group_settings: 
            return user_settings
        elif group_settings and not user_settings:
            return group_settings
        else:
            return (group_settings, user_settings)[priority==User]

然后,您可以在视图中执行类似的操作:

def display_frob(request):
    if Setting.has_setting('display_frob', request.user):
         settings = Setting.get_setting('display_from', request.user, priority=Group)
         values = [setting.value for setting in settings]
         # if the User was in many groups, we now have a list of values, for the setting
         # `display_frob`, that aligns with each of those groups

您可以轻松构建一个装饰器来执行检查,并向视图提供值列表(或单个项目)。

You're probably going to need to create your own system for this. It shouldn't be very difficult.

class Setting(models.Model):
    name = models.CharField(..)
    value = models.CharField(..)
    users = models.ManyToManyField(User)
    groups = models.ManyToManyField(Group)

    @classmethod
    def has_setting(cls, name, user):
        user_settings = cls.objects.filter(name=name, users__in=[user]).count()
        group_settings = cls.objects.filter(name=name, groups__in=user.groups.all()).count()
        return user_settings or group_settings

    @classmethod
    def get_settings(cls, name, user, priority=User):
        user_settings = list(cls.objects.filter(name=name, users__in=[user]))
        group_settings = list(cls.objects.filter(name=name, groups__in=user.groups.all()))
        if user_settings and not group_settings: 
            return user_settings
        elif group_settings and not user_settings:
            return group_settings
        else:
            return (group_settings, user_settings)[priority==User]

Then you can do something like this in your views:

def display_frob(request):
    if Setting.has_setting('display_frob', request.user):
         settings = Setting.get_setting('display_from', request.user, priority=Group)
         values = [setting.value for setting in settings]
         # if the User was in many groups, we now have a list of values, for the setting
         # `display_frob`, that aligns with each of those groups

You can easy build a decorator which will do the check, and provide a list (or a single item) of values to the view.

波浪屿的海角声 2024-10-25 03:13:06

对于“操作”的权限(操作通常由视图实现),我通常使用装饰器。

user_passes_test 装饰器非常适合这种目的。

您可以创建不链接到模型的用户权限。

For permissions for "actions" (where an action is typically implemented by a view) I commonly use decorators.

The user_passes_test decorator is great for this kind of purpose.

You could create user permissions not linked to models.

红玫瑰 2024-10-25 03:13:06

也许这就是您正在寻找的: django-guardian

Maybe this is what you looking for : django-guardian

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文