from datetime import datetime from typing import Optional from atheneum import app, db from atheneum.model import User from atheneum.service import authentication_service def register(name: str, password: str, role: str) -> User: password_hash, password_revision = authentication_service.get_password_hash( password) new_user = User( name=name, role=role, password_hash=password_hash, password_revision=password_revision, creation_time=datetime.now(), version=0) db.session.add(new_user) db.session.commit() app.logger.info('Registered new user: %s with role: %s', name, role) return new_user def delete(user: User) -> bool: existing_user = db.session.delete(user) if existing_user is None: db.session.commit() return True return False def update_last_login_time(user: User): user.last_login_time = datetime.now() db.session.commit() def update_password(user: User, password: str): password_hash, password_revision = authentication_service.get_password_hash( password) user.password_hash = password_hash user.password_revision = password_revision db.session.commit() def find_by_name(name: str) -> Optional[User]: return User.query.filter_by(name=name).first()