Skip to content

Commit 6415b1f

Browse files
committed
fix: 修复了创建和更新用户时对密码的hash处理失败.
1 parent 6438c45 commit 6415b1f

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

fastapi_user_auth/admin/admin.py

Lines changed: 11 additions & 5 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
2+
from typing import Any, Callable, Dict, List, Type, Union
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
32+
from pydantic import BaseModel, SecretStr
3333
from sqlalchemy import select
3434
from sqlmodel.sql.expression import Select
3535
from starlette import status
@@ -307,15 +307,21 @@ class UserAdmin(AuthFieldModelAdmin, AuthSelectModelAdmin, SoftDeleteModelAdmin,
307307

308308
async def on_create_pre(self, request: Request, obj, **kwargs) -> Dict[str, Any]:
309309
data = await super(UserAdmin, self).on_create_pre(request, obj, **kwargs)
310-
data["password"] = request.auth.pwd_context.hash(data["password"]) # 密码hash保存
310+
data["password"] = self.get_password_hash(request, data["password"])
311311
return data
312312

313313
async def on_update_pre(self, request: Request, obj, item_id: List[int], **kwargs) -> Dict[str, Any]:
314314
data = await super(UserAdmin, self).on_update_pre(request, obj, item_id, **kwargs)
315-
if data.get("password"):
316-
data["password"] = request.auth.pwd_context.hash(data["password"]) # 密码hash保存
315+
if data.get("password", None):
316+
data["password"] = self.get_password_hash(request, data["password"])
317317
return data
318318

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+
319325

320326
class RoleAdmin(AutoTimeModelAdmin, FootableModelAdmin):
321327
unique_id = "Auth>RoleAdmin"

0 commit comments

Comments
 (0)