Skip to content

Commit 10c0c69

Browse files
authored
Update user email and phone operation logic (#654)
1 parent 0bc3786 commit 10c0c69

File tree

5 files changed

+34
-79
lines changed

5 files changed

+34
-79
lines changed

backend/app/admin/api/v1/sys/user.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
AddUserParam,
1010
GetCurrentUserInfoWithRelationDetail,
1111
GetUserInfoWithRelationDetail,
12-
RegisterUserParam,
1312
ResetPasswordParam,
1413
UpdateUserParam,
1514
)
@@ -24,12 +23,6 @@
2423
router = APIRouter()
2524

2625

27-
@router.post('/register', summary='注册用户')
28-
async def register_user(obj: RegisterUserParam) -> ResponseModel:
29-
await user_service.register(obj=obj)
30-
return response_base.success()
31-
32-
3326
@router.post('/add', summary='添加用户', dependencies=[DependsRBAC])
3427
async def add_user(request: Request, obj: AddUserParam) -> ResponseSchemaModel[GetUserInfoWithRelationDetail]:
3528
await user_service.add(request=request, obj=obj)

backend/app/admin/crud/crud_user.py

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010

1111
from backend.app.admin.model import Dept, Role, User
1212
from backend.app.admin.schema.user import (
13+
AddOAuth2UserParam,
1314
AddUserParam,
14-
RegisterUserParam,
1515
UpdateUserParam,
1616
)
1717
from backend.common.security.jwt import get_hash_password
@@ -61,26 +61,6 @@ async def update_login_time(self, db: AsyncSession, username: str) -> int:
6161
"""
6262
return await self.update_model_by_column(db, {'last_login_time': timezone.now()}, username=username)
6363

64-
async def create(self, db: AsyncSession, obj: RegisterUserParam, *, social: bool = False) -> None:
65-
"""
66-
创建用户
67-
68-
:param db: 数据库会话
69-
:param obj: 注册用户参数
70-
:param social: 是否社交用户
71-
:return:
72-
"""
73-
if not social:
74-
salt = bcrypt.gensalt()
75-
obj.password = get_hash_password(obj.password, salt)
76-
dict_obj = obj.model_dump()
77-
dict_obj.update({'is_staff': True, 'salt': salt})
78-
else:
79-
dict_obj = obj.model_dump()
80-
dict_obj.update({'is_staff': True, 'salt': None})
81-
new_user = self.model(**dict_obj)
82-
db.add(new_user)
83-
8464
async def add(self, db: AsyncSession, obj: AddUserParam) -> None:
8565
"""
8666
添加用户
@@ -101,6 +81,21 @@ async def add(self, db: AsyncSession, obj: AddUserParam) -> None:
10181

10282
db.add(new_user)
10383

84+
async def add_by_oauth2(self, db: AsyncSession, obj: AddOAuth2UserParam) -> None:
85+
"""
86+
通过 OAuth2 添加用户
87+
88+
:param db: 数据库会话
89+
:param obj: 注册用户参数
90+
:return:
91+
"""
92+
salt = bcrypt.gensalt()
93+
obj.password = get_hash_password(obj.password, salt)
94+
dict_obj = obj.model_dump()
95+
dict_obj.update({'is_staff': True, 'salt': salt})
96+
new_user = self.model(**dict_obj)
97+
db.add(new_user)
98+
10499
async def update(self, db: AsyncSession, input_user: User, obj: UpdateUserParam) -> int:
105100
"""
106101
更新用户信息
@@ -131,7 +126,7 @@ async def delete(self, db: AsyncSession, user_id: int) -> int:
131126

132127
async def check_email(self, db: AsyncSession, email: str) -> User | None:
133128
"""
134-
检查邮箱是否已被注册
129+
检查邮箱是否已被绑定
135130
136131
:param db: 数据库会话
137132
:param email: 电子邮箱

