This commit is contained in:
yumoqing 2024-11-08 11:46:39 +08:00
parent 721aa3fdef
commit 88798ee7c4
3 changed files with 41 additions and 0 deletions

0
rbac/__init__.py Normal file
View File

41
rbac/check_perm.py Normal file
View File

@ -0,0 +1,41 @@
from sqlor.dbpools import DBPools
from appPublic.registerfunction import RegisterFunction
from ahserver.auth_api import AuthAPI
class CheckPerms:
def __init__(self):
self.users = []
self.userrole = []
self.roleperms = []
self.permissions = []
self._need_refresh = True
self.rfexe = RegisterFunction().exe
async def load(self):
db = DBPools()
dbname = self.rfexe('get_module_dbname', 'rbac')
async with db.sqlorContext(dbname) as sor:
self.users = await sor.R('users',{})
self.userrole = await sor.R('userrole',{})
self.roleperms = await sor.R('roleperms', {})
perms = await sor.R('permission', {})
self.permissions = {p.path:p.id for p in perms}
async def checkperm(self, userid, path):
if self._need_refresh:
await self.load()
if not self.permissions.get(path):
return True
roleids = [ rp.roleid for rp in self.roleperms ]
userroles = [ ur.roleid for ur in self.userrole if ur.userid == userid ]
for ur in userroles:
if ur in roleids:
return True
return False
def objcheckperm(obj, userid, path):
cp = CheckPerm()
return cp.checkperm(userid, path)
AuthAPI.checkUserPermission = objcheckperm