11
This commit is contained in:
@@ -1,13 +1,13 @@
|
||||
from sqlalchemy import func, select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.core.roles import ROLE_ADMIN
|
||||
from app.core.roles import ROLE_ADMIN, ROLE_AGENT, ROLE_OEM
|
||||
from app.core.security import hash_password
|
||||
from app.models.user import User
|
||||
from app.schemas.admin_user import AdminUserCreate, AdminUserUpdate
|
||||
|
||||
DEFAULT_PAGE_SIZE = 20
|
||||
MAX_PAGE_SIZE = 100
|
||||
MAX_PAGE_SIZE = 200
|
||||
|
||||
|
||||
class AdminUserError(Exception):
|
||||
@@ -16,23 +16,72 @@ class AdminUserError(Exception):
|
||||
super().__init__(message)
|
||||
|
||||
|
||||
def _list_users_filters(
|
||||
*,
|
||||
username: str | None = None,
|
||||
oem_id: int | None = None,
|
||||
agent_id: int | None = None,
|
||||
role_id: int | None = None,
|
||||
) -> list:
|
||||
conditions = []
|
||||
if username and username.strip():
|
||||
conditions.append(User.username.ilike(f"%{username.strip()}%"))
|
||||
if oem_id is not None:
|
||||
conditions.append(User.oem_id == oem_id)
|
||||
if agent_id is not None:
|
||||
conditions.append(User.agent_id == agent_id)
|
||||
if role_id is not None:
|
||||
conditions.append(User.role_id == role_id)
|
||||
return conditions
|
||||
|
||||
|
||||
async def list_users(
|
||||
db: AsyncSession,
|
||||
*,
|
||||
page: int = 1,
|
||||
page_size: int = DEFAULT_PAGE_SIZE,
|
||||
username: str | None = None,
|
||||
oem_id: int | None = None,
|
||||
agent_id: int | None = None,
|
||||
role_id: int | None = None,
|
||||
) -> tuple[list[User], int]:
|
||||
page_size = min(max(page_size, 1), MAX_PAGE_SIZE)
|
||||
page = max(page, 1)
|
||||
offset = (page - 1) * page_size
|
||||
|
||||
total = await db.scalar(select(func.count()).select_from(User)) or 0
|
||||
result = await db.scalars(
|
||||
select(User).order_by(User.id.desc()).offset(offset).limit(page_size)
|
||||
conditions = _list_users_filters(
|
||||
username=username,
|
||||
oem_id=oem_id,
|
||||
agent_id=agent_id,
|
||||
role_id=role_id,
|
||||
)
|
||||
|
||||
count_stmt = select(func.count()).select_from(User)
|
||||
list_stmt = select(User).order_by(User.id.desc())
|
||||
if conditions:
|
||||
count_stmt = count_stmt.where(*conditions)
|
||||
list_stmt = list_stmt.where(*conditions)
|
||||
|
||||
total = await db.scalar(count_stmt) or 0
|
||||
result = await db.scalars(list_stmt.offset(offset).limit(page_size))
|
||||
return list(result.all()), total
|
||||
|
||||
|
||||
async def _validate_owner_ids(
|
||||
db: AsyncSession,
|
||||
*,
|
||||
oem_id: int | None,
|
||||
agent_id: int | None,
|
||||
) -> None:
|
||||
if oem_id is not None:
|
||||
oem = await db.get(User, oem_id)
|
||||
if oem is None or oem.role_id != ROLE_OEM:
|
||||
raise AdminUserError("所选 OEM 不存在")
|
||||
if agent_id is not None:
|
||||
agent = await db.get(User, agent_id)
|
||||
if agent is None or agent.role_id != ROLE_AGENT:
|
||||
raise AdminUserError("所选代理不存在")
|
||||
|
||||
|
||||
async def create_user(db: AsyncSession, body: AdminUserCreate) -> User:
|
||||
existing = await db.scalar(select(User).where(User.username == body.username))
|
||||
if existing is not None:
|
||||
@@ -43,11 +92,15 @@ async def create_user(db: AsyncSession, body: AdminUserCreate) -> User:
|
||||
if phone_taken is not None:
|
||||
raise AdminUserError("手机号已被使用")
|
||||
|
||||
await _validate_owner_ids(db, oem_id=body.oem_id, agent_id=body.agent_id)
|
||||
|
||||
user = User(
|
||||
username=body.username,
|
||||
password_hash=hash_password(body.password),
|
||||
phone=body.phone,
|
||||
role_id=body.role_id,
|
||||
oem_id=body.oem_id,
|
||||
agent_id=body.agent_id,
|
||||
vip_end_time=body.vip_end_time,
|
||||
)
|
||||
db.add(user)
|
||||
@@ -85,6 +138,17 @@ async def update_user(
|
||||
raise AdminUserError("不能修改自己的管理员角色")
|
||||
user.role_id = body.role_id
|
||||
|
||||
if body.oem_id is not None or body.agent_id is not None:
|
||||
await _validate_owner_ids(
|
||||
db,
|
||||
oem_id=body.oem_id if body.oem_id is not None else user.oem_id,
|
||||
agent_id=body.agent_id if body.agent_id is not None else user.agent_id,
|
||||
)
|
||||
if body.oem_id is not None:
|
||||
user.oem_id = body.oem_id
|
||||
if body.agent_id is not None:
|
||||
user.agent_id = body.agent_id
|
||||
|
||||
if body.password:
|
||||
user.password_hash = hash_password(body.password)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user