backend/app/admin/model/user.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,22 @@ class User(Base):
2626
id: Mapped[id_key] = mapped_column(init=False)
2727
uuid: Mapped[str] = mapped_column(String(50), init=False, default_factory=uuid4_str, unique=True)
2828
username: Mapped[str] = mapped_column(String(20), unique=True, index=True, comment='用户名')
29-
nickname: Mapped[str] = mapped_column(String(20), unique=True, comment='昵称')
30-
password: Mapped[str | None] = mapped_column(String(255), comment='密码')
31-
salt: Mapped[bytes | None] = mapped_column(VARBINARY(255).with_variant(BYTEA(255), 'postgresql'), comment='加密盐')
32-
email: Mapped[str] = mapped_column(String(50), unique=True, index=True, comment='邮箱')
29+
nickname: Mapped[str] = mapped_column(String(20), comment='昵称')
30+
password: Mapped[str] = mapped_column(String(255), comment='密码')
31+
salt: Mapped[bytes] = mapped_column(VARBINARY(255).with_variant(BYTEA(255), 'postgresql'), comment='加密盐')
32+
email: Mapped[str | None] = mapped_column(String(50), default=None, unique=True, index=True, comment='邮箱')
33+
phone: Mapped[str | None] = mapped_column(String(11), default=None, comment='手机号')
34+
avatar: Mapped[str | None] = mapped_column(String(255), default=None, comment='头像')
35+
status: Mapped[int] = mapped_column(default=1, index=True, comment='用户账号状态(0停用 1正常)')
3336
is_superuser: Mapped[bool] = mapped_column(
3437
Boolean().with_variant(INTEGER, 'postgresql'), default=False, comment='超级权限(0否 1是)'
3538
)
3639
is_staff: Mapped[bool] = mapped_column(
3740
Boolean().with_variant(INTEGER, 'postgresql'), default=False, comment='后台管理登陆(0否 1是)'
3841
)
39-
status: Mapped[int] = mapped_column(default=1, index=True, comment='用户账号状态(0停用 1正常)')
4042
is_multi_login: Mapped[bool] = mapped_column(
4143
Boolean().with_variant(INTEGER, 'postgresql'), default=False, comment='是否重复登陆(0否 1是)'
4244
)
43-
avatar: Mapped[str | None] = mapped_column(String(255), default=None, comment='头像')
44-
phone: Mapped[str | None] = mapped_column(String(11), default=None, comment='手机号')
4545
join_time: Mapped[datetime] = mapped_column(
4646
DateTime(timezone=True), init=False, default_factory=timezone.now, comment='注册时间'
4747
)

backend/app/admin/schema/user.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from backend.app.admin.schema.dept import GetDeptDetail
1010
from backend.app.admin.schema.role import GetRoleWithRelationDetail
1111
from backend.common.enums import StatusType
12-
from backend.common.schema import CustomPhoneNumber, SchemaBase
12+
from backend.common.schema import CustomEmailStr, CustomPhoneNumber, SchemaBase
1313

1414

1515
class AuthSchemaBase(SchemaBase):
@@ -25,20 +25,19 @@ class AuthLoginParam(AuthSchemaBase):
2525
captcha: str = Field(description='验证码')
2626

2727

28-
class RegisterUserParam(AuthSchemaBase):
29-
"""用户注册参数"""
30-
31-
nickname: str | None = Field(None, description='昵称')
32-
email: EmailStr = Field(examples=['user@example.com'], description='邮箱')
33-
34-
3528
class AddUserParam(AuthSchemaBase):
3629
"""添加用户参数"""
3730

3831
dept_id: int = Field(description='部门 ID')
3932
roles: list[int] = Field(description='角色 ID 列表')
4033
nickname: str | None = Field(None, description='昵称')
41-
email: EmailStr = Field(examples=['user@example.com'], description='邮箱')
34+
35+
36+
class AddOAuth2UserParam(AuthSchemaBase):
37+
"""添加 OAuth2 用户参数"""
38+
39+
nickname: str | None = Field(None, description='昵称')
40+
email: EmailStr = Field(description='邮箱')
4241

4342

4443
class ResetPasswordParam(SchemaBase):
@@ -56,8 +55,6 @@ class UserInfoSchemaBase(SchemaBase):
5655
username: str = Field(description='用户名')
5756
nickname: str = Field(description='昵称')
5857
avatar: HttpUrl | None = Field(None, description='头像')
59-
email: EmailStr = Field(examples=['user@example.com'], description='邮箱')
60-
phone: CustomPhoneNumber | None = Field(None, description='手机号')
6158

6259

6360
class UpdateUserParam(UserInfoSchemaBase):
@@ -74,7 +71,8 @@ class GetUserInfoDetail(UserInfoSchemaBase):
7471
dept_id: int | None = Field(None, description='部门 ID')
7572
id: int = Field(description='用户 ID')
7673
uuid: str = Field(description='用户 UUID')
77-
avatar: str | None = Field(None, description='头像')
74+
email: CustomEmailStr | None = Field(None, description='邮箱')
75+
phone: CustomPhoneNumber | None = Field(None, description='手机号')
7876
status: StatusType = Field(StatusType.enable, description='状态')
7977
is_superuser: bool = Field(description='是否超级管理员')
8078
is_staff: bool = Field(description='是否管理员')

backend/app/admin/service/user_service.py

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
from backend.app.admin.model import Role, User
1414
from backend.app.admin.schema.user import (
1515
AddUserParam,
16-
RegisterUserParam,
1716
ResetPasswordParam,
1817
UpdateUserParam,
1918
)
@@ -27,29 +26,6 @@
2726
class UserService:
2827
"""用户服务类"""
2928

30-
@staticmethod
31-
async def register(*, obj: RegisterUserParam) -> None:
32-
"""
33-
注册新用户
34-
35-
:param obj: 用户注册参数
36-
:return:
37-
"""
38-
async with async_db_session.begin() as db:
39-
if not obj.password:
40-
raise errors.ForbiddenError(msg='密码为空')
41-
username = await user_dao.get_by_username(db, obj.username)
42-
if username:
43-
raise errors.ForbiddenError(msg='用户已注册')
44-
obj.nickname = obj.nickname if obj.nickname else f'#{random.randrange(10000, 88888)}'
45-
nickname = await user_dao.get_by_nickname(db, obj.nickname)
46-
if nickname:
47-
raise errors.ForbiddenError(msg='昵称已注册')
48-
email = await user_dao.check_email(db, obj.email)
49-
if email:
50-
raise errors.ForbiddenError(msg='邮箱已注册')
51-
await user_dao.create(db, obj)
52-
5329
@staticmethod
5430
async def add(*, request: Request, obj: AddUserParam) -> None:
5531
"""
@@ -70,9 +46,6 @@ async def add(*, request: Request, obj: AddUserParam) -> None:
7046
raise errors.ForbiddenError(msg='昵称已注册')
7147
if not obj.password:
7248
raise errors.ForbiddenError(msg='密码为空')
73-
email = await user_dao.check_email(db, obj.email)
74-
if email:
75-
raise errors.ForbiddenError(msg='邮箱已注册')
7649
dept = await dept_dao.get(db, obj.dept_id)
7750
if not dept:
7851
raise errors.NotFoundError(msg='部门不存在')
@@ -175,10 +148,6 @@ async def update(*, request: Request, username: str, obj: UpdateUserParam) -> in
175148
nickname = await user_dao.get_by_nickname(db, obj.nickname)
176149
if nickname:
177150
raise errors.ForbiddenError(msg='昵称已注册')
178-
if user.email != obj.email:
179-
email = await user_dao.check_email(db, obj.email)
180-
if email:
181-
raise errors.ForbiddenError(msg='邮箱已注册')
182151
for role_id in obj.roles:
183152
role = await role_dao.get(db, role_id)
184153
if not role:

0 commit comments

Comments
 (0)