-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin_controller.py
More file actions
532 lines (487 loc) · 14.8 KB
/
admin_controller.py
File metadata and controls
532 lines (487 loc) · 14.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
from typing import Annotated, Optional
import bcrypt
from aiohttp.web import HTTPNotFound
from commondao import Commondao, Paged
from lessweb.annotation import Delete, Get, Post, Put
from shared.jwt_gateway import JwtGateway
from src.entity.admin import (
Admin,
AdminChangePasswordInput,
AdminCreateInput,
AdminInfoOutput,
AdminInsert,
AdminLoginInput,
AdminLoginOutput,
AdminUpdate,
AdminUpdateInput,
)
from src.service.auth_service import AuthRole, CurrentAdmin
async def login_admin(
login_input: AdminLoginInput,
/,
dao: Commondao,
jwt_gateway: JwtGateway) -> Annotated[AdminLoginOutput, Post('/login/admin')]:
"""
summary: 管理员登录
description: 管理员使用用户名和密码登录,验证成功后返回JWT token
tags:
- 认证管理
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/AdminLoginInput'
responses:
200:
description: 登录成功,返回JWT token
content:
application/json:
schema:
$ref: '#/components/schemas/AdminLoginOutput'
400:
description: 用户名或密码错误,或账号未激活
"""
# 查询管理员账号
admin = await dao.select_one(
'from tbl_admin where username = :username limit 1',
Admin,
{'username': login_input.username}
)
# 验证账号是否存在
assert admin, 'Invalid username or password'
# 验证账号是否激活
assert admin.isActive, 'Account is not active'
# 验证密码
assert bcrypt.checkpw(
login_input.password.encode('utf-8'),
admin.passwordHash.encode('utf-8')
), 'Invalid username or password'
# 生成JWT token
token = jwt_gateway.encrypt_jwt(
user_id=str(admin.id),
subject=AuthRole.ADMIN
)
# 在Redis中设置登录状态
await jwt_gateway.login(
user_id=str(admin.id),
user_role=AuthRole.ADMIN
)
# 返回登录结果
return AdminLoginOutput(
token=token,
adminId=admin.id,
username=admin.username
)
async def get_admin_me(current_admin: CurrentAdmin) -> Annotated[AdminInfoOutput, Get('/admin/me')]:
"""
summary: 获取当前登录管理员信息
description: 获取当前已登录的管理员的详细信息(不包含密码等敏感信息)
tags:
- 管理员管理
security:
- bearerAuth: []
responses:
200:
description: 成功返回管理员信息
content:
application/json:
schema:
$ref: '#/components/schemas/AdminInfoOutput'
401:
description: 未登录或token无效
403:
description: 无权限访问(非管理员角色)
"""
# 通过 CurrentAdmin service 获取当前登录的管理员
admin = await current_admin.get()
# 返回管理员信息(不包含 passwordHash)
return AdminInfoOutput(
id=admin.id,
username=admin.username,
nickname=admin.nickname,
email=admin.email,
isActive=admin.isActive,
createTime=admin.createTime,
updateTime=admin.updateTime
)
async def change_password(
change_password_input: AdminChangePasswordInput,
/,
dao: Commondao,
current_admin: CurrentAdmin,
) -> Annotated[dict, Put('/admin/change-password')]:
"""
summary: 修改当前登录管理员密码
description: 校验旧密码正确后,修改当前登录管理员的密码
tags:
- 管理员管理
security:
- bearerAuth: []
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/AdminChangePasswordInput'
responses:
200:
description: 修改成功
content:
application/json:
schema:
type: object
properties:
success:
type: boolean
400:
description: 旧密码错误或参数不合法
401:
description: 未登录或token无效
403:
description: 无权限访问(非管理员角色)
"""
admin = await current_admin.get()
assert bcrypt.checkpw(
change_password_input.oldPassword.encode('utf-8'),
admin.passwordHash.encode('utf-8'),
), 'Invalid old password'
assert change_password_input.newPassword, 'newPassword is required'
assert change_password_input.newPassword != change_password_input.oldPassword, 'newPassword must be different'
new_password_hash = bcrypt.hashpw(
change_password_input.newPassword.encode('utf-8'),
bcrypt.gensalt(),
).decode('utf-8')
updated_count = await dao.execute_mutation(
'update tbl_admin set passwordHash=:passwordHash where id=:id',
{'passwordHash': new_password_hash, 'id': admin.id},
)
if not updated_count:
raise RuntimeError('Failed to update password')
return {'success': True}
async def create_admin(
create_input: AdminCreateInput,
/,
dao: Commondao,
) -> Annotated[AdminInfoOutput, Post('/admin/admins')]:
"""
summary: 创建管理员
description: 创建新的管理员账号(检查用户名是否存在、密码至少8位)
tags:
- 管理员管理
security:
- bearerAuth: []
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/AdminCreateInput'
responses:
200:
description: 创建成功
content:
application/json:
schema:
$ref: '#/components/schemas/AdminInfoOutput'
400:
description: 参数不合法或用户名已存在
401:
description: 未登录或token无效
403:
description: 无权限访问(非管理员角色)
"""
assert 1 <= len(create_input.username) <= 100, 'username length must be between 1 and 100'
assert 1 <= len(create_input.nickname) <= 100, 'nickname length must be between 1 and 100'
assert len(create_input.password) >= 8, 'Password must be at least 8 characters'
if create_input.email is not None:
assert '@' in create_input.email, 'email is invalid'
username_existing = await dao.select_one(
'from tbl_admin where username = :username limit 1',
Admin,
{'username': create_input.username},
)
assert not username_existing, 'Username already exists'
password_hash = bcrypt.hashpw(
create_input.password.encode('utf-8'),
bcrypt.gensalt(),
).decode('utf-8')
inserted = await dao.insert(
AdminInsert(
username=create_input.username,
nickname=create_input.nickname,
passwordHash=password_hash,
email=create_input.email,
isActive=create_input.isActive,
)
)
if not inserted:
raise RuntimeError('Failed to create admin')
admin_id = dao.lastrowid()
admin = await dao.get_by_id_or_fail(Admin, admin_id)
return AdminInfoOutput(
id=admin.id,
username=admin.username,
nickname=admin.nickname,
email=admin.email,
isActive=admin.isActive,
createTime=admin.createTime,
updateTime=admin.updateTime,
)
async def get_admin_by_id(
dao: Commondao,
*,
id: int,
) -> Annotated[AdminInfoOutput, Get('/admin/admins/{id}')]:
"""
summary: 获取管理员详情
description: 根据ID获取管理员详情
tags:
- 管理员管理
security:
- bearerAuth: []
parameters:
- name: id
in: path
required: true
schema:
type: integer
responses:
200:
description: 成功返回管理员详情
content:
application/json:
schema:
$ref: '#/components/schemas/AdminInfoOutput'
401:
description: 未登录或token无效
403:
description: 无权限访问(非管理员角色)
404:
description: 管理员不存在
"""
admin = await dao.get_by_id(Admin, id)
if not admin:
raise HTTPNotFound(text='admin not found')
return AdminInfoOutput(
id=admin.id,
username=admin.username,
nickname=admin.nickname,
email=admin.email,
isActive=admin.isActive,
createTime=admin.createTime,
updateTime=admin.updateTime,
)
async def update_admin_by_id(
update_input: AdminUpdateInput,
/,
dao: Commondao,
*,
id: int,
) -> Annotated[AdminInfoOutput, Put('/admin/admins/{id}')]:
"""
summary: 更新管理员信息
description: 根据ID更新管理员的username、nickname、email、is_active字段(支持部分更新)
tags:
- 管理员管理
security:
- bearerAuth: []
parameters:
- name: id
in: path
required: true
schema:
type: integer
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/AdminUpdateInput'
responses:
200:
description: 更新成功
content:
application/json:
schema:
$ref: '#/components/schemas/AdminInfoOutput'
400:
description: 参数不合法或用户名已存在
401:
description: 未登录或token无效
403:
description: 无权限访问(非管理员角色)
"""
assert await dao.get_by_id(Admin, id), 'admin not found'
fields_set = update_input.model_dump(exclude_unset=True, exclude_none=True)
update_fields: dict = {}
if 'username' in fields_set:
assert update_input.username, 'username must not be empty'
username_existing = await dao.select_one(
'from tbl_admin where username = :username and id <> :id limit 1',
Admin,
{'username': update_input.username, 'id': id},
)
assert not username_existing, 'Username already exists'
update_fields['username'] = update_input.username
if 'nickname' in fields_set:
assert update_input.nickname, 'nickname must not be empty'
update_fields['nickname'] = update_input.nickname
if 'email' in fields_set:
assert update_input.email and '@' in update_input.email, 'email is invalid'
update_fields['email'] = update_input.email
if 'isActive' in fields_set:
update_fields['isActive'] = update_input.isActive
assert update_fields, 'No fields to update'
updated_count = await dao.update_by_id(AdminUpdate(id=id, **update_fields))
if not updated_count:
raise RuntimeError('Failed to update admin')
admin = await dao.get_by_id_or_fail(Admin, id)
return AdminInfoOutput(
id=admin.id,
username=admin.username,
nickname=admin.nickname,
email=admin.email,
isActive=admin.isActive,
createTime=admin.createTime,
updateTime=admin.updateTime,
)
async def get_admins(
dao: Commondao,
*,
username: Optional[str] = None,
nickname: Optional[str] = None,
email: Optional[str] = None,
isActive: Optional[bool] = None,
offset: int = 0,
size: int = 10,
) -> Annotated[Paged[AdminInfoOutput], Get('/admin/admins')]:
"""
summary: 分页查询管理员列表
description: 获取管理员列表,支持分页参数和筛选条件(username、nickname、email、isActive、offset、size)
tags:
- 管理员管理
security:
- bearerAuth: []
parameters:
- name: username
in: query
required: false
schema:
type: string
description: 用户名筛选(模糊匹配)
- name: nickname
in: query
required: false
schema:
type: string
description: 昵称筛选(模糊匹配)
- name: email
in: query
required: false
schema:
type: string
description: 邮箱筛选(模糊匹配)
- name: isActive
in: query
required: false
schema:
type: boolean
description: 是否激活筛选
- name: offset
in: query
required: false
schema:
type: integer
default: 0
description: 偏移量,默认为0
- name: size
in: query
required: false
schema:
type: integer
default: 10
description: 每页数量,默认为10
responses:
200:
description: 成功返回管理员列表
content:
application/json:
schema:
type: object
properties:
items:
type: array
items:
$ref: '#/components/schemas/AdminInfoOutput'
offset:
type: integer
size:
type: integer
total:
type: integer
401:
description: 未登录或token无效
403:
description: 无权限访问(非管理员角色)
"""
# 构建查询条件
where_clauses = []
params: dict = {}
if username is not None:
where_clauses.append('username LIKE :username')
params['username'] = f'%{username}%'
if nickname is not None:
where_clauses.append('nickname LIKE :nickname')
params['nickname'] = f'%{nickname}%'
if email is not None:
where_clauses.append('email LIKE :email')
params['email'] = f'%{email}%'
if isActive is not None:
where_clauses.append('isActive = :isActive')
params['isActive'] = isActive
# 构建WHERE子句
where_sql = ' AND '.join(where_clauses) if where_clauses else '1=1'
# 使用select_paged进行分页查询
return await dao.select_paged(
f'from tbl_admin where {where_sql} order by id desc',
AdminInfoOutput,
params,
size=size,
offset=offset
)
async def delete_admin_by_id(
dao: Commondao,
*,
id: int,
) -> Annotated[dict, Delete('/admin/admins/{id}')]:
"""
summary: 删除管理员
description: 根据ID删除管理员
tags:
- 管理员管理
security:
- bearerAuth: []
parameters:
- name: id
in: path
required: true
schema:
type: integer
responses:
200:
description: 删除成功
content:
application/json:
schema:
type: object
properties:
success:
type: boolean
401:
description: 未登录或token无效
403:
description: 无权限访问(非管理员角色)
"""
await dao.delete_by_id(Admin, id)
return {'success': True}