From 8b921426ebde73100bc30740d6faa6f84e571179 Mon Sep 17 00:00:00 2001 From: adk23333 <2633103794@qq.com> Date: Wed, 20 Dec 2023 04:52:01 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=95=E5=85=A5casbin=E6=9D=83=E9=99=90?= =?UTF-8?q?=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/dataSources.xml | 17 +++++++++++++++++ casbin_data/model.conf | 14 ++++++++++++++ server.py | 26 +++++++++++++++++--------- unit.py | 13 ++++++++++++- 4 files changed, 60 insertions(+), 10 deletions(-) create mode 100644 .idea/dataSources.xml create mode 100644 casbin_data/model.conf diff --git a/.idea/dataSources.xml b/.idea/dataSources.xml new file mode 100644 index 0000000..d996054 --- /dev/null +++ b/.idea/dataSources.xml @@ -0,0 +1,17 @@ + + + + + sqlite.xerial + true + org.sqlite.JDBC + jdbc:sqlite:E:\pythonProject\gitea_push2qq\db.sqlite3 + $ProjectFileDir$ + + + file://$APPLICATION_CONFIG_DIR$/jdbc-drivers/Xerial SQLiteJDBC/3.39.2/sqlite-jdbc-3.39.2.jar + + + + + \ No newline at end of file diff --git a/casbin_data/model.conf b/casbin_data/model.conf new file mode 100644 index 0000000..ef2e238 --- /dev/null +++ b/casbin_data/model.conf @@ -0,0 +1,14 @@ +[request_definition] +r = sub, act + +[policy_definition] +p = sub, act + +[role_definition] +g = _, _ + +[policy_effect] +e = some(where (p.eft == allow)) + +[matchers] +m = g(r.sub, p.sub) && (r.act == p.act || p.act == "*") diff --git a/server.py b/server.py index b890bc7..a748f20 100644 --- a/server.py +++ b/server.py @@ -2,6 +2,7 @@ import tomllib from typing import Dict, Any, List, Tuple import casbin +from casbin_tortoise_adapter import TortoiseAdapter from nacl.signing import SigningKey from sanic import Sanic, Request from sanic.log import logger, Colors @@ -11,7 +12,7 @@ from tortoise.contrib.sanic import register_tortoise from command import command_convert from gitea_model import WebHookIssueComment, WebHookIssue, GiteaEvent from sio_model import Ctx, SioConfig, Message -from unit import sio_log_format, int2str +from unit import sio_log_format, int2str, cas_log_fmt app = Sanic('GiteaPush', ctx=Ctx) @@ -25,7 +26,7 @@ def get_config() -> SioConfig: SIO_CONFIG = get_config() register_tortoise( - app, db_url=SIO_CONFIG.db_url, modules={"models": ["models"]}, generate_schemas=True + app, db_url=SIO_CONFIG.db_url, modules={"models": ["models", "casbin_tortoise_adapter"]}, generate_schemas=True ) @@ -34,19 +35,26 @@ async def setup_before_start(_app): _app.ctx.sio_config = SIO_CONFIG # 使用casbin策略管理 - e = casbin.Enforcer('./casbin_data/model.conf', './casbin_data/casbin.csv') + adapter = TortoiseAdapter() + e = casbin.AsyncEnforcer('./casbin_data/model.conf', adapter) + # e = casbin.Enforcer('./casbin_data/model.conf', './casbin_data/casbin.csv') _app.ctx.e = e - e.add_policy('admin', '*') - e.add_policy('default', 'ping') + t1 = await _app.ctx.e.add_policy('admin', '*') + t2 = await _app.ctx.e.add_policy('default', 'ping') + if t1 is True and t2 is True: + logger.info(cas_log_fmt('Init casbin rule success!')) admins = int2str(_app.ctx.sio_config.admin) for qid in admins: - logger.info(e.add_role_for_user(qid, 'admin')) - users = e.get_users_for_role('admin') + if await _app.ctx.e.add_role_for_user(qid, 'admin'): + logger.debug(cas_log_fmt(f'Added {Colors.PURPLE}{qid}{Colors.YELLOW} to admin group')) + users = await _app.ctx.e.get_users_for_role('admin') rm_user = set(users) ^ set(admins) for u in list(rm_user): - logger.info(e.delete_user(u)) - e.save_policy() + if await _app.ctx.e.delete_user(u): + logger.debug(f'Delete {Colors.PURPLE}{u}{Colors.YELLOW} for group admin') + + await _app.ctx.e.save_policy() # 初始化sio # _app.ctx.sio = AsyncClient() diff --git a/unit.py b/unit.py index bf742e1..3a13196 100644 --- a/unit.py +++ b/unit.py @@ -3,5 +3,16 @@ from typing import Any from sanic.log import Colors -def sio_log_format(text: str, data: Any): +def sio_log_format(text: str, data: Any = ''): return f"{Colors.GREEN}{text} {Colors.PURPLE}{data}{Colors.END}" + + +def cas_log_fmt(text: str, data: Any = ''): + return f'{Colors.YELLOW}{text} {Colors.PURPLE}{data}{Colors.END}' + + +def int2str(li: list): + t = [] + for i in li: + t.append(str(i)) + return t