-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthenticator.py
More file actions
95 lines (89 loc) · 3.75 KB
/
authenticator.py
File metadata and controls
95 lines (89 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
'''
This file defines the Authenticator class, which is responsible for authenticating users based on their credentials.
It loads user data, provides methods to authenticate users, and retrieve their roles.
'''
# Local application imports
from User import User
from Admin import Admin
from Student import Student
from Teacher import Teacher
class Authenticator:
def __init__(self, file_path="./data/user_data.txt"):
"""
Initializes the Authenticator and loads user data from the specified file.
Parameters:
file_path (str): The path to the user data file (default is "./data/user_data.txt").
"""
self.file_path = file_path
self.users = []
self.load_users()
def load_users(self):
"""
Load list of users from the ./data/users.txt file
:return: bool (True if successful, False otherwise)
"""
try:
with open(self.file_path, "r", encoding="utf8") as users_f:
users_lines = users_f.readlines()
for line in users_lines:
(fName,
lName,
username,
password,
role,
is_active) = line.strip().split(",")
if role == "AD":
user_obj = Admin(fName=fName,lName=lName,username=username,password=password,role=role,is_active=bool(is_active))
elif role == "TA":
user_obj = Teacher(fName=fName,lName=lName,username=username,password=password,role=role,is_active=bool(is_active))
elif role == "LR":
user_obj = Student(fName=fName,lName=lName,username=username,password=password,role=role,is_active=bool(is_active))
self.users.append(user_obj)
return True
except FileNotFoundError:
print(f"The file \"{self.file_path}\" does not exist!")
return False
def authenticate(self, input_username, input_password):
"""
Logic for authenticating a login procedure
:param input_username: str - username entered by the user
:param input_password: str - password entered by the user
:return: bool
"""
self.users = []
self.load_users()
for user_obj in self.users:
if user_obj.get_username() == input_username:
# If username is found
if (user_obj.get_password() == input_password
and user_obj.get_is_active()):
# Passwords match and account is active
return True
else:
# Authentication fails or account is no longer active
return False
# Account does not exist
return False
def authenticate_get_role(self, input_username, input_password):
"""
Authenticate a user based on input credentials and retrieve their role.
:param input_username: str - username entered by the user
:param input_password: str - password entered by the user
:return: str or None
"""
self.users = []
self.load_users()
for user_obj in self.users:
if user_obj.get_username() == input_username:
# If username is found
if (user_obj.get_password() == input_password
and user_obj.get_is_active()):
# Passwords match and account is active
return user_obj.get_role()
else:
# Authentication fails or account is no longer active
return None
# Account does not exist
return None
if __name__ == "__main__":
pass