Coverage for compiler_admin / commands / user / deactivate.py: 100%

39 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-04-28 05:48 +0000

1import click 

2 

3from compiler_admin import Result 

4from compiler_admin.commands.user.reset import reset 

5from compiler_admin.services.google import GoogleAccount, GoogleOrgs, GoogleUsers 

6 

7 

8@click.command() 

9@click.option("-f", "--force", is_flag=True, help="Don't ask for confirmation.") 

10@click.option("-n", "--notify", help="An email address to send the new password notification.") 

11@click.option( 

12 "-e", 

13 "--recovery-email", 

14 default="", 

15 help="An email address to use as the new recovery email. Without a value, clears the recovery email.", 

16) 

17@click.option( 

18 "-p", 

19 "--recovery-phone", 

20 default="", 

21 help="A phone number to use as the new recovery phone number. Without a value, clears the recovery phone number.", 

22) 

23@click.argument("username") 

24@click.pass_context 

25def deactivate( 

26 ctx: click.Context, username: str, force: bool = False, recovery_email: str = "", recovery_phone: str = "", **kwargs 

27): 

28 """Deactivate (but do not delete) a user.""" 

29 account = GoogleAccount(username) 

30 google_users = GoogleUsers() 

31 google_orgs = GoogleOrgs() 

32 

33 if not account.exists(): 

34 click.echo(f"User does not exist: {account}") 

35 raise SystemExit(Result.FAILURE) 

36 

37 if account.is_deactivated(): 

38 click.echo("User is already deactivated") 

39 raise SystemExit(Result.FAILURE) 

40 

41 if not force: 

42 cont = input(f"Deactivate account {account}? (Y/n): ") 

43 if not cont.lower().startswith("y"): 

44 click.echo("Aborting deactivation") 

45 raise SystemExit(Result.SUCCESS) 

46 

47 click.echo(f"User exists, deactivating: {account}") 

48 

49 click.echo(f"Moving to OU: {GoogleOrgs.OU_ALUMNI}") 

50 google_orgs.move_user(account, GoogleOrgs.OU_ALUMNI) 

51 

52 click.echo("Removing from groups") 

53 google_users.remove_from_groups(account) 

54 

55 # reset password, sign out 

56 ctx.forward(reset) 

57 

58 click.echo("Clearing user profile info") 

59 google_users.clear_profile(account) 

60 

61 click.echo("Resetting recovery email and phone") 

62 google_users.reset_recovery_info(account=account, recovery_email=recovery_email, recovery_phone=recovery_phone) 

63 

64 click.echo("Turning off 2FA") 

65 google_users.disable_2fa(account) 

66 

67 click.echo(f"User is deactivated: {account}")