Allow plugins to declare permissions as default for standard groups
This commit is contained in:
1 parent
df62f08ec3
commit
246c614635
6 files changed
+91
-30
No files matched your search
@@ -1,2 +1,6 @@
|
||||
# coding=utf-8
|
||||
from __future__ import absolute_import, division, print_function
|
||||
|
||||
ADMIN_GROUP = "admins"
|
||||
USER_GROUP = "users"
|
||||
GUEST_GROUP = "guests"
|
||||
@@ -15,14 +15,13 @@ from past.builtins import basestring
|
||||
|
||||
from octoprint.settings import settings
|
||||
from octoprint.util import atomic_write
|
||||
from octoprint.access import ADMIN_GROUP, USER_GROUP, GUEST_GROUP
|
||||
from octoprint.access.permissions import Permissions, OctoPrintPermission
|
||||
from flask_principal import Need, Permission
|
||||
|
||||
ADMIN_GROUP = "admins"
|
||||
DEFAULT_ADMIN_PERMISSIONS = [Permissions.ADMIN]
|
||||
"""Default admin permissions are the legacy permissions from before 1.4.0"""
|
||||
|
||||
USER_GROUP = "users"
|
||||
DEFAULT_USER_PERMISSIONS = [Permissions.STATUS,
|
||||
Permissions.CONNECTION,
|
||||
Permissions.WEBCAM,
|
||||
@@ -38,7 +37,6 @@ DEFAULT_USER_PERMISSIONS = [Permissions.STATUS,
|
||||
Permissions.TIMELAPSE_ADMIN]
|
||||
"""Default user permissions are the legacy permissions from before 1.4.0"""
|
||||
|
||||
GUEST_GROUP = "guests"
|
||||
DEFAULT_GUEST_PERMISSIONS = [Permissions.STATUS,
|
||||
Permissions.WEBCAM,
|
||||
Permissions.FILES_DOWNLOAD,
|
||||
@@ -58,6 +56,14 @@ class GroupPermission(Permission):
|
||||
|
||||
|
||||
class GroupManager(object):
|
||||
@classmethod
|
||||
def default_permissions_for_group(cls, group):
|
||||
result = []
|
||||
for permission in Permissions.all():
|
||||
if group in permission.default_groups:
|
||||
result.append(permission)
|
||||
return result
|
||||
|
||||
def __init__(self):
|
||||
self._logger = logging.getLogger(__name__)
|
||||
self._group_change_listeners = []
|
||||
@@ -83,14 +89,14 @@ class GroupManager(object):
|
||||
self.add_group(ADMIN_GROUP,
|
||||
"Admins",
|
||||
"Administrators",
|
||||
DEFAULT_ADMIN_PERMISSIONS,
|
||||
self.default_permissions_for_group(ADMIN_GROUP),
|
||||
changeable=False,
|
||||
removable=False,
|
||||
save=False)
|
||||
self.add_group(USER_GROUP,
|
||||
"Users",
|
||||
"All logged in users",
|
||||
DEFAULT_USER_PERMISSIONS,
|
||||
self.default_permissions_for_group(USER_GROUP),
|
||||
default=True,
|
||||
removable=False,
|
||||
toggleable=False,
|
||||
@@ -98,7 +104,7 @@ class GroupManager(object):
|
||||
self.add_group(GUEST_GROUP,
|
||||
"Guests",
|
||||
"Anyone who is not currently logged in",
|
||||
DEFAULT_GUEST_PERMISSIONS,
|
||||
self.default_permissions_for_group(GUEST_GROUP),
|
||||
removable=False,
|
||||
toggleable=False,
|
||||
save=False)
|
||||
@@ -179,14 +185,28 @@ class FilebasedGroupManager(GroupManager):
|
||||
try:
|
||||
with open(self._groupfile, "r") as f:
|
||||
data = yaml.safe_load(f)
|
||||
for key, attributes in data.items():
|
||||
|
||||
if not "groups" in data:
|
||||
groups = data
|
||||
data = dict(groups=groups)
|
||||
|
||||
groups = data.get("groups", dict())
|
||||
tracked_permissions = data.get("tracked", list())
|
||||
|
||||
for key, attributes in groups.items():
|
||||
if key in self._groups and not self._groups[key].is_changable():
|
||||
# group is already there (from the defaults most likely) and may not be changed -> bail
|
||||
continue
|
||||
|
||||
permissions = self._to_permissions(*attributes.get("permissions", []))
|
||||
default_permissions = self.default_permissions_for_group(key)
|
||||
for permission in default_permissions:
|
||||
if not permission.key in tracked_permissions and not permission in permissions:
|
||||
permissions.append(permission)
|
||||
|
||||
self._groups[key] = Group(key, attributes.get("name", ""),
|
||||
description=attributes.get("description", ""),
|
||||
permissions=self._to_permissions(*attributes.get("permissions", [])),
|
||||
permissions=permissions,
|
||||
default=attributes.get("default", False),
|
||||
removable=attributes.get("removable",
|
||||
not attributes.get("specialGroup", False)),
|
||||
@@ -198,10 +218,10 @@ class FilebasedGroupManager(GroupManager):
|
||||
if self._groupfile is None or not self._dirty and not force:
|
||||
return
|
||||
|
||||
data = dict()
|
||||
groups = dict()
|
||||
for key in self._groups.keys():
|
||||
group = self._groups[key]
|
||||
data[key] = dict(
|
||||
groups[key] = dict(
|
||||
name=group._name,
|
||||
description=group._description,
|
||||
permissions=self._from_permissions(*group._permissions),
|
||||
@@ -210,6 +230,9 @@ class FilebasedGroupManager(GroupManager):
|
||||
changeable=group.is_changable()
|
||||
)
|
||||
|
||||
data = dict(groups=groups,
|
||||
tracked=[x.key for x in Permissions.all()])
|
||||
|
||||
with atomic_write(self._groupfile, "wb", permissions=0o600, max_permissions=0o666) as f:
|
||||
import yaml
|
||||
yaml.safe_dump(data, f, default_flow_style=False, indent=" ", allow_unicode=True)
|
||||
|
||||
@@ -15,6 +15,8 @@ from collections import OrderedDict, defaultdict
|
||||
|
||||
from past.builtins import basestring
|
||||
|
||||
from octoprint.access import ADMIN_GROUP, USER_GROUP, GUEST_GROUP
|
||||
|
||||
|
||||
class OctoPrintPermission(Permission):
|
||||
@classmethod
|
||||
@@ -41,6 +43,7 @@ class OctoPrintPermission(Permission):
|
||||
self._name = name
|
||||
self._description = description
|
||||
self._dangerous = kwargs.pop("dangerous", False) == True
|
||||
self._default_groups = kwargs.pop("default_groups", [])
|
||||
|
||||
self._key = None
|
||||
|
||||
@@ -51,6 +54,7 @@ class OctoPrintPermission(Permission):
|
||||
key=self.key,
|
||||
name=self.get_name(),
|
||||
dangerous=self._dangerous,
|
||||
default_groups=self._default_groups,
|
||||
description=self.get_description(),
|
||||
needs=self.convert_needs_to_dict(self.needs)
|
||||
)
|
||||
@@ -63,6 +67,10 @@ class OctoPrintPermission(Permission):
|
||||
def key(self, value):
|
||||
self._key = value
|
||||
|
||||
@property
|
||||
def default_groups(self):
|
||||
return self._default_groups
|
||||
|
||||
def get_name(self):
|
||||
return self._name
|
||||
|
||||
@@ -285,20 +293,24 @@ class Permissions(object):
|
||||
ADMIN = OctoPrintPermission("Admin",
|
||||
gettext("Admin is allowed to do everything"),
|
||||
RoleNeed("admin"),
|
||||
dangerous=True)
|
||||
dangerous=True,
|
||||
default_groups=[ADMIN_GROUP])
|
||||
|
||||
STATUS = OctoPrintPermission("Status",
|
||||
gettext("Allows to gather status information, e.g. job progress, "
|
||||
"printer state, temperatures, ..."),
|
||||
RoleNeed("status"))
|
||||
RoleNeed("status"),
|
||||
default_groups=[USER_GROUP, GUEST_GROUP])
|
||||
|
||||
CONNECTION = OctoPrintPermission("Connection",
|
||||
gettext("Allows to connect to and disconnect from a printer"),
|
||||
RoleNeed("connection"))
|
||||
RoleNeed("connection"),
|
||||
default_groups=[USER_GROUP])
|
||||
|
||||
WEBCAM = OctoPrintPermission("Webcam",
|
||||
gettext("Allows to watch the webcam stream"),
|
||||
RoleNeed("webcam"))
|
||||
RoleNeed("webcam"),
|
||||
default_groups=[USER_GROUP, GUEST_GROUP])
|
||||
|
||||
SYSTEM = OctoPrintPermission("System",
|
||||
gettext("Allows to run system commands, e.g. restart OctoPrint, "
|
||||
@@ -309,59 +321,73 @@ class Permissions(object):
|
||||
FILES_LIST = OctoPrintPermission("File List",
|
||||
gettext("Allows to retrieve a list of all uploaded files, including"
|
||||
"their metadata (e.g. date, file size, analysis results, ...)"),
|
||||
RoleNeed("files_list"))
|
||||
RoleNeed("files_list"),
|
||||
default_groups=[USER_GROUP, GUEST_GROUP])
|
||||
FILES_UPLOAD = OctoPrintPermission("File Upload",
|
||||
gettext("Allows users to upload new files"),
|
||||
RoleNeed("files_upload"))
|
||||
RoleNeed("files_upload"),
|
||||
default_groups=[USER_GROUP])
|
||||
FILES_DOWNLOAD = OctoPrintPermission("File Download",
|
||||
gettext("Allows users to download files. The GCODE viewer is "
|
||||
"affected by this as well."),
|
||||
RoleNeed("files_download"), FILES_LIST)
|
||||
RoleNeed("files_download"), FILES_LIST,
|
||||
default_groups=[USER_GROUP, GUEST_GROUP])
|
||||
FILES_DELETE = OctoPrintPermission("File Delete",
|
||||
gettext("Allows users to delete files"),
|
||||
RoleNeed("files_delete"), FILES_LIST)
|
||||
RoleNeed("files_delete"), FILES_LIST,
|
||||
default_groups=[USER_GROUP])
|
||||
FILES_SELECT = OctoPrintPermission("File Select",
|
||||
gettext("Allows to select a file for printing"),
|
||||
RoleNeed("files_select"), FILES_LIST)
|
||||
RoleNeed("files_select"), FILES_LIST,
|
||||
default_groups=[USER_GROUP])
|
||||
|
||||
PRINT = OctoPrintPermission("Print",
|
||||
gettext("Allows to start, pause and cancel a print job. Includes the \"Select\" "
|
||||
"permission"),
|
||||
RoleNeed("print"), FILES_SELECT)
|
||||
RoleNeed("print"), FILES_SELECT,
|
||||
default_groups=[USER_GROUP])
|
||||
|
||||
GCODE_VIEWER = OctoPrintPermission("GCODE viewer",
|
||||
gettext("Allows access to the GCODE viewer. Includes the \"File Download\""
|
||||
"permission."),
|
||||
RoleNeed("gcodeviewer"), FILES_DOWNLOAD)
|
||||
RoleNeed("gcodeviewer"), FILES_DOWNLOAD,
|
||||
default_groups=[USER_GROUP, GUEST_GROUP])
|
||||
|
||||
MONITOR_TERMINAL = OctoPrintPermission("Terminal",
|
||||
gettext("Allows to watch the terminal tab but not to send commands "
|
||||
"to the printer from it"),
|
||||
RoleNeed("monitor_terminal"))
|
||||
RoleNeed("monitor_terminal"),
|
||||
default_groups=[USER_GROUP, GUEST_GROUP])
|
||||
|
||||
CONTROL = OctoPrintPermission("Control",
|
||||
gettext("Allows to control of the printer by using the temperature controls,"
|
||||
"the control tab or sending commands through the terminal."),
|
||||
RoleNeed("control"))
|
||||
RoleNeed("control"),
|
||||
default_groups=[USER_GROUP])
|
||||
|
||||
SLICE = OctoPrintPermission("Slice",
|
||||
gettext("Allows to slice files"),
|
||||
RoleNeed("slice"))
|
||||
RoleNeed("slice"),
|
||||
default_groups=[USER_GROUP])
|
||||
|
||||
TIMELAPSE_LIST = OctoPrintPermission("Timelapse List",
|
||||
gettext("Allows to list timelapse videos"),
|
||||
RoleNeed("timelapse_list"))
|
||||
RoleNeed("timelapse_list"),
|
||||
default_groups=[USER_GROUP, GUEST_GROUP])
|
||||
TIMELAPSE_DOWNLOAD = OctoPrintPermission("Timelapse Download",
|
||||
gettext("Allows to download timelapse videos"),
|
||||
RoleNeed("timelapse_download"))
|
||||
RoleNeed("timelapse_download"),
|
||||
default_groups=[USER_GROUP, GUEST_GROUP])
|
||||
TIMELAPSE_DELETE = OctoPrintPermission("Timelapse Delete",
|
||||
gettext("Allows to delete timelapse videos and unrendered timelapses"),
|
||||
RoleNeed("timelapse_delete"))
|
||||
RoleNeed("timelapse_delete"),
|
||||
default_groups=[USER_GROUP])
|
||||
TIMELAPSE_ADMIN = OctoPrintPermission("Timelapse Admin",
|
||||
gettext("Allows to change the timelapse settings and delete or "
|
||||
"render unrendered timelapses. Includes the \"Timelapse List\","
|
||||
"\"Timelapse Delete\" and \"Timelapse Download\" permissions"),
|
||||
RoleNeed("timelapse_admin"), TIMELAPSE_LIST, TIMELAPSE_DOWNLOAD)
|
||||
RoleNeed("timelapse_admin"), TIMELAPSE_LIST, TIMELAPSE_DOWNLOAD,
|
||||
default_groups=[USER_GROUP])
|
||||
|
||||
SETTINGS = OctoPrintPermission("Settings",
|
||||
gettext("Allows to manage settings"),
|
||||
|
||||
@@ -7,6 +7,7 @@ __copyright__ = "Copyright (C) 2018 The OctoPrint Project - Released under terms
|
||||
import octoprint.plugin
|
||||
|
||||
from octoprint.events import Events
|
||||
from octoprint.access import USER_GROUP
|
||||
from octoprint.access.permissions import Permissions
|
||||
|
||||
import flask
|
||||
@@ -103,7 +104,8 @@ class PrinterSafetyCheckPlugin(octoprint.plugin.AssetPlugin,
|
||||
dict(key="DISPLAY",
|
||||
name="Display printer safety warnings",
|
||||
description=gettext("Allows to see printer safety warnings"),
|
||||
roles=["display"])
|
||||
roles=["display"],
|
||||
default_groups=[USER_GROUP])
|
||||
]
|
||||
|
||||
##~~ Helpers
|
||||
|
||||
@@ -25,6 +25,7 @@ from flask_babel import gettext
|
||||
|
||||
from octoprint.server.util.flask import require_firstrun, with_revalidation_checking, check_etag
|
||||
from octoprint.server import VERSION, REVISION, BRANCH
|
||||
from octoprint.access import USER_GROUP
|
||||
from octoprint.access.permissions import Permissions
|
||||
from octoprint.util import dict_merge, to_unicode
|
||||
import octoprint.settings
|
||||
@@ -83,7 +84,8 @@ class SoftwareUpdatePlugin(octoprint.plugin.BlueprintPlugin,
|
||||
dict(key="CHECK",
|
||||
name="Check",
|
||||
description=gettext("Allows to check for software updates"),
|
||||
roles=["check"]),
|
||||
roles=["check"],
|
||||
default_groups=[USER_GROUP]),
|
||||
dict(key="UPDATE",
|
||||
name="Update",
|
||||
description=gettext("Allows to perform software updates"),
|
||||
|
||||
@@ -1696,6 +1696,7 @@ class Server(object):
|
||||
roles = definition["roles"]
|
||||
description = definition.get("description", "")
|
||||
dangerous = definition.get("dangerous", False) == True
|
||||
default_groups = definition.get("default_groups", [])
|
||||
|
||||
key = permission_key(plugin_info.key, definition)
|
||||
setattr(octoprint.access.permissions.Permissions,
|
||||
@@ -1704,6 +1705,7 @@ class Server(object):
|
||||
description,
|
||||
plugin=plugin_info.key,
|
||||
dangerous=dangerous,
|
||||
default_groups=default_groups,
|
||||
*[permission_role(plugin_info.key, role) for role in roles]))
|
||||
|
||||
self._logger.debug("Added new permission from plugin {}: {}".format(plugin_info.key, key))
|
||||
@@ -1723,12 +1725,14 @@ class Server(object):
|
||||
|
||||
key = permission_key(plugin_info.key, definition)
|
||||
dangerous = definition.get("dangerous", False) == True
|
||||
default_groups = definition.get("default_groups", [])
|
||||
setattr(octoprint.access.permissions.Permissions,
|
||||
key,
|
||||
CombinedPluginOctoPrintPermission.from_permissions(permission_name(plugin_info.name, definition),
|
||||
description=definition.get("description", ""),
|
||||
plugin=plugin_info.key,
|
||||
dangerous=dangerous,
|
||||
default_groups=default_groups
|
||||
*resolved))
|
||||
|
||||
self._logger.debug("Added new permission from plugin {}: {}".format(plugin_info.key, key))
|
||||
|
||||
Reference in new issue
Block a user