Last active
December 4, 2021 00:21
-
-
Save robert-mcdermott/24ba4f7b62c88c4fc814afbed30405c0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
Alternate account naming rules | |
Rule 1: firstname + lastname is less 18 or less characters long total | |
== firstname + "." + lastname (john.doe) | |
if conflict, check if rule 2 format (below) is available. If so use it | |
== firstname_initial + "." + lastname (j.doe) | |
if not, append an integer (starting with 2) to the end of the username | |
=== firstname + "." + lastname + int (john.doe2) | |
Rule 2: firstname + lastname is greater than 18 characters long total *and* | |
firstname_initial + lastname <= 18 characters long total | |
== firstname_initial + "." + lastname (jimmy reallylongname = j.reallylongname) | |
if conflict append an integer (starting with 2) to the end of the username | |
== firstname_initial + "." + lastname (johnny reallylongname = j.reallylongname2) | |
Rule 3: firstname + lastname is greater than 18 characters long total *and* | |
firstname_initial + lastname > 18 characters long total | |
== firstname_initial + "." + lastname[truncated to 17 chars] (jimmy reallyverylongname = j.reallyverylongnam) | |
if conflict append an integer (starting with 2) to the end of the username | |
== firstname_initial + "." + lastname[truncated to 17 chars] (jimbo reallyverylongname = j.reallyverylongnam2) | |
""" | |
import sys | |
def generate(first_name, last_name, email_domain): | |
""" | |
Take a first name, last name and based on the defined rules print the new users | |
unique username and email address. | |
""" | |
first_name = first_name.replace(" ", "").lower() | |
last_name = last_name.replace(" ", "").lower() | |
email_domain = email_domain.lower() | |
# Handling of users with 18 or less characters in their first+last name. | |
if len(first_name + last_name) <= 18: | |
username = "{}.{}".format(first_name, last_name) | |
# If the username already exists, check if the alternate format is available | |
# and use if there is no conflict. If it's also already in use append an integer | |
if username in existing_usernames: | |
username_alt = "{}.{}".format(first_name[0], last_name) | |
if username_alt not in existing_usernames: | |
username = dupe_fix("{}.{}".format(first_name[0], last_name)) | |
else: | |
username = dupe_fix("{}.{}".format(first_name, last_name)) | |
print("sAMAccountName: {}".format(username)) | |
print("Email Address: {}@{}".format(username, email_domain)) | |
# Handing of users with 19 more more characters in the first+last name | |
elif len(first_name[0] + last_name) <=18: | |
username = dupe_fix("{}.{}".format(first_name[0], last_name)) | |
print("sAMAccountName: {}".format(username)) | |
print("Email Address: {}@{}".format(username, email_domain)) | |
# Really long names that need to have their last name truncated | |
else: | |
username = dupe_fix("{}.{}".format(first_name[0], last_name[:17])) | |
print("sAMAccountName: {}".format(username)) | |
print("Email Address: {}@{}".format(username, email_domain)) | |
def dupe_fix(username): | |
""" | |
Check to see if the generanted username is an existing username and if so generate | |
a non conflicting username by appeneding an integer until if finds a unique username. | |
""" | |
conflict = 0 | |
while True: | |
if username not in existing_usernames: | |
break | |
conflict += 1 | |
if username[-1].isdigit(): | |
username = "{}{}".format(username[:-1], conflict + 1) | |
else: | |
username = "{}{}".format(username, conflict + 1) | |
return(username) | |
# This is just to demo username collision detection, in production you'd need to retrive | |
# your alias/username list externally from this script | |
existing_usernames = ['john.doe', | |
'j.doe', | |
'sally.doe', | |
'robert.mcdermott', | |
'robert.mcdermott2', | |
'c.trakarnsilpa', | |
'j.reallylongname', | |
'j.reallylongerlastn'] | |
if __name__ == "__main__": | |
if len(sys.argv) < 3: | |
print("Usage: {} firstname lastname".format(sys.argv[0])) | |
sys.exit(1) | |
email_domain = "fredhutch.org" | |
first_name = sys.argv[1] | |
last_name = sys.argv[2] | |
generate(first_name, last_name, email_domain) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment