Skip to content

Commit c7a0d99

Browse files
committed
fix: 修复用户信息表单管理类
1 parent 6415b1f commit c7a0d99

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

fastapi_user_auth/admin/admin.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import contextlib
2-
from typing import Any, Callable, Dict, List, Type, Union
2+
from typing import Any, Callable, Dict, List, Type
33

44
from fastapi import Depends, HTTPException
55
from fastapi_amis_admin.admin import (
@@ -29,7 +29,7 @@
2929
from fastapi_amis_admin.crud.schema import BaseApiOut
3030
from fastapi_amis_admin.utils.pydantic import model_fields
3131
from fastapi_amis_admin.utils.translation import i18n as _
32-
from pydantic import BaseModel, SecretStr
32+
from pydantic import BaseModel
3333
from sqlalchemy import select
3434
from sqlmodel.sql.expression import Select
3535
from starlette import status
@@ -250,10 +250,9 @@ async def handle(self, request: Request, data: SchemaUpdateT, **kwargs) -> BaseA
250250
if k == "password":
251251
if not v:
252252
continue
253-
else:
254-
v = request.auth.pwd_context.hash(v) # 密码hash保存
253+
v = request.auth.get_password_hash(v)
255254
setattr(request.user, k, v)
256-
return BaseApiOut(data=self.schema_submit_out.parse_obj(request.user))
255+
return BaseApiOut(data=request.user.dict(exclude={"password"}))
257256

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

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

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

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

326319
class RoleAdmin(AutoTimeModelAdmin, FootableModelAdmin):
327320
unique_id = "Auth>RoleAdmin"

fastapi_user_auth/auth/auth.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,11 @@ async def request_login(self, request: Request, response: Response, username: st
319319
response.set_cookie("Authorization", f"bearer {token_info.access_token}")
320320
return BaseApiOut(code=0, data=token_info)
321321

322+
def get_password_hash(self, password: Union[str, SecretStr]) -> str:
323+
if isinstance(password, SecretStr):
324+
password = password.get_secret_value()
325+
return self.pwd_context.hash(password) if password else ""
326+
322327

323328
class AuthRouter(RouterMixin):
324329
auth: Auth = None

0 commit comments

Comments
 (0)