33from datetime import datetime
44from typing import NoReturn
55
6+ from fast_captcha import text_captcha
67from sqlalchemy import select , update , desc , and_
78from sqlalchemy .ext .asyncio import AsyncSession
89from sqlalchemy .orm import selectinload
@@ -29,11 +30,14 @@ async def update_login_time(self, db: AsyncSession, username: str, login_time: d
2930 await db .commit ()
3031 return user .rowcount
3132
32- async def create (self , db : AsyncSession , create : CreateUser ) -> NoReturn :
33- create .password = await jwt .get_hash_password (create .password )
34- new_user = self .model (** create .dict (exclude = {'roles' }))
33+ async def create (self , db : AsyncSession , obj : CreateUser ) -> NoReturn :
34+ salt = text_captcha (5 )
35+ obj .password = await jwt .get_hash_password (obj .password + salt )
36+ dict_obj = obj .dict (exclude = {'roles' })
37+ dict_obj .update ({'salt' : salt })
38+ new_user = self .model (** dict_obj )
3539 role_list = []
36- for role_id in create .roles :
40+ for role_id in obj .roles :
3741 role_list .append (await db .get (Role , role_id ))
3842 new_user .roles .extend (role_list )
3943 db .add (new_user )
@@ -64,9 +68,9 @@ async def check_email(self, db: AsyncSession, email: str) -> User | None:
6468 mail = await db .execute (select (self .model ).where (self .model .email == email ))
6569 return mail .scalars ().first ()
6670
67- async def reset_password (self , db : AsyncSession , pk : int , password : str ) -> int :
71+ async def reset_password (self , db : AsyncSession , pk : int , password : str , salt : str ) -> int :
6872 user = await db .execute (
69- update (self .model ).where (self .model .id == pk ).values (password = await jwt .get_hash_password (password ))
73+ update (self .model ).where (self .model .id == pk ).values (password = await jwt .get_hash_password (password + salt ))
7074 )
7175 return user .rowcount
7276
@@ -94,6 +98,10 @@ async def get_super(self, db: AsyncSession, user_id: int) -> bool:
9498 user = await self .get (db , user_id )
9599 return user .is_superuser
96100
101+ async def get_staff (self , db : AsyncSession , user_id : int ) -> bool :
102+ user = await self .get (db , user_id )
103+ return user .is_staff
104+
97105 async def get_status (self , db : AsyncSession , user_id : int ) -> bool :
98106 user = await self .get (db , user_id )
99107 return user .status
@@ -109,6 +117,13 @@ async def set_super(self, db: AsyncSession, user_id: int) -> int:
109117 )
110118 return user .rowcount
111119
120+ async def set_staff (self , db : AsyncSession , user_id : int ) -> int :
121+ staff_status = await self .get_staff (db , user_id )
122+ user = await db .execute (
123+ update (self .model ).where (self .model .id == user_id ).values (is_staff = False if staff_status else True )
124+ )
125+ return user .rowcount
126+
112127 async def set_status (self , db : AsyncSession , user_id : int ) -> int :
113128 status = await self .get_status (db , user_id )
114129 user = await db .execute (
0 commit comments