I'm learning OOPs design principles and design patterns. As an exercise, I came up with this model for multiple user authentication using OOPs. I plan to build this on top of a Flask server, but I've just mocked the behavior for now.
Please let me know your thoughts about the implementation design. Thanks in advance.
User Model:
class User:
def __init__(self, name: str = None, ph_no: str = None, username: str = None, password: str = None):
self.username = username
self.password = Utils.encrypt(password)
self.ph_no = ph_no
self.name = name
Utils.persist_in_db(self)
def update_password(self, old_password: str, new_password: str):
if Utils.encrypt(old_password) == self.password:
self.password = Utils.encrypt(new_password)
else:
raise PasswordNotCorrectException()
def authenticate(self, password) -> bool:
return self.password == Utils.encrypt(password)
def __repr__(self):
return f'<User: {OrderedDict(self.__dict__)}>'
Core logic:
import threading
import time
from login_service import LoginService
from models.user import User
class BMSApp:
def __init__(self):
self.mock()
@staticmethod
def mock():
user1 = UserThread(name='Rick',
username='ricky',
ph_no='xxxxx',
password='password1234')
user2 = UserThread(name='Morty',
username='morty',
ph_no='xxxx',
password='lifeisawesome')
user1.start()
user2.start()
user1.join()
user2.join()
class UserThread(threading.Thread):
def __init__(self, **kwargs):
super().__init__()
self.kwargs = kwargs
def run(self) -> None:
BookingStrategy.execute(**self.kwargs)
class BookingStrategy:
@staticmethod
def execute(**kwargs):
login_result = LoginService.login(**kwargs)
if login_result[0]:
print(f"Login succeeded for user {login_result[1]}")
else:
print(f"Login failed for user {login_result[1]}")
Mock Login service:
class LoginService:
@staticmethod
def login(**kwargs):
# TODO: implement proper login functionality
user = User(**kwargs)
return random.choice([(True, user), (False, user)])
Utils.encrypt(old_password) == self.password. Fixed it now. \$\endgroup\$encrypt()implies that there is a matchingdecrypt()function, and if there is, you're still not doing it right. \$\endgroup\$