core/services/auth/roles.py

34 lines
830 B
Python
Raw Normal View History

import asyncio
from sqlalchemy.orm import selectinload
from orm.rbac import Role
2022-09-03 10:50:14 +00:00
class RoleStorage:
2022-09-03 10:50:14 +00:00
roles = {}
lock = asyncio.Lock()
2022-09-03 10:50:14 +00:00
@staticmethod
def init(session):
self = RoleStorage
roles = session.query(Role).options(selectinload(Role.permissions)).all()
self.roles = dict([(role.id, role) for role in roles])
print("[auth.roles] %d precached" % len(roles))
2022-09-03 10:50:14 +00:00
@staticmethod
async def get_role(id):
self = RoleStorage
async with self.lock:
return self.roles.get(id)
2022-09-03 10:50:14 +00:00
@staticmethod
async def add_role(role):
self = RoleStorage
async with self.lock:
self.roles[id] = role
2022-09-03 10:50:14 +00:00
@staticmethod
async def del_role(id):
self = RoleStorage
async with self.lock:
del self.roles[id]