From 46ea1343d7b5ace4ecc942959ba066c3c85d268d Mon Sep 17 00:00:00 2001 From: j7126 Date: Wed, 9 Dec 2020 15:48:44 +1100 Subject: [PATCH 1/4] add permissions object as keys for get_settings_restricted_paths --- src/octoprint/plugin/types.py | 66 ++++++++++++++++++++++++++--------- 1 file changed, 49 insertions(+), 17 deletions(-) diff --git a/src/octoprint/plugin/types.py b/src/octoprint/plugin/types.py index 6012549b9..a81d5b3b8 100644 --- a/src/octoprint/plugin/types.py +++ b/src/octoprint/plugin/types.py @@ -1580,7 +1580,7 @@ class SettingsPlugin(OctoPrintPlugin): from flask_login import current_user - from octoprint.access.permissions import Permissions + from octoprint.access.permissions import OctoPrintPermission, Permissions data = copy.deepcopy(self._settings.get_all_data(merged=True)) if self.config_version_key in data: @@ -1632,10 +1632,17 @@ class SettingsPlugin(OctoPrintPlugin): "never": lambda: False, } - for level, condition in conditions.items(): - paths_for_level = restricted_paths.get(level, []) - for path in paths_for_level: - restrict_path_unless(data, path, condition) + for level, paths in restricted_paths.items(): + if isinstance(level, OctoPrintPermission): + for path in paths: + condition = lambda: ( + current_user is not None and current_user.has_permission(level) + ) + restrict_path_unless(data, path, condition) + else: + condition = conditions.get(level, lambda: False) + for path in paths: + restrict_path_unless(data, path, condition) return data @@ -1708,12 +1715,22 @@ class SettingsPlugin(OctoPrintPlugin): Retrieves the list of paths in the plugin's settings which be restricted on the REST API. Override this in your plugin's implementation to restrict whether a path should only be returned to users with - the SETTINGS permission, any logged in users, or never on the REST API. + certain permissions, or never on the REST API. - Return a ``dict`` with the keys ``admin``, ``user``, ``never`` mapping to a list of paths (as tuples or lists of - the path elements) for which to restrict access via the REST API accordingly. Paths returned for the ``admin`` - key will only be available on the REST API when access with admin rights, ``user`` will only be available when accessed - as a logged in user. ``never`` will never be returned on the API. + Return a ``dict`` with one of the following keys, mapping to a list of paths (as tuples or lists of + the path elements) for which to restrict access via the REST API accordingly. + + .. list-table:: + :widths: 5 95 + + * - An octoprint permission object + - Paths will only be available on the REST API for users with the permission + * - admin + - Paths will only be available on the REST API for users with admin rights (any user with the SETTINGS permission) + * - user + - Paths will only be available on the REST API when accessed as a logged in user + * - never + - Paths will never be returned on the API Example: @@ -1724,12 +1741,15 @@ class SettingsPlugin(OctoPrintPlugin): user_only=dict(path="path", bar="bar")), another=dict(admin_only=dict(path="path"), field="field"), - path=dict(to=dict(never=dict(return="return")))) + path=dict(to=dict(never=dict(return="return"))), + the=dict(webcam=dict(data="webcam"))) def get_settings_restricted_paths(self): + from octoprint.access.permissions import Permissions return dict(admin=[["some", "admin_only", "path"], ["another", "admin_only", "path"],], user=[["some", "user_only", "path"],], - never=[["path", "to", "never", "return"],]) + never=[["path", "to", "never", "return"],], + Permissions.WEBCAM=[["the", "webcam", "data"],]) # this will make the plugin return settings on the REST API like this for an anonymous user # @@ -1737,23 +1757,35 @@ class SettingsPlugin(OctoPrintPlugin): # user_only=dict(path=None, bar="bar")), # another=dict(admin_only=dict(path=None), # field="field"), - # path=dict(to=dict(never=dict(return=None)))) + # path=dict(to=dict(never=dict(return=None))), + # the=dict(webcam=dict(data=None))) # - # like this for a logged in user + # like this for a logged in user without the webcam permission # # dict(some=dict(admin_only=dict(path=None, foo="foo"), # user_only=dict(path="path", bar="bar")), # another=dict(admin_only=dict(path=None), # field="field"), - # path=dict(to=dict(never=dict(return=None)))) + # path=dict(to=dict(never=dict(return=None))), + # the=dict(webcam=dict(data=None))) # - # and like this for an admin user + # like this for a logged in user with the webcam permission + # + # dict(some=dict(admin_only=dict(path=None, foo="foo"), + # user_only=dict(path="path", bar="bar")), + # another=dict(admin_only=dict(path=None), + # field="field"), + # path=dict(to=dict(never=dict(return=None))), + # the=dict(webcam=dict(data="webcam"))) + # + # and like this for an admin user with the webcam permission # # dict(some=dict(admin_only=dict(path="path", foo="foo"), # user_only=dict(path="path", bar="bar")), # another=dict(admin_only=dict(path="path"), # field="field"), - # path=dict(to=dict(never=dict(return=None)))) + # path=dict(to=dict(never=dict(return=None))), + # the=dict(webcam=dict(data="webcam"))) ..versionadded:: 1.2.17 """ From 17389ac17baad7981ba964f0633295910ad3faac Mon Sep 17 00:00:00 2001 From: j7126 <35882868+j7126@users.noreply.github.com> Date: Mon, 14 Dec 2020 17:08:28 +1000 Subject: [PATCH 2/4] fix get_settings_restricted_paths documentation --- src/octoprint/plugin/types.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/octoprint/plugin/types.py b/src/octoprint/plugin/types.py index a81d5b3b8..5c804fb08 100644 --- a/src/octoprint/plugin/types.py +++ b/src/octoprint/plugin/types.py @@ -1746,10 +1746,10 @@ class SettingsPlugin(OctoPrintPlugin): def get_settings_restricted_paths(self): from octoprint.access.permissions import Permissions - return dict(admin=[["some", "admin_only", "path"], ["another", "admin_only", "path"],], - user=[["some", "user_only", "path"],], - never=[["path", "to", "never", "return"],], - Permissions.WEBCAM=[["the", "webcam", "data"],]) + return {'admin':[["some", "admin_only", "path"], ["another", "admin_only", "path"],], + 'user':[["some", "user_only", "path"],], + 'never':[["path", "to", "never", "return"],], + Permissions.WEBCAM:[["the", "webcam", "data"],]} # this will make the plugin return settings on the REST API like this for an anonymous user # From 1af19bb41716c9cada258fc8df4264347e00bf75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Mon, 18 Jan 2021 12:14:25 +0100 Subject: [PATCH 3/4] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Get=20rid=20of=20some?= =?UTF-8?q?=20code=20duplication?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/octoprint/plugin/types.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/octoprint/plugin/types.py b/src/octoprint/plugin/types.py index 5c804fb08..9241e5502 100644 --- a/src/octoprint/plugin/types.py +++ b/src/octoprint/plugin/types.py @@ -1634,15 +1634,14 @@ class SettingsPlugin(OctoPrintPlugin): for level, paths in restricted_paths.items(): if isinstance(level, OctoPrintPermission): - for path in paths: - condition = lambda: ( - current_user is not None and current_user.has_permission(level) - ) - restrict_path_unless(data, path, condition) + condition = lambda: ( + current_user is not None and current_user.has_permission(level) + ) else: condition = conditions.get(level, lambda: False) - for path in paths: - restrict_path_unless(data, path, condition) + + for path in paths: + restrict_path_unless(data, path, condition) return data From fe287cf6f88f4eb97920dc00647e79857d6afae2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Mon, 18 Jan 2021 12:28:05 +0100 Subject: [PATCH 4/4] =?UTF-8?q?=F0=9F=93=9D=20Slight=20rephrasing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/octoprint/plugin/types.py | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/src/octoprint/plugin/types.py b/src/octoprint/plugin/types.py index 9241e5502..1c6bf04d3 100644 --- a/src/octoprint/plugin/types.py +++ b/src/octoprint/plugin/types.py @@ -1719,17 +1719,10 @@ class SettingsPlugin(OctoPrintPlugin): Return a ``dict`` with one of the following keys, mapping to a list of paths (as tuples or lists of the path elements) for which to restrict access via the REST API accordingly. - .. list-table:: - :widths: 5 95 - - * - An octoprint permission object - - Paths will only be available on the REST API for users with the permission - * - admin - - Paths will only be available on the REST API for users with admin rights (any user with the SETTINGS permission) - * - user - - Paths will only be available on the REST API when accessed as a logged in user - * - never - - Paths will never be returned on the API + * An :py:class:`~octoprint.access.permissions.OctoPrintPermission` instance: Paths will only be available on the REST API for users with the permission + * ``admin``: Paths will only be available on the REST API for users with admin rights (any user with the SETTINGS permission) + * ``user``: Paths will only be available on the REST API when accessed as a logged in user + * ``never``: Paths will never be returned on the API Example: @@ -1746,9 +1739,9 @@ class SettingsPlugin(OctoPrintPlugin): def get_settings_restricted_paths(self): from octoprint.access.permissions import Permissions return {'admin':[["some", "admin_only", "path"], ["another", "admin_only", "path"],], - 'user':[["some", "user_only", "path"],], - 'never':[["path", "to", "never", "return"],], - Permissions.WEBCAM:[["the", "webcam", "data"],]} + 'user':[["some", "user_only", "path"],], + 'never':[["path", "to", "never", "return"],], + Permissions.WEBCAM:[["the", "webcam", "data"],]} # this will make the plugin return settings on the REST API like this for an anonymous user # @@ -1777,7 +1770,7 @@ class SettingsPlugin(OctoPrintPlugin): # path=dict(to=dict(never=dict(return=None))), # the=dict(webcam=dict(data="webcam"))) # - # and like this for an admin user with the webcam permission + # and like this for an admin user # # dict(some=dict(admin_only=dict(path="path", foo="foo"), # user_only=dict(path="path", bar="bar")),