From 88798ee7c463963206cd3d94263d01b58d952d0f Mon Sep 17 00:00:00 2001 From: yumoqing Date: Fri, 8 Nov 2024 11:46:39 +0800 Subject: [PATCH] bugfix --- {app => rbac}/README.md | 0 rbac/__init__.py | 0 rbac/check_perm.py | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+) rename {app => rbac}/README.md (100%) create mode 100644 rbac/__init__.py create mode 100644 rbac/check_perm.py diff --git a/app/README.md b/rbac/README.md similarity index 100% rename from app/README.md rename to rbac/README.md diff --git a/rbac/__init__.py b/rbac/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/rbac/check_perm.py b/rbac/check_perm.py new file mode 100644 index 0000000..1337d9b --- /dev/null +++ b/rbac/check_perm.py @@ -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 +