|
1 | | -from argparse import Namespace |
| 1 | +import click |
2 | 2 |
|
3 | 3 | from compiler_admin import RESULT_FAILURE, RESULT_SUCCESS |
4 | 4 | from compiler_admin.commands.user.reset import reset |
5 | 5 | from compiler_admin.services.google import ( |
6 | 6 | OU_ALUMNI, |
7 | | - USER_HELLO, |
8 | 7 | CallGAMCommand, |
9 | 8 | move_user_ou, |
10 | 9 | user_account_name, |
11 | 10 | user_exists, |
12 | 11 | ) |
13 | 12 |
|
14 | 13 |
|
15 | | -def alumni(args: Namespace) -> int: |
16 | | - """Convert a user to a Compiler alumni. |
17 | | -
|
18 | | - Optionally notify an email address with the new randomly generated password. |
19 | | -
|
20 | | - Args: |
21 | | - username (str): the user account to convert. |
22 | | -
|
23 | | - notify (str): an email address to send the new password notification. |
24 | | - Returns: |
25 | | - A value indicating if the operation succeeded or failed. |
| 14 | +@click.command() |
| 15 | +@click.option("-f", "--force", is_flag=True, help="Don't ask for confirmation.") |
| 16 | +@click.option("-n", "--notify", help="An email address to send the new password notification.") |
| 17 | +@click.option( |
| 18 | + "-e", |
| 19 | + "--recovery-email", |
| 20 | + help="An email address to use as the new recovery email. Without a value, clears the recovery email.", |
| 21 | +) |
| 22 | +@click.option( |
| 23 | + "-p", |
| 24 | + "--recovery-phone", |
| 25 | + help="A phone number to use as the new recovery phone number. Without a value, clears the recovery phone number.", |
| 26 | +) |
| 27 | +@click.argument("username") |
| 28 | +@click.pass_context |
| 29 | +def alumni( |
| 30 | + ctx: click.Context, username: str, force: bool = False, recovery_email: str = "", recovery_phone: str = "", **kwargs |
| 31 | +): |
26 | 32 | """ |
27 | | - if not hasattr(args, "username"): |
28 | | - raise ValueError("username is required") |
29 | | - |
30 | | - account = user_account_name(args.username) |
| 33 | + Convert a user to a Compiler alumni. |
| 34 | + """ |
| 35 | + account = user_account_name(username) |
31 | 36 |
|
32 | 37 | if not user_exists(account): |
33 | | - print(f"User does not exist: {account}") |
34 | | - return RESULT_FAILURE |
| 38 | + click.echo(f"User does not exist: {account}") |
| 39 | + raise SystemExit(RESULT_FAILURE) |
35 | 40 |
|
36 | | - if getattr(args, "force", False) is False: |
37 | | - cont = input(f"Convert account to alumni: {account}? (Y/n) ") |
| 41 | + if not force: |
| 42 | + cont = input(f"Convert account to alumni for {account}? (Y/n): ") |
38 | 43 | if not cont.lower().startswith("y"): |
39 | | - print("Aborting conversion.") |
40 | | - return RESULT_SUCCESS |
| 44 | + click.echo("Aborting conversion.") |
| 45 | + raise SystemExit(RESULT_SUCCESS) |
41 | 46 |
|
42 | | - res = RESULT_SUCCESS |
| 47 | + click.echo(f"User exists, converting to alumni: {account}") |
43 | 48 |
|
44 | | - print("Removing from groups") |
45 | | - res += CallGAMCommand(("user", account, "delete", "groups")) |
| 49 | + click.echo("Removing from groups") |
| 50 | + CallGAMCommand(("user", account, "delete", "groups")) |
46 | 51 |
|
47 | | - print(f"Moving to OU: {OU_ALUMNI}") |
48 | | - res += move_user_ou(account, OU_ALUMNI) |
| 52 | + click.echo(f"Moving to OU: {OU_ALUMNI}") |
| 53 | + move_user_ou(account, OU_ALUMNI) |
49 | 54 |
|
50 | 55 | # reset password, sign out |
51 | | - res += reset(args) |
| 56 | + ctx.forward(reset) |
52 | 57 |
|
53 | | - print("Clearing user profile info") |
| 58 | + click.echo("Clearing user profile info") |
54 | 59 | for prop in ["address", "location", "otheremail", "phone"]: |
55 | 60 | command = ("update", "user", account, prop, "clear") |
56 | | - res += CallGAMCommand(command) |
| 61 | + CallGAMCommand(command) |
57 | 62 |
|
58 | | - print("Resetting recovery email") |
59 | | - recovery = getattr(args, "recovery_email", "") |
60 | | - command = ("update", "user", account, "recoveryemail", recovery) |
61 | | - res += CallGAMCommand(command) |
| 63 | + click.echo("Resetting recovery email") |
| 64 | + command = ("update", "user", account, "recoveryemail", recovery_email) |
| 65 | + CallGAMCommand(command) |
62 | 66 |
|
63 | | - print("Resetting recovery phone") |
64 | | - recovery = getattr(args, "recovery_phone", "") |
65 | | - command = ("update", "user", account, "recoveryphone", recovery) |
66 | | - res += CallGAMCommand(command) |
| 67 | + click.echo("Resetting recovery phone") |
| 68 | + command = ("update", "user", account, "recoveryphone", recovery_phone) |
| 69 | + CallGAMCommand(command) |
67 | 70 |
|
68 | | - print("Turning off 2FA") |
| 71 | + click.echo("Turning off 2FA") |
69 | 72 | command = ("user", account, "turnoff2sv") |
70 | | - res += CallGAMCommand(command) |
71 | | - |
72 | | - print("Resetting email signature") |
73 | | - # https://github.com/taers232c/GAMADV-XTD3/wiki/Users-Gmail-Send-As-Signature-Vacation#manage-signature |
74 | | - command = ( |
75 | | - "user", |
76 | | - account, |
77 | | - "signature", |
78 | | - f"Compiler LLC<br />https://compiler.la<br />{USER_HELLO}", |
79 | | - "replyto", |
80 | | - USER_HELLO, |
81 | | - "default", |
82 | | - "treatasalias", |
83 | | - "false", |
84 | | - "name", |
85 | | - "Compiler LLC", |
86 | | - "primary", |
87 | | - ) |
88 | | - res += CallGAMCommand(command) |
89 | | - |
90 | | - print("Turning on email autoresponder") |
91 | | - # https://github.com/taers232c/GAMADV-XTD3/wiki/Users-Gmail-Send-As-Signature-Vacation#manage-vacation |
92 | | - message = ( |
93 | | - "Thank you for contacting Compiler. This inbox is no longer actively monitored.<br /><br />" |
94 | | - + f"Please reach out to {USER_HELLO} if you need to get a hold of us." |
95 | | - ) |
96 | | - command = ( |
97 | | - "user", |
98 | | - account, |
99 | | - "vacation", |
100 | | - "true", |
101 | | - "subject", |
102 | | - "[This inbox is no longer active]", |
103 | | - "message", |
104 | | - message, |
105 | | - "contactsonly", |
106 | | - "false", |
107 | | - "domainonly", |
108 | | - "false", |
109 | | - "start", |
110 | | - "Started", |
111 | | - "end", |
112 | | - "2999-12-31", |
113 | | - ) |
114 | | - res += CallGAMCommand(command) |
115 | | - |
116 | | - return res |
| 73 | + CallGAMCommand(command) |
0 commit comments