向新用户授予默认权限或默认组

向新用户授予默认权限或默认组

本文介绍了向新用户授予默认权限或默认组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认行为似乎是新用户没有权限也没有组。但是,我不想手动为每个新用户授予基本权限,并且我认为他们不想等我这样做。

Default behaviour seems to be new users have no permissions and no groups. However I'd like not to have to manually grant every single new users basic permissions, and I'd assume they'd like not to have to wait for me to do so.

我应该如何为新用户分配默认权限?

How should I assign default permissions for new users?

有人问了类似的问题,但对于一般情况却没有明确的答案:

Some similar questions have been asked but with no clear answer for the general case:





  • Django default user permissions
  • How do I define default permissions for users in Django Guardian?
  • Django Assign-perm w/ Post-Save Signal

我是使用 python-social-auth ,所以我没有自己的create-user表单,无法查看其他人设置d的位置错误权限。我假设我需要某种用户创建的钩子,但不确定最干净的方法是什么。

I'm using python-social-auth so I don't have my own create-user form and view which I guess is where everyone else sets default permissions. I assume I need an on-user-create hook of some sort but not sure what the cleanest approach is.

这与创建默认的权限无关,该默认权限可能是授予用户,但仅供参考

This is unrelated to creating the default possible permissions that may be granted to users, although for reference,




  • Default permission/group data in Django
  • In Django, how do you programmatically create a group with permissions

推荐答案

您可以订阅在用户模型上发出信号,并将新创建的用户放入所需的组或添加权限。

You can subscribe to post_save signal on User model and put newly created user to desired group or add permissions.

from django.contrib.auth.models import Group

def add_to_default_group(sender, **kwargs):
    user = kwargs["instance"]
    if kwargs["created"]:
        group = Group.objects.get(name='groupname')
        user.groups.add(group)

在django 1.8+上,将以下代码放入您的

And on django 1.8+ put following code into your AppConfig.ready()

from django.conf import settings

post_save.connect(add_to_default_group, sender=settings.AUTH_USER_MODEL)

这篇关于向新用户授予默认权限或默认组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 04:43