测试casbin
This commit is contained in:
parent
da306f9b99
commit
1aae69ea63
33
server.py
33
server.py
@ -1,17 +1,17 @@
|
|||||||
import tomllib
|
import tomllib
|
||||||
from typing import Dict, Any, List, Tuple
|
from typing import Dict, Any, List, Tuple
|
||||||
|
|
||||||
|
import casbin
|
||||||
from nacl.signing import SigningKey
|
from nacl.signing import SigningKey
|
||||||
from sanic import Sanic, Request
|
from sanic import Sanic, Request
|
||||||
from sanic.log import logger, Colors
|
from sanic.log import logger, Colors
|
||||||
from sanic.response import text
|
from sanic.response import text
|
||||||
from socketio import AsyncClient
|
|
||||||
from tortoise.contrib.sanic import register_tortoise
|
from tortoise.contrib.sanic import register_tortoise
|
||||||
|
|
||||||
from command import command_convert
|
from command import command_convert
|
||||||
from gitea_model import WebHookIssueComment, WebHookIssue, GiteaEvent
|
from gitea_model import WebHookIssueComment, WebHookIssue, GiteaEvent
|
||||||
from model import Ctx, SioConfig, Message
|
from sio_model import Ctx, SioConfig, Message
|
||||||
from unit import sio_log_format
|
from unit import sio_log_format, int2str
|
||||||
|
|
||||||
app = Sanic('GiteaPush', ctx=Ctx)
|
app = Sanic('GiteaPush', ctx=Ctx)
|
||||||
|
|
||||||
@ -25,18 +25,34 @@ def get_config() -> SioConfig:
|
|||||||
SIO_CONFIG = get_config()
|
SIO_CONFIG = get_config()
|
||||||
|
|
||||||
register_tortoise(
|
register_tortoise(
|
||||||
app, db_url=SIO_CONFIG.db_url, modules={"models": ["orm"]}, generate_schemas=True
|
app, db_url=SIO_CONFIG.db_url, modules={"models": ["models"]}, generate_schemas=True
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@app.before_server_start
|
@app.before_server_start
|
||||||
async def setup_before_start(_app):
|
async def setup_before_start(_app):
|
||||||
_app.ctx.sio_config = SIO_CONFIG
|
_app.ctx.sio_config = SIO_CONFIG
|
||||||
_app.ctx.sio = AsyncClient()
|
|
||||||
|
|
||||||
start_sio_listener()
|
# 使用casbin策略管理
|
||||||
|
e = casbin.Enforcer('./casbin_data/model.conf', './casbin_data/casbin.csv')
|
||||||
|
_app.ctx.e = e
|
||||||
|
|
||||||
await _app.ctx.sio.connect(_app.ctx.sio_config.host)
|
e.add_policy('admin', '*')
|
||||||
|
e.add_policy('default', 'ping')
|
||||||
|
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')
|
||||||
|
rm_user = set(users) ^ set(admins)
|
||||||
|
for u in list(rm_user):
|
||||||
|
logger.info(e.delete_user(u))
|
||||||
|
e.save_policy()
|
||||||
|
|
||||||
|
# 初始化sio
|
||||||
|
# _app.ctx.sio = AsyncClient()
|
||||||
|
# start_sio_listener()
|
||||||
|
#
|
||||||
|
# await _app.ctx.sio.connect(_app.ctx.sio_config.host)
|
||||||
|
|
||||||
|
|
||||||
@app.post('/receive')
|
@app.post('/receive')
|
||||||
@ -75,6 +91,7 @@ async def receive(rqt: Request):
|
|||||||
@app.get('/redirect')
|
@app.get('/redirect')
|
||||||
async def redirect(rqt: Request):
|
async def redirect(rqt: Request):
|
||||||
print(rqt.args)
|
print(rqt.args)
|
||||||
|
print(rqt.ctx.state)
|
||||||
return text('success')
|
return text('success')
|
||||||
|
|
||||||
|
|
||||||
@ -164,7 +181,7 @@ def start_sio_listener():
|
|||||||
sender_id = data['message']['senderId']
|
sender_id = data['message']['senderId']
|
||||||
|
|
||||||
if sender_id != app.ctx.sio_config.self_id:
|
if sender_id != app.ctx.sio_config.self_id:
|
||||||
msg = command_convert(data)
|
msg = await command_convert(data, app)
|
||||||
if msg is not None:
|
if msg is not None:
|
||||||
await app.ctx.sio.emit('sendMessage', msg.to_json())
|
await app.ctx.sio.emit('sendMessage', msg.to_json())
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user