diff --git a/cmds.py b/cmds.py index ab6a2b8..5617d4f 100644 --- a/cmds.py +++ b/cmds.py @@ -1,5 +1,6 @@ import secrets +from casbin import AsyncEnforcer from sanic import SanicException from models import User, GiteaUser @@ -56,4 +57,42 @@ def cmds(app, data): if len(sqt.args) == 0: parser.parse_args(["-h"]) + @sio_decorator.cmd('authadd') + async def auth_add(sqt: SioRequest): + parser = sqt.parser + parser.add_argument('-ag', '--addgroup', help='添加用户组', + action="store_true") + parser.add_argument('-g', '--group', help='组名') + parser.add_argument('-m', '--command', help='命令名') + parser.add_argument('-au', '--adduser', help='添加用户到组', + action="store_true") + parser.add_argument('-u', '--user', help='用户QQ号') + + args = parser.parse_args(sqt.args) + e: AsyncEnforcer = sqt.app.ctx.e + msg = '' + if args.addgroup: + if args.group and args.command: + if await e.add_policy(args.group, args.command): + msg = Message(content=f'添加成功:p, {args.group}, {args.command}', room_id=sqt.room_id) + else: + msg = Message(content='添加失败,用户组已存在或其它错误', room_id=sqt.room_id) + else: + msg = Message(content='缺失参数,请使用-h查看帮助', room_id=sqt.room_id) + if args.adduser: + if args.user and args.group: + if await e.add_role_for_user(args.user, args.group): + msg = Message(content=f'添加成功:g, {args.user}, {args.group}', room_id=sqt.room_id) + else: + msg = Message(content='添加失败,用户已在组内或其它错误', room_id=sqt.room_id) + else: + msg = Message(content='缺失参数,请使用-h查看帮助', room_id=sqt.room_id) + + if len(sqt.args) == 0: + parser.parse_args(["-h"]) + else: + if msg == '': + msg = Message(content='参数错误,请使用-h查看帮助', room_id=sqt.room_id) + await sqt.app.ctx.sio.emit('sendMessage', msg.to_json()) + return sio_decorator