Skip to content

Instantly share code, notes, and snippets.

@collinsrj
Created February 23, 2021 14:55
Show Gist options
  • Save collinsrj/e16b43c47572812f339bfcd29048b9ef to your computer and use it in GitHub Desktop.
Save collinsrj/e16b43c47572812f339bfcd29048b9ef to your computer and use it in GitHub Desktop.
#Build on the previous assignment for “user authentication.” Define a User class which represents the user of an app. A user should have a username, password, and a collection of permissions. User should define methods to add and remove permissions. Create 1 user with the ‘admin’ permission.
#After authenticating as the admin user, you should be able to add new users and remove existing ones through the use of functions.
#Acceptance Criteria
# 1. Authenticate username and password against an admin account
# 2. Given that authentication is successful;
# a. allow admin to add a new user
# b. allow admin to remove an existing user
# 3. Allow these actions until “exit” is entered into the REPL
# ---
# Define a user class
class User:
def __init__(self, username, password):
self.username = username
self.password = password
self.permissions = set()
def add_permission(self, permission):
self.permissions.add(permission)
admin_user = User("e050246", "abc123")
admin_user.add_permission('admin')
users = [admin_user]
current_user = None
username = input('Enter your username: ')
password = input('Enter your password: ')
# Does this match the admin user?
for user in users:
if user.username == username and user.password == password and "admin" in user.permissions:
# Take input
command = ""
while command != "exit":
print("Enter a command")
print("(1) to create a new user")
print("(2) to remove a user")
command = input()
if command == "1":
# Code to create a new user
new_username = input("Enter the new username: ")
new_password = input(f"Enter the password for {new_username}: ")
new_user = User(new_username, new_password)
users.append(new_user)
print(f"Added {new_username} as a user.")
elif command == "2":
# Code to remove a user
print("Remove a user")
else:
command = "exit"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment