Skip to content

Commit

Permalink
fix: 修复用户信息表单管理类
Browse files Browse the repository at this point in the history
  • Loading branch information
amisadmin committed Dec 25, 2023
1 parent 6415b1f commit c7a0d99
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
19 changes: 6 additions & 13 deletions fastapi_user_auth/admin/admin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import contextlib
from typing import Any, Callable, Dict, List, Type, Union
from typing import Any, Callable, Dict, List, Type

from fastapi import Depends, HTTPException
from fastapi_amis_admin.admin import (
Expand Down Expand Up @@ -29,7 +29,7 @@
from fastapi_amis_admin.crud.schema import BaseApiOut
from fastapi_amis_admin.utils.pydantic import model_fields
from fastapi_amis_admin.utils.translation import i18n as _
from pydantic import BaseModel, SecretStr
from pydantic import BaseModel
from sqlalchemy import select
from sqlmodel.sql.expression import Select
from starlette import status
Expand Down Expand Up @@ -250,10 +250,9 @@ async def handle(self, request: Request, data: SchemaUpdateT, **kwargs) -> BaseA
if k == "password":
if not v:
continue
else:
v = request.auth.pwd_context.hash(v) # 密码hash保存
v = request.auth.get_password_hash(v)
setattr(request.user, k, v)
return BaseApiOut(data=self.schema_submit_out.parse_obj(request.user))
return BaseApiOut(data=request.user.dict(exclude={"password"}))

async def has_page_permission(self, request: Request, obj: PageSchemaAdmin = None, action: str = None) -> bool:
return await self.site.auth.requires(response=False)(request)
Expand Down Expand Up @@ -307,21 +306,15 @@ class UserAdmin(AuthFieldModelAdmin, AuthSelectModelAdmin, SoftDeleteModelAdmin,

async def on_create_pre(self, request: Request, obj, **kwargs) -> Dict[str, Any]:
data = await super(UserAdmin, self).on_create_pre(request, obj, **kwargs)
data["password"] = self.get_password_hash(request, data["password"])
data["password"] = request.auth.get_password_hash(data["password"])
return data

async def on_update_pre(self, request: Request, obj, item_id: List[int], **kwargs) -> Dict[str, Any]:
data = await super(UserAdmin, self).on_update_pre(request, obj, item_id, **kwargs)
if data.get("password", None):
data["password"] = self.get_password_hash(request, data["password"])
data["password"] = request.auth.get_password_hash(data["password"])
return data

@staticmethod
def get_password_hash(request: Request, password: Union[str, SecretStr]) -> str:
if isinstance(password, SecretStr):
password = password.get_secret_value()
return request.auth.pwd_context.hash(password) # 密码hash保存


class RoleAdmin(AutoTimeModelAdmin, FootableModelAdmin):
unique_id = "Auth>RoleAdmin"
Expand Down
5 changes: 5 additions & 0 deletions fastapi_user_auth/auth/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,11 @@ async def request_login(self, request: Request, response: Response, username: st
response.set_cookie("Authorization", f"bearer {token_info.access_token}")
return BaseApiOut(code=0, data=token_info)

def get_password_hash(self, password: Union[str, SecretStr]) -> str:
if isinstance(password, SecretStr):
password = password.get_secret_value()
return self.pwd_context.hash(password) if password else ""


class AuthRouter(RouterMixin):
auth: Auth = None
Expand Down

0 comments on commit c7a0d99

Please sign in to comment.