def handle(self, *args, **options): """ Execute the command. """ username = options['username'] user_email = options['user_email'] try: user = User.objects.get(username=username, email=user_email) except: error_message = ( 'Could not find a user with specified username and email ' 'address. Make sure you have everything correct before ' 'trying again') logger.error(error_message) raise CommandError(error_message) # lint-amnesty, pylint: disable=raise-missing-from user_model = get_user_model() try: with transaction.atomic(): # Add user to retirement queue. UserRetirementStatus.create_retirement(user) # Unlink LMS social auth accounts UserSocialAuth.objects.filter(user_id=user.id).delete() # Change LMS password & email user.email = get_retired_email_by_email(user.email) user.set_unusable_password() user.save() # TODO: Unlink social accounts & change password on each IDA. # Remove the activation keys sent by email to the user for account activation. Registration.objects.filter(user=user).delete() # Delete OAuth tokens associated with the user. retire_dot_oauth2_models(user) AccountRecovery.retire_recovery_email(user.id) except KeyError: error_message = 'Username not specified {}'.format(user) logger.error(error_message) raise CommandError(error_message) # lint-amnesty, pylint: disable=raise-missing-from except user_model.DoesNotExist: error_message = 'The user "{}" does not exist.'.format( user.username) logger.error(error_message) raise CommandError(error_message) # lint-amnesty, pylint: disable=raise-missing-from except Exception as exc: # pylint: disable=broad-except error_message = '500 error deactivating account {}'.format(exc) logger.error(error_message) raise CommandError(error_message) # lint-amnesty, pylint: disable=raise-missing-from logger.info("User succesfully moved to the retirment pipeline")
def test_retire_recovery_email(self): """ Assert that Account Record for a given user is deleted when `retire_recovery_email` is called """ # Create user and associated recovery email record user = UserFactory() AccountRecoveryFactory(user=user) assert len(AccountRecovery.objects.filter(user_id=user.id)) == 1 # Retire recovery email AccountRecovery.retire_recovery_email(user_id=user.id) # Assert that there is no longer an AccountRecovery record for this user assert len(AccountRecovery.objects.filter(user_id=user.id)) == 0
def create_retirement_request_and_deactivate_account(user): """ Adds user to retirement queue, unlinks social auth accounts, changes user passwords and delete tokens and activation keys """ # Add user to retirement queue. UserRetirementStatus.create_retirement(user) # Unlink LMS social auth accounts UserSocialAuth.objects.filter(user_id=user.id).delete() # Change LMS password & email user.email = get_retired_email_by_email(user.email) user.set_unusable_password() user.save() # TODO: Unlink social accounts & change password on each IDA. # Remove the activation keys sent by email to the user for account activation. Registration.objects.filter(user=user).delete() # Delete OAuth tokens associated with the user. retire_dot_oauth2_models(user) AccountRecovery.retire_recovery_email(user.id)