fooder-api/fooder/controller/user.py

29 lines
883 B
Python

from fooder.controller.base import ModelController
from fooder.domain import User
from fooder.context import Context
from fooder.exc import Unauthorized, NotFound
class UserController(ModelController[User]):
@classmethod
async def session_start(
cls,
ctx: Context,
username: str,
password: str,
) -> "UserController":
try:
obj = await ctx.repo.user.get_by_username(username)
except NotFound:
raise Unauthorized()
if not obj.verify_password(password):
raise Unauthorized()
return cls(ctx, obj)
async def change_password(self, current_password: str, new_password: str) -> None:
if not self.obj.verify_password(current_password):
raise Unauthorized()
self.obj.set_password(new_password)
await self.ctx.repo.user.update(self.obj)