From b50748598b7dd1d9dca1130f09432010a74f403d Mon Sep 17 00:00:00 2001 From: earthcomfy Date: Sat, 22 Oct 2022 09:15:49 +0300 Subject: [PATCH] fix - Remove staff type from group and add is-staff arg --- babybuddy/management/commands/createuser.py | 26 ++++++++++++++------- babybuddy/tests/tests_commands.py | 12 ++++++---- docs/user-guide/managing-users.md | 16 ++++++++++++- 3 files changed, 41 insertions(+), 13 deletions(-) diff --git a/babybuddy/management/commands/createuser.py b/babybuddy/management/commands/createuser.py index acf9f3d3..dec07b8a 100644 --- a/babybuddy/management/commands/createuser.py +++ b/babybuddy/management/commands/createuser.py @@ -7,7 +7,7 @@ Example usage: manage.py createuser \ --username test \ --email test@test.test \ - --group staff + --group read_only """ import sys import getpass @@ -54,15 +54,22 @@ class Command(BaseCommand): parser.add_argument( "--group", dest="groups", - choices=("read_only", "standard", "staff"), - default="read_only", - help="Specifies the group a user belongs to. Default is read_only.", + choices=("read_only", "standard"), + default="standard", + 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): username = options.get(self.UserModel.USERNAME_FIELD) password = options.get("password") - group_name = options.get("groups", "read_only") + group_name = options.get("groups", "standard") user_data = {} user_password = "" @@ -115,10 +122,13 @@ class Command(BaseCommand): DEFAULT_DB_ALIAS ).create_user(**user_data, password=user_password) user.email = options.get("email") - if group_name == "staff": - user.is_staff = True + user.is_staff = options.get("is_staff") 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: self.stdout.write(f"User {username} created successfully.") diff --git a/babybuddy/tests/tests_commands.py b/babybuddy/tests/tests_commands.py index 74c844ad..c2322e85 100644 --- a/babybuddy/tests/tests_commands.py +++ b/babybuddy/tests/tests_commands.py @@ -25,7 +25,7 @@ class CommandsTestCase(TransactionTestCase): def test_createuser(self): Group.objects.create(name="read_only") - Group.objects.create(name="staff") + Group.objects.create(name="standard") call_command( "createuser", @@ -35,14 +35,18 @@ class CommandsTestCase(TransactionTestCase): verbosity=0, ) 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( "createuser", + "--is-staff", username="testadmin", email="testadmin@testadmin.testadmin", password="test", - group="staff", verbosity=0, ) 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) + ) diff --git a/docs/user-guide/managing-users.md b/docs/user-guide/managing-users.md index 0f2667ed..5ed0f2b9 100644 --- a/docs/user-guide/managing-users.md +++ b/docs/user-guide/managing-users.md @@ -20,6 +20,12 @@ ## 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: 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 --password ``` +This will create a user with the standard privileges. + 2. Interactively setting user's password: ```shell @@ -36,7 +44,13 @@ python manage.py createuser --username 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 --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 python manage.py createuser --username --is-staff