From 5a331e773d88fd79cdd7048288a139deb0c9b64a Mon Sep 17 00:00:00 2001 From: adk23333 <2633103794@qq.com> Date: Thu, 21 Dec 2023 01:11:02 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9C=A8QQ=E5=86=85=E6=B7=BB=E5=8A=A0=E6=9D=83?= =?UTF-8?q?=E9=99=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmds.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) 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