sage/set_role_perm.py
2026-03-20 15:52:28 +08:00

74 lines
1.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import sys
import asyncio
from sqlor.dbpools import DBPools
from appPublic.jsonConfig import getConfig
from appPublic.uniqueID import getID
async def delete_anonymous_perm(sor, permid):
await sor.D('rolepermission', {
'roleid': 'anonymous',
'permid': permid
})
async def add_roleperm(sor, roleid, path, permid=None):
if permid is None:
perms = await sor.R('permission', {'path', path})
if perms:
permid = perms[0].id
ns = {
'roleid': roleid,
'permid': permid
}
recs = await sor.R('rolepermission', ns)
if recs:
print(f'{role}, {path} 已经存在')
return
await sor.C('rolepermission', {
'id': getID(),
'roleid': 'anonymous',
'perid': p.id
}
print(f'{role}, {path} perm add')
return
async def main():
config = getConfig('.')
db = DBPools(config.databases)
if len(sys.argv) < 3:
print(f'{sys.argv[0]} role path')
sys.exit(1)
role = sys.argv[1]
path = sys.argv[2]
async with db.sqlorContext('sage') as sor:
perms = None
if '%' in path:
perms = await sor.sqlExe("select * from permission where path like ${path}$", {'path': path})
else:
perms = await sor.R('permission', {'path', path})
if role in ['anonymous', 'logined']:
for p in perms:
await add_roleperm(sor, role, p.id)
return
"""
if role == customer.*' # 客户企业的任意角色
if role == '*.admin' # 任意类型的其他的管理员
"""
orgtypeid, name = role.split('.')
ns = {
'orgtypeid': orgtypeid,
'name': name
}
roles = await sor.R('role', ns.copy())
if not roles:
ns['id'] = getID()
await sor.C('role', ns.copy())
for p in perms:
await add_roleperm(sor, ns['id'], path, permid=p.id)
await delete_anonymous_perm(sor, p.id)
if __name__ == '__main__':
asyncio.get_event_loop().run_until_complete(main())