An ebook/comic library service and web client
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.3 KiB

  1. from datetime import datetime
  2. from typing import Optional
  3. from atheneum import app, db
  4. from atheneum.model import User
  5. from atheneum.service import authentication_service
  6. def register(name: str, password: str, role: str) -> User:
  7. password_hash, password_revision = authentication_service.get_password_hash(
  8. password)
  9. new_user = User(
  10. name=name,
  11. role=role,
  12. password_hash=password_hash,
  13. password_revision=password_revision,
  14. creation_time=datetime.now(),
  15. version=0)
  16. db.session.add(new_user)
  17. db.session.commit()
  18. app.logger.info('Registered new user: %s with role: %s', name, role)
  19. return new_user
  20. def delete(user: User) -> bool:
  21. existing_user = db.session.delete(user)
  22. if existing_user is None:
  23. db.session.commit()
  24. return True
  25. return False
  26. def update_last_login_time(user: User):
  27. user.last_login_time = datetime.now()
  28. db.session.commit()
  29. def update_password(user: User, password: str):
  30. password_hash, password_revision = authentication_service.get_password_hash(
  31. password)
  32. user.password_hash = password_hash
  33. user.password_revision = password_revision
  34. db.session.commit()
  35. def find_by_name(name: str) -> Optional[User]:
  36. return User.query.filter_by(name=name).first()