A multipurpose python flask API server and administration SPA
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.

48 lines
1.2 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. pw_hash, pw_revision = authentication_service.get_password_hash(password)
  8. new_user = User(
  9. name=name,
  10. role=role,
  11. password_hash=pw_hash,
  12. password_revision=pw_revision,
  13. creation_time=datetime.now(),
  14. version=0)
  15. db.session.add(new_user)
  16. db.session.commit()
  17. app.logger.info('Registered new user: %s with role: %s', name, role)
  18. return new_user
  19. def delete(user: User) -> bool:
  20. existing_user = db.session.delete(user)
  21. if existing_user is None:
  22. db.session.commit()
  23. return True
  24. return False
  25. def update_last_login_time(user: User) -> None:
  26. user.last_login_time = datetime.now()
  27. db.session.commit()
  28. def update_password(user: User, password: str) -> None:
  29. pw_hash, pw_revision = authentication_service.get_password_hash(
  30. password)
  31. user.password_hash = pw_hash
  32. user.password_revision = pw_revision
  33. db.session.commit()
  34. def find_by_name(name: str) -> Optional[User]:
  35. return User.query.filter_by(name=name).first()