Implement basic permissions system

This commit is contained in:
Ben 2019-05-29 23:03:06 +01:00
parent fb23387301
commit 1c558307fb
4 changed files with 32 additions and 1 deletions

View File

@ -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

View File

@ -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],

View File

@ -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

13
houdini/permissions.py Normal file
View File

@ -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)