def get_logs(self): try: if self.user != None and hasattr(self.user, 'get_logs'): self.user.get_logs(self.DB) else: if self.user != None: self.log.addLogEntry(LogEntry(1, self.user.username, "[get-logs] Unauthorized user tried to get the logs")) else: self.log.addLogEntry(LogEntry(1, None, "[get-logs] Someone without an account tried to get the logs")) print("Not allowed!") self.checkForBan() except Exception as e: print(f"Something went wrong.1 \n{e}")
def add_client(self): try: if self.user != None and hasattr(self.user, 'add_client'): self.user.add_client(self.DB, self.CITIES) else: if self.user != None: self.log.addLogEntry(LogEntry(1, self.user.username, "[add-client] Unauthorized user tried to make a new client")) else: self.log.addLogEntry(LogEntry(1, None, "[add-client] Someone without an account tried to make a new user")) print("Not allowed!") self.checkForBan() except Exception as e: print(f"Something went wrong. ")
def new_user(self): try: if self.user != None and hasattr(self.user, 'new_user'): self.user.new_user(self.DB) else: if self.user != None: self.log.addLogEntry(LogEntry(1, self.user.username, "[new-user] Unauthorized user tried to make a new user")) else: self.log.addLogEntry(LogEntry(1, None, "[new-user] Someone without an account tried to make a new user.")) print("Not allowed!") self.checkForBan() except Exception as e: print(f"Something went wrong. ")
def logout(self): if self.user != None: try: self.log.addLogEntry(LogEntry(3, self.user.username, "[logout] User logged out.")) self.user = None self.notPermittedCount = 0 print("You are now logged out") except Exception as e: print(f"Something went wrong. ") else: try: self.log.addLogEntry(LogEntry(1, None, "[logout] User tried to log out while already boing logged out.")) except Exception as e: print(f"Something went wrong. ") print("You are already logged out!")
def validate_password(password, user): """ Password must match: - at least 8 characters - maximum of 30 characters − can contain letters (a-z), (A-Z), numbers (0-9), Special characters such as ~!@#$%^&*_-+=`|\(){}[]:;'<>,.?/. − must have a combination of at least one lowercase letter, one uppercase letter, one digit, and one specialcharacter """ while not re.match( "^(?=(?:.*[A-Z]){1,})(?=(?:.*[a-z]){1,})(?=(?:.*\d){1,})(?=(?:.*[\~\!\@\#\$\%\^\&\*\_\-\+\=\`\|\\\(\)\{\}\[\]\:\;\'\<\>\,\.\?\/]){1,})([A-Za-z0-9\~\!\@\#\$\%\^\&\*\_\-\+\=\`\|\\\(\)\{\}\[\]\:\;\'\<\>\,\.\?\/]{8,30})$", password): try: log.addLogEntry( LogEntry( 2, user, "[validate-password] User tried to create a user with a wrong password." )) print( "Given password is invalid, the password should have: \n" + " - at least 8 characters \n" + " - a maximum of 30 characters \n" + " - can contain letters (a-z), (A-Z), numbers (0-9), Special characters such as ~!@#$%^&*_-+=`|\(){}[]:;'<>,.?/. \n" + " - must have a combination of at least one lowercase letter, one uppercase letter, one digit, and one specialcharacter" ) password = input("Password: "******"Something has changed. ") else: return password
def checkForBan(self): try: self.notPermittedCount += 1 if self.notPermittedCount >= 4: if self.user != None and self.user.ROLE != "Super Administrator": print("You are banned from the system") self.log.addLogEntry(LogEntry(1, self.user.username, "[banned] user got banned")) self.DB.banUser(self.user.username) self.logout() elif self.user == None: self.log.addLogEntry(LogEntry(1, None, "[Kicked] Someone with no account got kicked for unauthorized command abuse")) print("You are being kicked for unauthorized command abuse") print("Closing system...") exit() except Exception as e: print(f"Something went wrong. ")
def add_client(self, db, cities): print('Adding a new client') fullName = input('Full name: ').translate(str.maketrans( '', '', '\x00')) email = input('Email: ').translate(str.maketrans('', '', '\x00')) email = validate_email(email, self.username) mobilePhone = input('Mobile phone: ').translate( str.maketrans('', '', '\x00')) mobilePhone = validate_phone(mobilePhone, self.username) streetHouseNumber = input('Street & house number: ').translate( str.maketrans('', '', '\x00')) streetHouseNumber = validate_streethouse(streetHouseNumber, self.username) zipcode = input('Zipcode: ').upper().translate( str.maketrans('', '', '\x00')) zipcode = validate_zipcode(zipcode, self.username) print('Available cities: ' + ', '.join(cities)) city = input('City: ') city = validate_city(city, cities, self.username) try: newClient = db.addClient((fullName, email, mobilePhone, streetHouseNumber, zipcode, city)) log.addLogEntry( LogEntry(3, self.username, f"[add-client] Client {fullName} added.")) print( f"Added new client {newClient.fullName} ({newClient.emailAddress})" ) except Exception as e: try: log.addLogEntry( LogEntry(2, self.username, "[add-client] Failed to add client.")) print('Failed to add a new client') except Exception as ex: print(f"Something went wrong. ")
def validate_city(city, cities, user): while not city in cities: try: log.addLogEntry( LogEntry( 2, user, "[validate-city] User tried to create a client with a wrong city." )) print("Invalid city, choose from: " + ", ".join(cities)) city = input("City: ").translate(str.maketrans('', '', '\x00')) except Exception as e: print(f"Something went wrong. ") else: return city
def new_user(self, db): print("Creating a new Advisor") username = input('Username: '******'', '', '\x00')) username = validate_username(username, self.username) password = input('Password: '******'', '', '\x00')) password = validate_password(password, self.username) try: log.addLogEntry( LogEntry(3, self.username, f"[new-user] User added with username: {username}")) newUser = db.addUser((username, password, "Advisor")) print(f"Created new user {newUser.username} ({newUser.ROLE})") except Exception as e: try: log.addLogEntry( LogEntry(2, self.username, "[new-user] Failed to create user")) print('Failed to create user') except Exception as ex: print(f"Something went wrong. ")
def validate_priority(priority, user): while not re.match("^[123]$", priority): try: log.addLogEntry( LogEntry( 2, user, "[validate-priority] User tried to get logs with a wrong priority." )) print("Invalid priority, pick either 1, 2 or 3") priority = input("Minimum priority: ").translate( str.maketrans('', '', '\x00')) except Exception as e: print(f"Something went wrong. ") else: return priority
def validate_zipcode(zipcode, user): while not re.match("^\d{4}[A-Z]{2}$", zipcode): try: log.addLogEntry( LogEntry( 2, user, "[validate-zipcode] User tried to create a client with a wrong zipcode." )) print("Invalid zipcode: it should look like '1234AB'") zipcode = input("Zipcode: ").upper().translate( str.maketrans('', '', '\x00')) except Exception as e: print(f"Something went wrong. ") else: return zipcode
def validate_email(email, user): while not re.match("^[\w.+-]+@[\w.-]+\.\w{2,}$", email): try: log.addLogEntry( LogEntry( 2, user, "[validate-email] User tried to create a client with a wrong email address" )) print( "Invalid email address, it should look like '*****@*****.**'" ) email = input("Email: ").translate(str.maketrans('', '', '\x00')) except Exception as e: print(f"Something went wrong. ") else: return email
def validate_phone(mobilePhone, user): while not re.match("^\+31-6-[\d]{4}-[\d]{4}$", mobilePhone): try: log.addLogEntry( LogEntry( 2, user, "[validate-phone] User tried to create a client with a wrong phone number" )) print( "Invalid phone number, it should look like '+31-6-0123-4567'") mobilePhone = input("Mobile phone: ").translate( str.maketrans('', '', '\x00')) except Exception as e: print(f"Something went wrong. ") else: return mobilePhone
def validate_streethouse(streetHouse, user): while not re.match("^[\w ]+ [\d]+[a-z]?$", streetHouse): try: log.addLogEntry( LogEntry( 2, user, "[validate-streethouse] User tried to create a client with a wrong address (street and house number)" )) print( "Invalid street & house number: it should look like 'Streetname 12'" ) streetHouse = input("Street & house number: ").translate( str.maketrans('', '', '\x00')) except Exception as e: print(f"Something went wrong. ") else: return streetHouse
def validate_username(username, user): while not re.match("^[a-z]{1}[a-z0-9\.\-\_\']{4,19}$", username): try: log.addLogEntry( LogEntry( 2, user, "[validate-username] User tried to create a user with a wrong username." )) print( "Given username is invalid:\n" + " - The username must be between 5 and 20 characters long\n" + " - Start with letter\n" + " - Contain only letters, numbers and .-_\' characters") username = input("Username: "******"Something went wrong. ") else: return username
def login(self): if self.user == None: username = input("Username: "******"Password: "******"Logged in as {self.user.username} ({self.user.ROLE})") self.log.addLogEntry(LogEntry(3, username, "[login] User logged in")) except Exception as e: if isinstance(e, PasswordException) or isinstance(e, UsernameException): self.wrongLoginCount += 1 try: self.log.addLogEntry(LogEntry(2, username, "[login] User tried to log in with wrong credentials")) print("Wrong username or password!") except Exception as ex: print(f"Something went wrong. ") if self.wrongLoginCount >= 5: try: print("You are being kicked for login abuse") self.log.addLogEntry(LogEntry(2, username, "[login] User kicked after logging in with wrong credentials 5 times")) print("Closing system...") exit() except Exception as ex: print(f"something went wrong. ") elif isinstance(e, BannedException): try: self.log.addLogEntry(LogEntry(2, username, "[login] Banned user tried to log in")) print("You are banned!") except Exception as ex: print(f"Something went wrong. ") else: try: self.log.addLogEntry(LogEntry(1, username, f"[login] Error occured when trying to log in. ")) print("Something went wrong") except Exception as ex: print(f"Something went wrong ") else: try: self.log.addLogEntry(LogEntry(1, username, "[login] User tried to log in while already being logged in.")) print(f"Already logged in as {self.user.username} ({self.user.ROLE})") except Exception as ex: print(f"Something went wrong. ")