|
| 1 | +# Copyright (c) 2023-present Plane Software, Inc. and contributors |
| 2 | +# SPDX-License-Identifier: AGPL-3.0-only |
| 3 | +# See the LICENSE file for details. |
| 4 | + |
| 5 | +# Django imports |
| 6 | +from django.core.management import BaseCommand, CommandError |
| 7 | + |
| 8 | +# Module imports |
| 9 | +from plane.db.models import User, Workspace, WorkspaceMember |
| 10 | + |
| 11 | + |
| 12 | +class Command(BaseCommand): |
| 13 | + help = "Reactivate a workspace member given a workspace slug and user email" |
| 14 | + |
| 15 | + def add_arguments(self, parser): |
| 16 | + # Positional arguments |
| 17 | + parser.add_argument("slug", type=str, help="workspace slug") |
| 18 | + parser.add_argument("email", type=str, help="user email") |
| 19 | + |
| 20 | + def handle(self, *args, **options): |
| 21 | + # get the workspace slug and user email from console |
| 22 | + slug = options.get("slug", False) |
| 23 | + email = options.get("email", False) |
| 24 | + |
| 25 | + # raise error if slug is not present |
| 26 | + if not slug: |
| 27 | + raise CommandError("Error: Workspace slug is required") |
| 28 | + |
| 29 | + # raise error if email is not present |
| 30 | + if not email: |
| 31 | + raise CommandError("Error: Email is required") |
| 32 | + |
| 33 | + # filter the user |
| 34 | + user = User.objects.filter(email=email).first() |
| 35 | + |
| 36 | + # Raise error if the user is not present |
| 37 | + if not user: |
| 38 | + raise CommandError(f"Error: User with {email} does not exists") |
| 39 | + |
| 40 | + # filter the workspace |
| 41 | + workspace = Workspace.objects.filter(slug=slug).first() |
| 42 | + |
| 43 | + # Raise error if the workspace is not present |
| 44 | + if not workspace: |
| 45 | + raise CommandError(f"Error: Workspace with slug {slug} does not exists") |
| 46 | + |
| 47 | + # Find the workspace membership (includes inactive members; soft-deleted are excluded by default manager) |
| 48 | + workspace_member = WorkspaceMember.objects.filter(workspace=workspace, member=user).first() |
| 49 | + |
| 50 | + # Raise error if the membership is not present |
| 51 | + if not workspace_member: |
| 52 | + raise CommandError(f"Error: User {email} is not a member of workspace {slug}") |
| 53 | + |
| 54 | + # If already active, report without erroring |
| 55 | + if workspace_member.is_active: |
| 56 | + self.stdout.write( |
| 57 | + self.style.SUCCESS(f"User {email} is already an active member of workspace {slug}") |
| 58 | + ) |
| 59 | + return |
| 60 | + |
| 61 | + # Reactivate the membership |
| 62 | + workspace_member.is_active = True |
| 63 | + workspace_member.save() |
| 64 | + |
| 65 | + self.stdout.write( |
| 66 | + self.style.SUCCESS(f"User {email} reactivated successfully in workspace {slug}") |
| 67 | + ) |
0 commit comments