Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>they gain the ability to set the is_superuser flag on any account, including their own. (!!!)</p> </blockquote> <p>Not only this, they also gain the ability to give themselves any permissions one-by-one, same effect...</p> <blockquote> <p>I'm sure it involves subclassing django.contrib.auth.forms.UserChangeForm</p> </blockquote> <p>Well, not necessarily. The form you see in the change page of django's admin is dynamically created by the admin application, and based on <code>UserChangeForm</code>, but this class barely adds regex validation to the <code>username</code> field.</p> <blockquote> <p>and hooking it into my already-custom UserAdmin object... </p> </blockquote> <p>A custom <code>UserAdmin</code> is the way to go here. Basically, you want to change the <code>fieldsets</code> property to something like that :</p> <pre><code>class MyUserAdmin(UserAdmin): fieldsets = ( (None, {'fields': ('username', 'password')}), (_('Personal info'), {'fields': ('first_name', 'last_name', 'email')}), # Removing the permission part # (_('Permissions'), {'fields': ('is_staff', 'is_active', 'is_superuser', 'user_permissions')}), (_('Important dates'), {'fields': ('last_login', 'date_joined')}), # Keeping the group parts? Ok, but they shouldn't be able to define # their own groups, up to you... (_('Groups'), {'fields': ('groups',)}), ) </code></pre> <p>But the problem here is that this restriction will apply to all users. If this is not what you want, you could for example override <code>change_view</code> to behave differently depending on the permission of the users. Code snippet :</p> <pre><code>class MyUserAdmin(UserAdmin): staff_fieldsets = ( (None, {'fields': ('username', 'password')}), (_('Personal info'), {'fields': ('first_name', 'last_name', 'email')}), # No permissions (_('Important dates'), {'fields': ('last_login', 'date_joined')}), (_('Groups'), {'fields': ('groups',)}), ) def change_view(self, request, *args, **kwargs): # for non-superuser if not request.user.is_superuser: try: self.fieldsets = self.staff_fieldsets response = super(MyUserAdmin, self).change_view(request, *args, **kwargs) finally: # Reset fieldsets to its original value self.fieldsets = UserAdmin.fieldsets return response else: return super(MyUserAdmin, self).change_view(request, *args, **kwargs) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload