Skip to content

Simplify user permission database queries #717

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 0 additions & 44 deletions backend/app/admin/crud/crud_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,50 +192,6 @@ async def get_list(self, dept: int | None, username: str | None, phone: str | No
**filters,
)

async def get_super(self, db: AsyncSession, user_id: int) -> bool:
"""
获取用户是否为超级管理员

:param db: 数据库会话
:param user_id: 用户 ID
:return:
"""
user = await self.get(db, user_id)
return user.is_superuser

async def get_staff(self, db: AsyncSession, user_id: int) -> bool:
"""
获取用户是否可以登录后台

:param db: 数据库会话
:param user_id: 用户 ID
:return:
"""
user = await self.get(db, user_id)
return user.is_staff

async def get_status(self, db: AsyncSession, user_id: int) -> int:
"""
获取用户状态

:param db: 数据库会话
:param user_id: 用户 ID
:return:
"""
user = await self.get(db, user_id)
return user.status

async def get_multi_login(self, db: AsyncSession, user_id: int) -> bool:
"""
获取用户是否允许多端登录

:param db: 数据库会话
:param user_id: 用户 ID
:return:
"""
user = await self.get(db, user_id)
return user.is_multi_login

async def set_super(self, db: AsyncSession, user_id: int, is_super: bool) -> int:
"""
设置用户超级管理员状态
Expand Down
11 changes: 4 additions & 7 deletions backend/app/admin/service/user_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,7 @@ async def update_superuser(*, request: Request, pk: int) -> int:
raise errors.NotFoundError(msg='用户不存在')
if pk == request.user.id:
raise errors.ForbiddenError(msg='禁止修改自身权限')
super_status = await user_dao.get_super(db, pk)
count = await user_dao.set_super(db, pk, not super_status)
count = await user_dao.set_super(db, pk, not user.status)
await redis_client.delete(f'{settings.JWT_USER_REDIS_PREFIX}:{user.id}')
return count

Expand All @@ -155,8 +154,7 @@ async def update_staff(*, request: Request, pk: int) -> int:
raise errors.NotFoundError(msg='用户不存在')
if pk == request.user.id:
raise errors.ForbiddenError(msg='禁止修改自身权限')
staff_status = await user_dao.get_staff(db, pk)
count = await user_dao.set_staff(db, pk, not staff_status)
count = await user_dao.set_staff(db, pk, not user.is_staff)
await redis_client.delete(f'{settings.JWT_USER_REDIS_PREFIX}:{user.id}')
return count

Expand All @@ -176,8 +174,7 @@ async def update_status(*, request: Request, pk: int) -> int:
raise errors.NotFoundError(msg='用户不存在')
if pk == request.user.id:
raise errors.ForbiddenError(msg='禁止修改自身权限')
status = await user_dao.get_status(db, pk)
count = await user_dao.set_status(db, pk, 0 if status == 1 else 1)
count = await user_dao.set_status(db, pk, 0 if user.status == 1 else 1)
await redis_client.delete(f'{settings.JWT_USER_REDIS_PREFIX}:{user.id}')
return count

Expand All @@ -195,7 +192,7 @@ async def update_multi_login(*, request: Request, pk: int) -> int:
user = await user_dao.get(db, pk)
if not user:
raise errors.NotFoundError(msg='用户不存在')
multi_login = await user_dao.get_multi_login(db, pk) if pk != user.id else request.user.is_multi_login
multi_login = user.is_multi_login if pk != user.id else request.user.is_multi_login
new_multi_login = not multi_login
count = await user_dao.set_multi_login(db, pk, new_multi_login)
await redis_client.delete(f'{settings.JWT_USER_REDIS_PREFIX}:{user.id}')
Expand Down