password_history.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. """密码历史CRUD"""
  2. from typing import Optional, List
  3. from sqlalchemy.orm import Session
  4. from app.models.models import SysPasswordHistory
  5. from app.crud.base import CRUDBase
  6. from app.core.utils import generate_id
  7. class PasswordHistoryCRUD(CRUDBase[SysPasswordHistory]):
  8. """密码历史CRUD操作"""
  9. def create_password_history(
  10. self,
  11. db: Session,
  12. user_id: str,
  13. operation_type: str,
  14. operator_id: str = None,
  15. operator_name: str = None,
  16. ip_address: str = None
  17. ) -> SysPasswordHistory:
  18. """创建密码修改历史"""
  19. history = SysPasswordHistory(
  20. id=generate_id(),
  21. user_id=user_id,
  22. operation_type=operation_type,
  23. operator_id=operator_id,
  24. operator_name=operator_name,
  25. ip_address=ip_address
  26. )
  27. db.add(history)
  28. db.commit()
  29. db.refresh(history)
  30. return history
  31. def get_user_history(self, db: Session, user_id: str, limit: int = 5) -> List[SysPasswordHistory]:
  32. """获取用户密码修改历史"""
  33. return db.query(SysPasswordHistory).filter(
  34. SysPasswordHistory.user_id == user_id,
  35. SysPasswordHistory.is_del == 0
  36. ).order_by(SysPasswordHistory.create_at.desc()).limit(limit).all()
  37. password_history_crud = PasswordHistoryCRUD(SysPasswordHistory)