mirror of
https://github.com/zylon-ai/private-gpt.git
synced 2025-12-22 20:12:55 +01:00
77 lines
No EOL
2.3 KiB
Python
77 lines
No EOL
2.3 KiB
Python
from typing import Any, Dict, List, Optional, Union
|
|
|
|
from private_gpt.users.core.security import get_password_hash, verify_password
|
|
from private_gpt.users.crud.base import CRUDBase
|
|
from private_gpt.users.models.user import User
|
|
from private_gpt.users.schemas.user import UserCreate, UserUpdate
|
|
from sqlalchemy.orm import Session
|
|
|
|
|
|
class CRUDUser(CRUDBase[User, UserCreate, UserUpdate]):
|
|
def get_by_email(self, db: Session, *, email: str) -> Optional[User]:
|
|
return db.query(self.model).filter(User.email == email).first()
|
|
|
|
def create(self, db: Session, *, obj_in: UserCreate) -> User:
|
|
db_obj = User(
|
|
email=obj_in.email,
|
|
hashed_password=get_password_hash(obj_in.password),
|
|
fullname=obj_in.fullname,
|
|
)
|
|
db.add(db_obj)
|
|
db.commit()
|
|
db.refresh(db_obj)
|
|
return db_obj
|
|
|
|
def update(
|
|
self,
|
|
db: Session,
|
|
*,
|
|
db_obj: User,
|
|
obj_in: Union[UserUpdate, Dict[str, Any]],
|
|
) -> User:
|
|
if isinstance(obj_in, dict):
|
|
update_data = obj_in
|
|
else:
|
|
update_data = obj_in.dict(exclude_unset=True)
|
|
if "password" in update_data:
|
|
hashed_password = get_password_hash(update_data["password"])
|
|
del update_data["password"]
|
|
update_data["hashed_password"] = hashed_password
|
|
return super().update(db, db_obj=db_obj, obj_in=update_data)
|
|
|
|
def get_multi(
|
|
self, db: Session, *, skip: int = 0, limit: int = 100,
|
|
) -> List[User]:
|
|
return db.query(self.model).offset(skip).limit(limit).all()
|
|
|
|
def authenticate(
|
|
self, db: Session, *, email: str, password: str
|
|
) -> Optional[User]:
|
|
user = self.get_by_email(db, email=email)
|
|
if not user:
|
|
return None
|
|
if not verify_password(password, user.hashed_password):
|
|
return None
|
|
return user
|
|
|
|
def is_active(self, user: User) -> bool:
|
|
return user.is_active
|
|
|
|
def get_by_account_id(
|
|
self,
|
|
db: Session,
|
|
*,
|
|
account_id: int,
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
) -> List[User]:
|
|
return (
|
|
db.query(self.model)
|
|
.filter(User.account_id == account_id)
|
|
.offset(skip)
|
|
.limit(limit)
|
|
.all()
|
|
)
|
|
|
|
|
|
user = CRUDUser(User) |