|
| 1 | +import click |
| 2 | + |
| 3 | +from compiler_admin import RESULT_FAILURE, RESULT_SUCCESS |
| 4 | +from compiler_admin.commands.user.backupcodes import backupcodes |
| 5 | +from compiler_admin.commands.user.reset import reset |
| 6 | +from compiler_admin.services.google import ( |
| 7 | + GROUP_STAFF, |
| 8 | + GROUP_TEAM, |
| 9 | + OU_CONTRACTORS, |
| 10 | + OU_STAFF, |
| 11 | + CallGAMCommand, |
| 12 | + add_user_to_group, |
| 13 | + move_user_ou, |
| 14 | + user_account_name, |
| 15 | + user_exists, |
| 16 | + user_is_deactivated, |
| 17 | +) |
| 18 | + |
| 19 | + |
| 20 | +@click.command() |
| 21 | +@click.option("-f", "--force", is_flag=True, help="Don't ask for confirmation.") |
| 22 | +@click.option("-n", "--notify", help="An email address to send the new password notification.") |
| 23 | +@click.option( |
| 24 | + "-e", |
| 25 | + "--recovery-email", |
| 26 | + default="", |
| 27 | + help="An email address to use as the new recovery email.", |
| 28 | +) |
| 29 | +@click.option( |
| 30 | + "-p", |
| 31 | + "--recovery-phone", |
| 32 | + default="", |
| 33 | + help="A phone number to use as the new recovery phone number.", |
| 34 | +) |
| 35 | +@click.option("-s", "--staff", is_flag=True, help="Reactivate the user as a staff member. The default is contractor.") |
| 36 | +@click.argument("username") |
| 37 | +@click.pass_context |
| 38 | +def reactivate( |
| 39 | + ctx: click.Context, |
| 40 | + username: str, |
| 41 | + force: bool = False, |
| 42 | + recovery_email: str = "", |
| 43 | + recovery_phone: str = "", |
| 44 | + staff: bool = False, |
| 45 | + **kwargs, |
| 46 | +): |
| 47 | + """ |
| 48 | + Reactivate a previously deactivated user. |
| 49 | + """ |
| 50 | + account = user_account_name(username) |
| 51 | + |
| 52 | + if not user_exists(account): |
| 53 | + click.echo(f"User does not exist: {account}") |
| 54 | + raise SystemExit(RESULT_FAILURE) |
| 55 | + |
| 56 | + if not user_is_deactivated(account): |
| 57 | + click.echo("User is not deactivated, cannot reactivate") |
| 58 | + raise SystemExit(RESULT_FAILURE) |
| 59 | + |
| 60 | + if not force: |
| 61 | + cont = input(f"Reactivate account {account}? (Y/n): ") |
| 62 | + if not cont.lower().startswith("y"): |
| 63 | + click.echo("Aborting reactivation") |
| 64 | + raise SystemExit(RESULT_SUCCESS) |
| 65 | + |
| 66 | + click.echo(f"User exists, reactivating: {account}") |
| 67 | + |
| 68 | + click.echo(f"Adding to group: {GROUP_TEAM}") |
| 69 | + add_user_to_group(account, GROUP_TEAM) |
| 70 | + |
| 71 | + if staff: |
| 72 | + click.echo(f"Moving to OU: {OU_STAFF}") |
| 73 | + move_user_ou(account, OU_STAFF) |
| 74 | + click.echo(f"Adding to group: {GROUP_STAFF}") |
| 75 | + add_user_to_group(account, GROUP_STAFF) |
| 76 | + else: |
| 77 | + click.echo(f"Moving to OU: {OU_CONTRACTORS}") |
| 78 | + move_user_ou(account, OU_CONTRACTORS) |
| 79 | + |
| 80 | + # reset password, sign out |
| 81 | + ctx.forward(reset) |
| 82 | + |
| 83 | + click.echo("Update user profile info") |
| 84 | + profile = dict(recoveryemail=recovery_email, recoveryphone=recovery_phone) |
| 85 | + profile = {k: v for k, v in profile.items() if v} |
| 86 | + for prop, val in profile.items(): |
| 87 | + command = ("update", "user", account, prop, val) |
| 88 | + CallGAMCommand(command) |
| 89 | + |
| 90 | + # get the user's backup codes |
| 91 | + ctx.forward(backupcodes) |
| 92 | + |
| 93 | + click.echo(f"User is reactivated: {account}") |
0 commit comments