Some Refactorings to the Granular Permission System

This commit is contained in:
Marc Hannappel committed 2017-02-17 13:57:02 +01:00
1 parent 8962ebd175
commit f60a759dca
6 files changed
+97 -95

No files matched your search

+4 -4
View File
@@ -255,9 +255,9 @@ class Group(object):
raise GroupCantbeChanged(self.get_name())
dirty = False
from octoprint.permissions import OctoPermission
from octoprint.permissions import OctoPrintPermission
for permission in permissions:
if isinstance(permission, OctoPermission) and permission not in self.permissions:
if isinstance(permission, OctoPrintPermission) and permission not in self.permissions:
self._permissions.append(permission)
dirty = True
@@ -268,9 +268,9 @@ class Group(object):
raise GroupCantbeChanged(self.get_name())
dirty = False
from octoprint.permissions import OctoPermission
from octoprint.permissions import OctoPrintPermission
for permission in permissions:
if isinstance(permission, OctoPermission) and permission in self._permissions:
if isinstance(permission, OctoPrintPermission) and permission in self._permissions:
self._permissions.remove(permission)
dirty = True
+34 -31
View File
@@ -9,13 +9,15 @@ from flask_principal import Permission, RoleNeed
from octoprint.util import variable_deprecated
all_permissions = []
class OctoPermission(Permission):
class OctoPrintPermission(Permission):
def __init__(self, name, description, *needs):
self._name = name
self._description = description
all_permissions.append(self)
super(OctoPermission, self).__init__(*needs)
super(OctoPrintPermission, self).__init__(*needs)
def asDict(self):
return dict(
@@ -35,7 +37,7 @@ class OctoPermission(Permission):
Returns reverse of current state (needs->excludes, excludes->needs)
"""
p = OctoPermission(self._name, self._description)
p = OctoPrintPermission(self._name, self._description)
all_permissions.remove(p)
p.needs.update(self.excludes)
@@ -48,7 +50,7 @@ class OctoPermission(Permission):
:param other: The other permission
"""
p = OctoPermission(self._name, self._description, *self.needs.union(other.needs))
p = OctoPrintPermission(self._name, self._description, *self.needs.union(other.needs))
all_permissions.remove(p)
p.excludes.update(self.excludes.union(other.excludes))
@@ -59,7 +61,7 @@ class OctoPermission(Permission):
permission and not in the other.
"""
p = OctoPermission(self._name, self._description, *self.needs.difference(other.needs))
p = OctoPrintPermission(self._name, self._description, *self.needs.difference(other.needs))
all_permissions.remove(p)
p.excludes.update(self.excludes.difference(other.excludes))
@@ -68,49 +70,50 @@ class OctoPermission(Permission):
def __repr__(self):
return '{0} name={1}'.format(self.__class__.__name__, self.get_name())
class Permissions(object):
# Special permission
admin = OctoPermission("Admin", "Admin is allowed to do everything", RoleNeed("admin"))
admin = OctoPrintPermission("Admin", "Admin is allowed to do everything", RoleNeed("admin"))
################################################################################
# Deprecated should be removed with the user_permission variable in a future version
user = variable_deprecated("user_permission has been deprecated and will be removed in the future", since="now")(
OctoPermission("User", "User is allowed to do basic stuff", RoleNeed("user")))
OctoPrintPermission("User", "User is allowed to do basic stuff", RoleNeed("user")))
################################################################################
status = OctoPermission("Status",
status = OctoPrintPermission("Status",
"Allows to gather Statusinformations like, Connection, Printstate, Temperaturegraph",
RoleNeed("status"))
connection = OctoPermission("Connection", "Allows to connect and disconnect to a printer", RoleNeed("connection"))
webcam = OctoPermission("Webcam", "Allows to watch the webcam stream", RoleNeed("webcam"))
system = OctoPermission("System", "Allows to run system commands, e.g. shutdown, reboot, restart octoprint",
RoleNeed("system"))
upload = OctoPermission("Upload", "Allows users to upload new gcode files", RoleNeed("upload"))
download = OctoPermission("Download",
RoleNeed("status"))
connection = OctoPrintPermission("Connection", "Allows to connect and disconnect to a printer", RoleNeed("connection"))
webcam = OctoPrintPermission("Webcam", "Allows to watch the webcam stream", RoleNeed("webcam"))
system = OctoPrintPermission("System", "Allows to run system commands, e.g. shutdown, reboot, restart octoprint",
RoleNeed("system"))
upload = OctoPrintPermission("Upload", "Allows users to upload new gcode files", RoleNeed("upload"))
download = OctoPrintPermission("Download",
"Allows users to download gcode files, the gCodeViewer is affected by this too.",
RoleNeed("download"))
delete = OctoPermission("Delete", "Allows users to delete files in their folder", RoleNeed("delete_file"))
select = OctoPermission("Selecting", "Allows to select a file", RoleNeed("select"))
printing = OctoPermission("Printing", "Allows to start a print job, inherits the select permission",
RoleNeed("print"))
terminal = OctoPermission("Terminal", "Allows to watch the Terminaltab, without the ability to send any commands",
RoleNeed("terminal"))
control = OctoPermission("Control",
RoleNeed("download"))
delete = OctoPrintPermission("Delete", "Allows users to delete files in their folder", RoleNeed("delete_file"))
select = OctoPrintPermission("Selecting", "Allows to select a file", RoleNeed("select"))
printing = OctoPrintPermission("Printing", "Allows to start a print job, inherits the select permission",
RoleNeed("print"))
terminal = OctoPrintPermission("Terminal", "Allows to watch the Terminaltab, without the ability to send any commands",
RoleNeed("terminal"))
control = OctoPrintPermission("Control",
"Allows to manually control the printer by using the controltab or sending gcodes through the terminal, this implies the terminal permission",
RoleNeed("control"))
slice = OctoPermission("Slicen", "Allows to slice stl files into gcode files", RoleNeed("slice"))
timelapse = OctoPermission("Timelapse", "Allows to download timelapse videos", RoleNeed("timelapse"))
timelapse_admin = OctoPermission("Timelapse Admin",
RoleNeed("control"))
slice = OctoPrintPermission("Slicen", "Allows to slice stl files into gcode files", RoleNeed("slice"))
timelapse = OctoPrintPermission("Timelapse", "Allows to download timelapse videos", RoleNeed("timelapse"))
timelapse_admin = OctoPrintPermission("Timelapse Admin",
"Allows to change the timelapse settings, remove timelapses, implies timelapse",
RoleNeed("timelapse_admin"))
settings = OctoPermission("Settings", "Allows to open and change Settings", RoleNeed("settings"))
logs = OctoPermission("Logs", "Allows to download and remove logs", RoleNeed("logs"))
RoleNeed("timelapse_admin"))
settings = OctoPrintPermission("Settings", "Allows to open and change Settings", RoleNeed("settings"))
logs = OctoPrintPermission("Logs", "Allows to download and remove logs", RoleNeed("logs"))
file_permission = Permission(*upload.needs.union(download.needs).union(delete.needs).union(select.needs).union(printing.needs).union(slice.needs))
@classmethod
def getPermissionFrom(cls, permission):
return permission if isinstance(permission, OctoPermission) \
return permission if isinstance(permission, OctoPrintPermission) \
else cls.permission_by_name(permission["name"]) if isinstance(permission, dict) \
else cls.permission_by_name(permission)
+17 -7
View File
@@ -51,6 +51,8 @@ pluginManager = None
appSessionManager = None
pluginLifecycleManager = None
preemptiveCache = None
jsonEncoder = None
jsonDecoder = None
principals = Principal(app)
@@ -169,6 +171,8 @@ class Server(object):
global appSessionManager
global pluginLifecycleManager
global preemptiveCache
global jsonEncoder
global jsonDecoder
global debug
global safe_mode
@@ -181,12 +185,9 @@ class Server(object):
self._logger = logging.getLogger(__name__)
pluginManager = self._plugin_manager
app.json_encoder = OctoPrintJsonEncoder
app.json_decoder = OctoPrintJsonDecoder
OctoPrintJsonEncoder.add_encoder(permissions.OctoPermission, lambda op: op.asDict())
OctoPrintJsonEncoder.add_encoder(groups.Group, lambda g: g.asDict())
OctoPrintJsonEncoder.add_multi_encoder(users.User, lambda u: u.asDict())
# setup octoprint's flask json serialization/deserialization
app.json_encoder = OctoPrintJsonEncoder.Encoder
app.json_decoder = OctoPrintJsonDecoder.Decoder
# monkey patch a bunch of stuff
util.tornado.fix_ioloop_scheduling()
@@ -219,6 +220,13 @@ class Server(object):
pluginLifecycleManager = LifecycleManager(pluginManager)
preemptiveCache = PreemptiveCache(os.path.join(self._settings.getBaseFolder("data"), "preemptive_cache_config.yaml"))
jsonEncoder = OctoPrintJsonEncoder()
jsonDecoder = OctoPrintJsonDecoder()
jsonEncoder.add_multi_encoder(users.User, lambda u: u.asDict())
jsonEncoder.add_encoder(groups.Group, lambda g: g.asDict())
jsonEncoder.add_encoder(permissions.OctoPrintPermission, lambda op: op.asDict())
# setup access control
groupManagerName = self._settings.get(["accessControl", "groupManager"])
try:
@@ -256,7 +264,9 @@ class Server(object):
plugin_lifecycle_manager=pluginLifecycleManager,
user_manager=userManager,
group_manager=groupManager,
preemptive_cache=preemptiveCache
preemptive_cache=preemptiveCache,
json_encoder=jsonEncoder,
json_decoder=jsonDecoder
)
# create printer instance
+36 -47
View File
@@ -7,65 +7,54 @@ __copyright__ = "Copyright (C) 2014 The OctoPrint Project - Released under terms
from flask.json import JSONEncoder, JSONDecoder
class OctoPrintJsonEncoder(JSONEncoder):
class OctoPrintJsonEncoder:
json_encoder = dict()
json_multi_encoder = dict()
def default(self, obj):
data_types = type(obj).__mro__
if data_types[0] in self.json_encoder:
node = self.json_encoder[data_types[0]](obj)
else:
for data_type in data_types:
if data_type in self.json_multi_encoder:
node = self.json_multi_encoder[data_type](obj)
break
class Encoder(JSONEncoder):
def default(self, obj):
data_types = type(obj).__mro__
if data_types[0] in OctoPrintJsonEncoder.json_encoder:
node = OctoPrintJsonEncoder.json_encoder[data_types[0]](obj)
else:
node = JSONEncoder.default(self, obj)
for data_type in data_types:
if data_type in OctoPrintJsonEncoder.json_multi_encoder:
node = OctoPrintJsonEncoder.json_multi_encoder[data_type](obj)
break
else:
node = JSONEncoder.default(self, obj)
return node
return node
@classmethod
def add_encoder(cls, data_type, encoder):
if 'json_encoder' not in cls.__dict__:
cls.json_encoder = cls.json_encoder.copy()
def add_encoder(self, data_type, encoder):
OctoPrintJsonEncoder.json_encoder[data_type] = encoder
cls.json_encoder[data_type] = encoder
@classmethod
def add_multi_encoder(cls, data_type, encoder):
if 'json_multi_encoder' not in cls.__dict__:
cls.json_multi_encoder = cls.json_multi_encoder.copy()
cls.json_multi_encoder[data_type] = encoder
def add_multi_encoder(self, data_type, encoder):
OctoPrintJsonEncoder.json_multi_encoder[data_type] = encoder
class OctoPrintJsonDecoder(JSONDecoder):
class OctoPrintJsonDecoder:
json_decoder = dict()
def __init__(self, **kwargs):
JSONDecoder.__init__(self, object_hook=self.dict_to_object)
self._object_hook = kwargs.pop('object_hook') if 'object_hook' in kwargs else None
class Decoder(JSONDecoder):
def __init__(self, **kwargs):
JSONDecoder.__init__(self, object_hook=self.dict_to_object)
self._object_hook = kwargs.pop('object_hook', None)
def dict_to_object(self, d):
inst = None
if self._object_hook is not None:
inst = self._object_hook(d)
def dict_to_object(self, d):
inst = None
if self._object_hook is not None:
inst = self._object_hook(d)
if inst is None:
for decoder in self.json_decoder:
inst = self.json_decoder[decoder](self, d)
if inst is not None:
return inst
else:
inst = d
if inst is None:
for decoder in OctoPrintJsonDecoder.json_decoder:
inst = OctoPrintJsonDecoder.json_decoder[decoder](self, d)
if inst is not None:
return inst
else:
inst = d
return inst
return inst
@classmethod
def add_decoder(cls, name, decoder):
if 'json_decoder' not in cls.__dict__:
cls.json_decoder = cls.json_decoder.copy()
cls.json_decoder[name] = decoder
def add_decoder(self, name, decoder):
OctoPrintJsonDecoder.json_decoder[name] = decoder
+4 -4
View File
@@ -553,9 +553,9 @@ class User(UserMixin):
def add_permissions_to_user(self, permissions):
dirty = False
from octoprint.permissions import OctoPermission
from octoprint.permissions import OctoPrintPermission
for permission in permissions:
if isinstance(permission, OctoPermission) and permission not in self.permissions:
if isinstance(permission, OctoPrintPermission) and permission not in self.permissions:
self._permissions.append(permission)
dirty = True
@@ -563,9 +563,9 @@ class User(UserMixin):
def remove_permissions_from_user(self, permissions):
dirty = False
from octoprint.permissions import OctoPermission
from octoprint.permissions import OctoPrintPermission
for permission in permissions:
if isinstance(permission, OctoPermission) and permission in self._permissions:
if isinstance(permission, OctoPrintPermission) and permission in self._permissions:
self._permissions.remove(permission)
dirty = True
+2 -2
View File
@@ -10,7 +10,7 @@ from yaml.dumper import SafeDumper
from yaml.loader import SafeLoader
from octoprint.groups import Group
from octoprint.permissions import OctoPermission
from octoprint.permissions import OctoPrintPermission
def OctoPermission_yaml_representer(dumper, data):
return dumper.represent_scalar(u'!octopermission', repr(data))
@@ -34,7 +34,7 @@ def group_yaml_constructor(loader, node):
return groupManager.findGroup(name)
yaml.add_representer(OctoPermission, OctoPermission_yaml_representer, Dumper=SafeDumper)
yaml.add_representer(OctoPrintPermission, OctoPermission_yaml_representer, Dumper=SafeDumper)
yaml.add_constructor(u'!octopermission', OctoPermission_yaml_constructor, Loader=SafeLoader)
yaml.add_representer(Group, group_yaml_representer, Dumper=SafeDumper)
yaml.add_constructor(u'!group', group_yaml_constructor, Loader=SafeLoader)