diff --git a/django_etebase/admin-cli/management/commands/user-create.py b/django_etebase/admin-cli/management/commands/users-create.py similarity index 69% rename from django_etebase/admin-cli/management/commands/user-create.py rename to django_etebase/admin-cli/management/commands/users-create.py index 0d4bbe1..abc4268 100755 --- a/django_etebase/admin-cli/management/commands/user-create.py +++ b/django_etebase/admin-cli/management/commands/users-create.py @@ -9,10 +9,6 @@ class Command(BaseCommand): parser.add_argument( 'username' , type=str , help="New user's login username." ) - parser.add_argument( '-p' - , '--password' - , type=str - , help="New user's plain text login password." ) parser.add_argument( '-f' , '--first_name' , '--first' @@ -57,16 +53,14 @@ class Command(BaseCommand): def handle(self, *args, **options): try: - user = User.objects.create_user( username = options["username" ] - , password = options["password" ] - , email = options["email" ] - , first_name = options["first_name" ] - , last_name = options["last_name" ] - , is_superuser = options["is_superuser" ] - , is_staff = options["is_staff" ] - , is_active = options["is_active" ] ) - user.save() - except (IntegrityError,Group.DoesNotExist,Permission.DoesNotExist) as exception: + User.objects.create_user( username = options["username" ] + , email = options["email" ] + , first_name = options["first_name" ] + , last_name = options["last_name" ] + , is_active = options["is_active" ] + , is_staff = options["is_staff" ] + , is_superuser = options["is_superuser"] ) + except IntegrityError as exception: self.stdout.write(self.style.ERROR(f'Unable to create user "{options["username"]}": ' + str(exception))) exit(1) diff --git a/django_etebase/admin-cli/management/commands/users-delete.py b/django_etebase/admin-cli/management/commands/users-delete.py new file mode 100755 index 0000000..bc5c56b --- /dev/null +++ b/django_etebase/admin-cli/management/commands/users-delete.py @@ -0,0 +1,33 @@ +from django.core.management.base import BaseCommand +from myauth.models import User +from django.db.models.deletion import ProtectedError + +class Command(BaseCommand): + + def add_arguments(self, parser): + parser.add_argument( 'username' + , type=str + , help="Login username of the user to be deleted." ) + parser.add_argument( '--delete-user-data' + , action='store_true' + , default=False + , help="Delete all user's collections!" ) + + def handle(self, *args, **options): + try: + user = User.objects.get(username = options["username"]) + if options["delete_user_data"]: + collections = user.collection_set.all() + for collection in collections: + collection.delete() + user.delete() + except User.DoesNotExist as exception: + self.stdout.write(self.style.ERROR(f'Unable to delete user "{options["username"]}": ' + str(exception))) + exit(1) + except ProtectedError as exception: + self.stdout.write(self.style.ERROR(f'Unable to delete user "{options["username"]}": ' + str(exception))) + self.stdout.write(self.style.NOTICE('Use --delete-user-data to overcome this protection.')) + exit(2) + + self.stdout.write(self.style.SUCCESS(f'User "{options["username"]}" has been deleted.')) + exit(0) diff --git a/django_etebase/admin-cli/management/commands/users-list.py b/django_etebase/admin-cli/management/commands/users-list.py index 860385e..f4e1402 100755 --- a/django_etebase/admin-cli/management/commands/users-list.py +++ b/django_etebase/admin-cli/management/commands/users-list.py @@ -6,3 +6,4 @@ class Command(BaseCommand): def handle(self, *args, **options): for user in User.objects.all(): print(user.username) + exit(0) diff --git a/django_etebase/admin-cli/management/commands/user-modify.py b/django_etebase/admin-cli/management/commands/users-modify.py similarity index 100% rename from django_etebase/admin-cli/management/commands/user-modify.py rename to django_etebase/admin-cli/management/commands/users-modify.py