fix - Remove staff type from group and add is-staff arg

This commit is contained in:
earthcomfy 2022-10-22 09:15:49 +03:00 committed by Christopher Charbonneau Wells
parent 400ad66968
commit b50748598b
3 changed files with 41 additions and 13 deletions

View File

@ -7,7 +7,7 @@ Example usage:
manage.py createuser \ manage.py createuser \
--username test \ --username test \
--email test@test.test \ --email test@test.test \
--group staff --group read_only
""" """
import sys import sys
import getpass import getpass
@ -54,15 +54,22 @@ class Command(BaseCommand):
parser.add_argument( parser.add_argument(
"--group", "--group",
dest="groups", dest="groups",
choices=("read_only", "standard", "staff"), choices=("read_only", "standard"),
default="read_only", default="standard",
help="Specifies the group a user belongs to. Default is read_only.", help="Specifies the group a user belongs to. Default is standard.",
)
parser.add_argument(
"--is-staff",
dest="is_staff",
action="store_true",
default=False,
help="Specifies the staff status for the user. Default is False.",
) )
def handle(self, *args, **options): def handle(self, *args, **options):
username = options.get(self.UserModel.USERNAME_FIELD) username = options.get(self.UserModel.USERNAME_FIELD)
password = options.get("password") password = options.get("password")
group_name = options.get("groups", "read_only") group_name = options.get("groups", "standard")
user_data = {} user_data = {}
user_password = "" user_password = ""
@ -115,10 +122,13 @@ class Command(BaseCommand):
DEFAULT_DB_ALIAS DEFAULT_DB_ALIAS
).create_user(**user_data, password=user_password) ).create_user(**user_data, password=user_password)
user.email = options.get("email") user.email = options.get("email")
if group_name == "staff": user.is_staff = options.get("is_staff")
user.is_staff = True
user.save() user.save()
user.groups.add(group) if user.is_staff:
user.is_superuser = True
user.save()
else:
user.groups.add(group)
if options.get("verbosity") > 0: if options.get("verbosity") > 0:
self.stdout.write(f"User {username} created successfully.") self.stdout.write(f"User {username} created successfully.")

View File

@ -25,7 +25,7 @@ class CommandsTestCase(TransactionTestCase):
def test_createuser(self): def test_createuser(self):
Group.objects.create(name="read_only") Group.objects.create(name="read_only")
Group.objects.create(name="staff") Group.objects.create(name="standard")
call_command( call_command(
"createuser", "createuser",
@ -35,14 +35,18 @@ class CommandsTestCase(TransactionTestCase):
verbosity=0, verbosity=0,
) )
self.assertIsInstance(User.objects.get(username="test"), User) self.assertIsInstance(User.objects.get(username="test"), User)
self.assertFalse(User.objects.filter(username="test", is_staff=True)) self.assertFalse(
User.objects.filter(username="test", is_staff=True, is_superuser=True)
)
call_command( call_command(
"createuser", "createuser",
"--is-staff",
username="testadmin", username="testadmin",
email="testadmin@testadmin.testadmin", email="testadmin@testadmin.testadmin",
password="test", password="test",
group="staff",
verbosity=0, verbosity=0,
) )
self.assertIsInstance(User.objects.get(username="testadmin"), User) self.assertIsInstance(User.objects.get(username="testadmin"), User)
self.assertTrue(User.objects.filter(username="testadmin", is_staff=True)) self.assertTrue(
User.objects.filter(username="testadmin", is_staff=True, is_superuser=True)
)

View File

@ -20,6 +20,12 @@
## Creating a User from the Command Line ## Creating a User from the Command Line
A user's type can be:
- Read only (can access all data but not make new entries)
- Standard (default, can access and make/edit any type of entry)
- Staff (bypasses permissions, can access Database Admin area)
There are 2 ways you can create a user from the command line: There are 2 ways you can create a user from the command line:
1. Passing user's password as an argument: 1. Passing user's password as an argument:
@ -28,6 +34,8 @@ There are 2 ways you can create a user from the command line:
python manage.py createuser --username <username> --password <password> python manage.py createuser --username <username> --password <password>
``` ```
This will create a user with the standard privileges.
2. Interactively setting user's password: 2. Interactively setting user's password:
```shell ```shell
@ -36,7 +44,13 @@ python manage.py createuser --username <username>
You will then be prompted to enter and confirm a password. You will then be prompted to enter and confirm a password.
- If you want to make the user a staff, you can append the `--is-staff` argument: - If you want to create a user with read only privileges, pass in the `read_only` value to the `group` arg:
```shell
python manage.py createuser --username <username> --password <password> --group read_only
```
- If you want to create a user with the highest level of permission, you can append the `--is-staff` argument:
```shell ```shell
python manage.py createuser --username <username> --is-staff python manage.py createuser --username <username> --is-staff