This commit is contained in:
yumoqing 2024-11-10 09:22:56 +08:00
parent ce24dc4b90
commit 4730cecea1

View File

@ -1,5 +1,6 @@
from sqlor.dbpools import DBPools
from appPublic.registerfunction import RegisterFunction
from appPublic.log import debug, exception
from ahserver.auth_api import AuthAPI
class CheckPerms:
@ -7,34 +8,38 @@ class CheckPerms:
self.users = []
self.userrole = []
self.roleperms = []
self.permissions = []
self.permissions = {}
self._need_refresh = True
self.rfexe = RegisterFunction().exe
async def load(self):
db = DBPools()
dbname = self.rfexe('get_module_dbname', 'rbac')
dbname = await 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', {})
self.roleperms = await sor.R('rolepermission', {})
perms = await sor.R('permission', {})
self.permissions = {p.path:p.id for p in perms}
async def checkperm(self, userid, path):
debug(f'{userid=}, {path=}, checkperm() called ..')
if self._need_refresh:
await self.load()
if not self.permissions.get(path):
debug(f'{path=} public access, checkperm() return true ..')
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:
debug(f'{user=} can access {path=} , checkperm() return true ..')
return True
debug(f'{userid=}, can not access {path=}, checkperm() return false ..')
return False
def objcheckperm(obj, userid, path):
cp = CheckPerm()
cp = CheckPerms()
return cp.checkperm(userid, path)
AuthAPI.checkUserPermission = objcheckperm