From 8e33779ed8b460932b6369964e8b6919db1be7e6 Mon Sep 17 00:00:00 2001 From: Ben Date: Sun, 17 May 2020 03:24:14 +0100 Subject: [PATCH] Extra wrapper methods for creating permission handler checks --- houdini/permissions.py | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/houdini/permissions.py b/houdini/permissions.py index 38b1b2b..18e9efa 100644 --- a/houdini/permissions.py +++ b/houdini/permissions.py @@ -1,13 +1,30 @@ from houdini.handlers import check +def check_permission(p, permission_name, check_above=True): + def check_permission_recursive(permissions, permission): + if permission in permissions: + return p.server.permissions[permission].enabled + if '.' in permission and check_above: + return check_permission_recursive(permissions, '.'.join(permission.split('.')[:-1])) + return False + + return check_permission_recursive(p.permissions, permission_name) + + 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) + def has_permission(_, p): + return check_permission(p, permission_name, check_above) + return check(has_permission) + + +def has_or_moderator(permission_name, check_above=True): + def or_moderator(_, p): + return p.moderator or check_permission(p, permission_name, check_above) + return check(or_moderator) + + +def has_or_mascot(permission_name, check_above=True): + def or_mascot(_, p): + return p.character or check_permission(p, permission_name, check_above) + return check(or_mascot)