From 1c558307fb0ccdb402eb44a8d224bbb909e87d8a Mon Sep 17 00:00:00 2001 From: Ben Date: Wed, 29 May 2019 23:03:06 +0100 Subject: [PATCH] Implement basic permissions system --- houdini/data/permission.py | 7 +++++++ houdini/houdini.py | 4 +++- houdini/penguin.py | 9 +++++++++ houdini/permissions.py | 13 +++++++++++++ 4 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 houdini/permissions.py diff --git a/houdini/data/permission.py b/houdini/data/permission.py index 19767b6..b8d221c 100644 --- a/houdini/data/permission.py +++ b/houdini/data/permission.py @@ -19,3 +19,10 @@ class PermissionCrumbsCollection(BaseCrumbsCollection): inventory_key='penguin_id', inventory_value='permission_id', inventory_id=inventory_id) + + async def register(self, permission_name): + try: + permission = await self.get(permission_name) + except KeyError: + permission = await self.set(name=permission_name) + return permission diff --git a/houdini/houdini.py b/houdini/houdini.py index fe6d4cb..55ecb3c 100644 --- a/houdini/houdini.py +++ b/houdini/houdini.py @@ -29,7 +29,6 @@ from houdini.data.stamp import StampCrumbsCollection from houdini.data.ninja import CardCrumbsCollection from houdini.data.mail import PostcardCrumbsCollection from houdini.data.pet import PuffleCrumbsCollection, PuffleItemCrumbsCollection - from houdini.data.permission import PermissionCrumbsCollection try: @@ -77,6 +76,7 @@ class HoudiniFactory: self.commands = CommandManager(self) self.plugins = PluginManager(self) + self.permissions = None self.items = None self.igloos = None @@ -208,6 +208,8 @@ class HoudiniFactory: self.puffle_items = await PuffleItemCrumbsCollection.get_collection() self.logger.info('Loaded {} puffle care items'.format(len(self.puffle_items))) + self.permissions = await PermissionCrumbsCollection.get_collection() + handlers_path = './houdini{}handlers'.format(os.path.sep) plugins_path = './houdini{}plugins'.format(os.path.sep) self.configure_observers([handlers_path, ListenerFileEventHandler], diff --git a/houdini/penguin.py b/houdini/penguin.py index e364e35..3640a56 100644 --- a/houdini/penguin.py +++ b/houdini/penguin.py @@ -1,10 +1,12 @@ from houdini.spheniscidae import Spheniscidae +from houdini.data.permission import PermissionCrumbsCollection class Penguin(Spheniscidae): __slots__ = ['x', 'y', 'room', 'waddle', 'table', 'data', 'member', 'membership_days', 'avatar', 'walking_puffle'] + 'walking_puffle', 'permissions'] def __init__(self, *args): super().__init__(*args) @@ -23,6 +25,8 @@ class Penguin(Spheniscidae): self.walking_puffle = None + self.permissions = None + self.logger.debug('New penguin created') @property @@ -37,6 +41,11 @@ class Penguin(Spheniscidae): def penguin_state(self): return str() + + async def load(self): + if self.data: + self.permissions = await PermissionCrumbsCollection.get_collection(self.data.id) + async def add_inventory(self, item): pass diff --git a/houdini/permissions.py b/houdini/permissions.py new file mode 100644 index 0000000..38b1b2b --- /dev/null +++ b/houdini/permissions.py @@ -0,0 +1,13 @@ +from houdini.handlers import check + + +def has(permission_name, check_above=True): + def check_permission(_, p): + def check_permission_recursive(permissions, permission): + if permission in permissions: + return permissions[permission].enabled + if '.' in permission and check_above: + check_permission_recursive(permissions, '.'.join(permission.split('.')[:-1])) + return False + return check_permission_recursive(p.permissions, permission_name) + return check(check_permission)