mirror of
https://github.com/netbox-community/netbox.git
synced 2025-12-20 11:16:24 +00:00
* fix(users): Override create_superuser to drop is_staff Override `UserManager.create_superuser()` to strip `is_staff` from `extra_fields` and enforce `is_superuser=True`, fixing the `TypeError` during `createsuperuser` with the custom `User` model. Fixes #20342 * Set alters_data=True on manager methods --------- Co-authored-by: Jeremy Stretch <jstretch@netboxlabs.com>
This commit is contained in:
parent
24fff6bd74
commit
b4eaeead13
@ -3,7 +3,7 @@ from django.contrib.auth.models import (
|
|||||||
GroupManager as DjangoGroupManager,
|
GroupManager as DjangoGroupManager,
|
||||||
Permission,
|
Permission,
|
||||||
PermissionsMixin,
|
PermissionsMixin,
|
||||||
UserManager as DjangoUserManager
|
UserManager as DjangoUserManager,
|
||||||
)
|
)
|
||||||
from django.contrib.auth.validators import UnicodeUsernameValidator
|
from django.contrib.auth.validators import UnicodeUsernameValidator
|
||||||
from django.core.exceptions import ValidationError
|
from django.core.exceptions import ValidationError
|
||||||
@ -74,9 +74,37 @@ class Group(models.Model):
|
|||||||
class UserManager(DjangoUserManager.from_queryset(RestrictedQuerySet)):
|
class UserManager(DjangoUserManager.from_queryset(RestrictedQuerySet)):
|
||||||
|
|
||||||
def create_user(self, username, email=None, password=None, **extra_fields):
|
def create_user(self, username, email=None, password=None, **extra_fields):
|
||||||
extra_fields.setdefault("is_superuser", False)
|
extra_fields.setdefault('is_superuser', False)
|
||||||
return self._create_user(username, email, password, **extra_fields)
|
return self._create_user(username, email, password, **extra_fields)
|
||||||
|
|
||||||
|
create_user.alters_data = True
|
||||||
|
|
||||||
|
async def acreate_user(self, username, email=None, password=None, **extra_fields):
|
||||||
|
extra_fields.setdefault('is_superuser', False)
|
||||||
|
return await self._acreate_user(username, email, password, **extra_fields)
|
||||||
|
|
||||||
|
acreate_user.alters_data = True
|
||||||
|
|
||||||
|
def create_superuser(self, username, email=None, password=None, **extra_fields):
|
||||||
|
extra_fields.setdefault('is_superuser', True)
|
||||||
|
|
||||||
|
if extra_fields.get('is_superuser') is not True:
|
||||||
|
raise ValueError('Superuser must have is_superuser=True.')
|
||||||
|
|
||||||
|
return self._create_user(username, email, password, **extra_fields)
|
||||||
|
|
||||||
|
create_superuser.alters_data = True
|
||||||
|
|
||||||
|
async def acreate_superuser(self, username, email=None, password=None, **extra_fields):
|
||||||
|
extra_fields.setdefault('is_superuser', True)
|
||||||
|
|
||||||
|
if extra_fields.get('is_superuser') is not True:
|
||||||
|
raise ValueError('Superuser must have is_superuser=True.')
|
||||||
|
|
||||||
|
return await self._acreate_user(username, email, password, **extra_fields)
|
||||||
|
|
||||||
|
acreate_superuser.alters_data = True
|
||||||
|
|
||||||
|
|
||||||
class User(AbstractBaseUser, PermissionsMixin):
|
class User(AbstractBaseUser, PermissionsMixin):
|
||||||
username = models.CharField(
|
username = models.CharField(
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user