From 0d8b370b5d6a5a54635ad52f71bfbe2a669a208d Mon Sep 17 00:00:00 2001 From: nate Date: Fri, 23 Feb 2018 13:58:54 -0800 Subject: [PATCH] write rename_user standalone script --- scripts/rename_user.py | 37 +++++++++++++++++++++++++++++++++++++ ttadmin/users/forms.py | 1 + 2 files changed, 38 insertions(+) create mode 100644 scripts/rename_user.py diff --git a/scripts/rename_user.py b/scripts/rename_user.py new file mode 100644 index 0000000..30497e7 --- /dev/null +++ b/scripts/rename_user.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python3 +"""This script wraps the usermod command to allow user account renames via sudoers.""" +import os +import sys +import subprocess + + +def rename_user(username, new_username): + # usermod -l new_username -m -d /home/{new_username} username + args = [ + 'usermod', + '-l', + new_username, + '-m', + '-d', + os.path.join('/home', new_username), + username + ] + subprocess.run(args, check=True) + + +def main(argv): + if len(argv) < 3: + print('[rename_user] Too few arguments passed.', file=sys.stderr) + return 1 + + try: + rename_user(argv[1], argv[2]) + except subprocess.CalledProcessError as e: + print('[rename_user] {}'.format(e), file=sys.stderr) + return 2 + + return 0 + + +if __name__ == '__main__': + exit(main(sys.argv)) diff --git a/ttadmin/users/forms.py b/ttadmin/users/forms.py index 0dcd555..9b29d3e 100644 --- a/ttadmin/users/forms.py +++ b/ttadmin/users/forms.py @@ -34,6 +34,7 @@ def validate_displayname(display_name): if not DISPLAY_NAME_RE.match(display_name): raise ValidationError("Valid characters: a-z, A-Z, 0-9, -, _, and '.") + def validate_pubkey(pubkey): # TODO see if I can get the type out key = ssh.SSHKey(pubkey, strict_mode=False, skip_option_parsing=True)