From 29bd4e96d6dc1074b1bc94987ce5eb2106324cbb Mon Sep 17 00:00:00 2001 From: Marc Hannappel Date: Wed, 26 Jul 2017 13:45:57 +0200 Subject: [PATCH] Add PluginPermissionDecorator and PluginIdentityContext to handle @Permissions.PLUGIN_X_Y.require() decorator correctly. This is necessary, because at the time of the evaluation these Permissions can't be already initialized. So I added a new PluginIdentityContext which will look for the permissions when needed and check them, if they can't be found it will raise a UnknownPermission Exception --- src/octoprint/access/permissions.py | 90 ++++++++++++++++++++++++----- src/octoprint/server/__init__.py | 2 +- 2 files changed, 77 insertions(+), 15 deletions(-) diff --git a/src/octoprint/access/permissions.py b/src/octoprint/access/permissions.py index eb1cf9cac..3f827b0d5 100644 --- a/src/octoprint/access/permissions.py +++ b/src/octoprint/access/permissions.py @@ -1,15 +1,18 @@ # coding=utf-8 from __future__ import absolute_import, division, print_function -from flask_babel import gettext -from flask_principal import Permission, RoleNeed -import logging - __author__ = "Marc Hannappel " __license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html' __copyright__ = "Copyright (C) 2017 The OctoPrint Project - Released under terms of the AGPLv3 License" +from flask import g, abort +from flask_babel import gettext +from flask_principal import Permission, PermissionDenied, RoleNeed +from functools import wraps +import logging + + class OctoPrintPermission(Permission): @classmethod def convert_needs_to_dict(cls, needs): @@ -69,6 +72,59 @@ class OctoPrintPermission(Permission): return '{0}("{1}", "{2}", {3})'.format(self.__class__.__name__, self.get_name(), self.get_description(), ', '.join(needs)) +class PluginIdentityContext(object): + """Identity context for not initialized Permissions + + Needed to support @Permissions.PLUGIN_X_Y.require() + + Will search the permission when needed + """ + def __init__(self, key, http_exception=None): + self.key = key + self.http_exception = http_exception + """The permission of this principal + """ + + @property + def identity(self): + """The identity of this principal + """ + return g.identity + + def __call__(self, f): + @wraps(f) + def _decorated(*args, **kw): + with self: + rv = f(*args, **kw) + return rv + + return _decorated + + def __enter__(self): + permission = Permissions.__getattr__(self.key) + if isinstance(permission, PluginPermissionDecorator): + raise UnknownPermission(self.key) + + # check the permission here + if not permission.can(): + if self.http_exception: + abort(self.http_exception, permission) + raise PermissionDenied(permission) + + def __exit__(self, *args): + return False + +class PluginPermissionDecorator(Permission): + """Decorator Class for not initialized Permissions + + Needed to support @Permissions.PLUGIN_X_Y.require() + """ + def __init__(self, key): + self.key = key + + def require(self, http_exception=None): + return PluginIdentityContext(self.key, http_exception) + class PermissionManager(object): def __init__(self): @@ -135,25 +191,30 @@ class PermissionManager(object): class Permissions: - class PluginPermission(type): + class PluginPermissionMetaclass(type): + plugin_permissions = dict() @classmethod def __setattr__(cls, key, value): - if cls.plugin_permissions.get(key, None) is not None: - raise PermissionAlreadyExists(key) + if key.startswith("PLUGIN"): + if cls.plugin_permissions.get(key, None) is not None: + raise PermissionAlreadyExists(key) - cls.plugin_permissions[key] = value + cls.plugin_permissions[key] = value def __getattr__(cls, key): - permission = cls.plugin_permissions.get(key, None) + if key.startswith("PLUGIN"): + permission = cls.plugin_permissions.get(key, None) - if permission is None: - raise AttributeError(key) + if permission is None: + return PluginPermissionDecorator(key) - return permission + return permission - __metaclass__ = PluginPermission + return None + + __metaclass__ = PluginPermissionMetaclass # Special permission ADMIN = OctoPrintPermission("Admin", gettext("Admin is allowed to do everything"), RoleNeed("admin")) @@ -222,7 +283,8 @@ class Permissions: @classmethod def __setattr__(cls, key, value): - cls.__metaclass__.__setattr__(key, value) + if key.startswith("PLUGIN"): + cls.__metaclass__.__setattr__(key, value) class PermissionAlreadyExists(Exception): def __init__(self, permission): diff --git a/src/octoprint/server/__init__.py b/src/octoprint/server/__init__.py index 5f96f2344..ba27f5ea0 100644 --- a/src/octoprint/server/__init__.py +++ b/src/octoprint/server/__init__.py @@ -1368,7 +1368,7 @@ class Server(object): if "name" not in p or "description" not in p or "roles" not in p: continue - permission = permissionManager.add_permission(OctoPrintPermission("Plugin_{}_{}".format(name, p["name"]), + permission = permissionManager.add_permission(OctoPrintPermission("Plugin {} {}".format(name, p["name"]), gettext(p["description"]), *[RoleNeed("plugin_{}_{}".format(name, need)) for need in p["roles"]]))