From 50825b574b4147f9f8c50cce6a379d956882031a Mon Sep 17 00:00:00 2001 From: Bavisetti Narayan <72156168+NarayanBavisetti@users.noreply.github.com> Date: Tue, 16 Apr 2024 18:06:46 +0530 Subject: [PATCH] chore: instance admin script (#4210) --- .../commands/create_instance_admin.py | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 apiserver/plane/db/management/commands/create_instance_admin.py diff --git a/apiserver/plane/db/management/commands/create_instance_admin.py b/apiserver/plane/db/management/commands/create_instance_admin.py new file mode 100644 index 000000000..21f79c15e --- /dev/null +++ b/apiserver/plane/db/management/commands/create_instance_admin.py @@ -0,0 +1,48 @@ +# Django imports +from django.core.management.base import BaseCommand, CommandError + +# Module imports +from plane.license.models import Instance, InstanceAdmin +from plane.db.models import User + + +class Command(BaseCommand): + help = "Add a new instance admin" + + def add_arguments(self, parser): + # Positional argument + parser.add_argument( + "admin_email", type=str, help="Instance Admin Email" + ) + + def handle(self, *args, **options): + + admin_email = options.get("admin_email", False) + + if not admin_email: + raise CommandError("Please provide the email of the admin.") + + user = User.objects.filter(email=admin_email).first() + if user is None: + raise CommandError("User with the provided email does not exist.") + + try: + # Get the instance + instance = Instance.objects.last() + + # Get or create an instance admin + _, created = InstanceAdmin.objects.get_or_create( + user=user, instance=instance, role=20 + ) + + if not created: + raise CommandError( + "The provided email is already an instance admin." + ) + + self.stdout.write( + self.style.SUCCESS("Successfully created the admin") + ) + except Exception as e: + print(e) + raise CommandError("Failed to create the instance admin.")