mirror of
https://github.com/avatao-content/test-tutorial-framework
synced 2025-07-06 08:46:23 +00:00
Perform mercilless separation of bussiness logic from request handling
This commit is contained in:
28
solvable/src/webservice/user_ops.py
Normal file
28
solvable/src/webservice/user_ops.py
Normal file
@ -0,0 +1,28 @@
|
||||
from crypto import PasswordHasher
|
||||
from model import Session, User
|
||||
from exceptions import InvalidCredentialsError, UserExistsError
|
||||
|
||||
|
||||
class UserOps:
|
||||
def __init__(self, username, password):
|
||||
self.username = username
|
||||
self.password = password
|
||||
|
||||
def authenticate(self):
|
||||
with Session() as db:
|
||||
|
||||
user = db.query(User).filter(User.username == self.username).first()
|
||||
|
||||
if not user or not PasswordHasher.verify(self.password, user.passwordhash):
|
||||
raise InvalidCredentialsError
|
||||
|
||||
def register(self):
|
||||
with Session() as db:
|
||||
|
||||
if db.query(User).filter(User.username == self.username).all():
|
||||
raise UserExistsError
|
||||
|
||||
user = User(username=self.username,
|
||||
passwordhash=PasswordHasher.hash(self.password))
|
||||
db.add(user)
|
||||
db.commit()
|
Reference in New Issue
Block a user