简体   繁体   English

Django-管理界面-允许为非超级用户创建用户

[英]Django - Admin Interfaces - to allow create users for non-superuser

I have the situation of permits the access to admin interfaces to three types of users: - Admin - Supervisor - Agent 我的情况是允许三种类型的用户访问管理界面:-管理员-主管-代理

It's a situation hierarchycal, the admin is one (the superuser) and it's creates the supervisors, and the supervisors create the agents. 这是一种层次结构的情况,管理员是一个(超级用户),它创建了主管,而主管则创建了代理。

All them can login to django admin with distincts authorizations. 他们所有人都可以使用专有授权登录django admin。

The login has managed by 'django.contrib.auth' with the default model auth_user (. 该登录名已由“ django.contrib.auth”使用默认模型auth_user(。

     from django.contrib.auth.models import User

class Supervisor(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    e_mail = models.EmailField(max_length=60, db_column='E-Mail',blank=True)
   ...other fields....


class Admin(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    e_mail = models.EmailField(max_length=60, db_column='E-Mail',blank=True)
     ...other fields...

But, there is a problem. 但有个问题。 If I allow the supervisor to create an agent implies that I must add the authorization to ADD and CHANGE the table USER. 如果我允许主管创建代理,则意味着我必须将授权添加到ADD并更改USER表。 And this is dangerous, any supervisors could become a superuser, deleting users, etc etc.... 这很危险,任何主管都可能成为超级用户,删除用户等。

How can I resolve this problem?? 我该如何解决这个问题? Is it possible to permit the supervisor to create an Agent without that he can be dangerous?? 是否可以允许主管创建代理,而没有危险呢?

Thanks 谢谢


EDIT 编辑

I have a doubt... in models.py during a definition of class I written this method: 我有一个疑问...在定义类的过程中,我在models.py中编写了此方法:

def save(self):
    self.user.is_staff = True
    self.user.save()
    super(Agent, self).save()

In Java, the method of EJB it was transactional/atomic (the commit is automatic)... In django, I have to call the method save(). 在Java中,EJB的方法是事务性/原子性的(提交是自动的)...在Django中,我必须调用方法save()。

It is a question different from the previous..... 这是一个与以前不同的问题。

Don't know if I got to understand your problem. 不知道我是否要了解你的问题。 But, if I did, you could edit the user creation form. 但是,如果这样做,您可以编辑用户创建表单。

@admin.register(User)
class UserAdmin(BaseUserAdmin):
    view_on_site = False
    list_display = ('username', 'first_name', 'last_name', 'is_active', 'is_staff', 'date_joined', 'last_login')
    list_filter = ('is_staff', )
    fieldsets = (
        ('Data', {'fields': ('username', 'email', 'first_name', 'last_name', 'password')}),
        ('Activation/Deactivation', {'fields': ('is_active', )}),
        ('Permissions', {'fields': ('is_staff', 'groups', )})
)

As superuser is not an option anymore, your supervisors can't became superusers. 由于不再具有superuser选项,因此您的主管不能成为超级用户。

Let me know if this helps. 让我知道是否有帮助。 Maybe we can get closer to the solution. 也许我们可以更接近解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM