Add ability to register combined permission to the permission manager, they will not show up in the regular permission list
This commit is contained in:
1 parent
281eb346a5
commit
a8fef06aa8
4 files changed
+144
-5
No files matched your search
@@ -141,6 +141,7 @@ class PluginPermissionDecorator(Permission):
|
||||
class PermissionManager(object):
|
||||
def __init__(self):
|
||||
self._permissions = OrderedDict()
|
||||
self._combined_permissions = OrderedDict()
|
||||
|
||||
self.logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -156,6 +157,11 @@ class PermissionManager(object):
|
||||
"""Returns a list of all registered permissions"""
|
||||
return self._permissions.values()
|
||||
|
||||
@property
|
||||
def combined_permissions(self):
|
||||
"""Returns a list of all registered combined permissions"""
|
||||
return self._combined_permissions.values()
|
||||
|
||||
def yaml_representer(self, dumper, data):
|
||||
return dumper.represent_scalar(u'!octoprintpermission', data.get_name())
|
||||
|
||||
@@ -202,6 +208,41 @@ class PermissionManager(object):
|
||||
else self.find_permission(permission["name"]) if isinstance(permission, dict) \
|
||||
else self.find_permission(permission)
|
||||
|
||||
def add_combined_permission(self, permission):
|
||||
"""Registers a combined OctoPrintPermission object inside the permission manager class"""
|
||||
if permission.get_name() in self._combined_permissions:
|
||||
raise PermissionAlreadyExists(permission.get_name())
|
||||
|
||||
self._combined_permissions[permission.get_name()] = permission
|
||||
return permission
|
||||
|
||||
def remove_combined_permission(self, permission):
|
||||
"""Removes a combined OctoPrintPermission object from the permission manager class"""
|
||||
if permission is None:
|
||||
self.logger.exception("Attribute permission is None")
|
||||
return
|
||||
|
||||
permission_name = permission.get_name() if isinstance(permission, OctoPrintPermission) else permission
|
||||
if permission_name not in self._combined_permissions.keys():
|
||||
raise UnknownPermission(permission_name)
|
||||
|
||||
try:
|
||||
del self._combined_permissions[permission_name]
|
||||
except KeyError as e:
|
||||
self.logger.exception("Tried to remove a combined permission (%s) that is already gone" % permission_name)
|
||||
pass
|
||||
|
||||
def find_combined_permission(self, name):
|
||||
"""Searches a registered OctoPrintPermission by name, returns either the permission object or None"""
|
||||
return self._combined_permissions.get(name, None)
|
||||
|
||||
def get_combined_permission_from(self, permission):
|
||||
"""This function accepts either a OctoPrintPermission object, a return value of asDict, or a name
|
||||
to search for a registered OctoPrintPermission in the permission manager class"""
|
||||
return self.find_combined_permission(permission.get_name()) if isinstance(permission, OctoPrintPermission) \
|
||||
else self.find_combined_permission(permission["name"]) if isinstance(permission, dict) \
|
||||
else self.find_combined_permission(permission)
|
||||
|
||||
def unionPermissions(name, *args):
|
||||
if len(args) == 0:
|
||||
return None
|
||||
@@ -308,11 +349,18 @@ class Permissions:
|
||||
pm.add_permission(cls.SETTINGS)
|
||||
pm.add_permission(cls.LOGS)
|
||||
|
||||
pm.add_combined_permission(cls.CONTROL_ACCESS)
|
||||
pm.add_combined_permission(cls.CONNECTION_ACCESS)
|
||||
pm.add_combined_permission(cls.FILES_ACCESS)
|
||||
pm.add_combined_permission(cls.PRINTERPROFILES_ACCESS)
|
||||
pm.add_combined_permission(cls.TIMELAPSE_ACCESS)
|
||||
|
||||
@classmethod
|
||||
def __setattr__(cls, key, value):
|
||||
if key.startswith("PLUGIN"):
|
||||
cls.__metaclass__.__setattr__(key, value)
|
||||
|
||||
|
||||
class PermissionAlreadyExists(Exception):
|
||||
def __init__(self, permission):
|
||||
Exception.__init__(self, "Permission %s already exists" % permission)
|
||||
|
||||
@@ -1356,15 +1356,20 @@ class Server(object):
|
||||
global pluginManager
|
||||
global permissionManager
|
||||
|
||||
from octoprint.access.permissions import OctoPrintPermission, RoleNeed
|
||||
postponed_combined_permissions = []
|
||||
|
||||
from octoprint.access.permissions import OctoPrintPermission, RoleNeed, unionPermissions, UnknownPermission
|
||||
permissions_initializers = pluginManager.get_hooks("octoprint.access.permissions")
|
||||
for name, permissions_initializer in permissions_initializers.items():
|
||||
try:
|
||||
permission_list = permissions_initializer(components)
|
||||
for p in permission_list:
|
||||
if not isinstance(p, dict):
|
||||
continue
|
||||
|
||||
regular_permission = filter(
|
||||
lambda p: isinstance(p, dict) and ("asCombined" not in p or not p["asCombined"]), permission_list)
|
||||
combined_permission = filter(
|
||||
lambda p: isinstance(p, dict) and "asCombined" in p and p["asCombined"], permission_list)
|
||||
|
||||
for p in regular_permission:
|
||||
if "name" not in p or "description" not in p or "roles" not in p:
|
||||
continue
|
||||
|
||||
@@ -1376,6 +1381,56 @@ class Server(object):
|
||||
key = permission.get_name().replace(" ", "_").upper()
|
||||
octoprint.access.permissions.Permissions.__setattr__(key, permission)
|
||||
|
||||
for p in combined_permission:
|
||||
if "name" not in p or "permissions" not in p:
|
||||
continue
|
||||
|
||||
permissions = []
|
||||
postponed = False
|
||||
for permission_name in p["permissions"]:
|
||||
perm = permissionManager.find_permission(permission_name)
|
||||
# If we can't find a permission by the name, try to add the plugins prefix and look again
|
||||
if perm is None:
|
||||
perm = permissionManager.find_permission("Plugin {} {}".format(name, permission_name))
|
||||
|
||||
# If there is still no permission postpone this, maybe it is a permission out of another Plugin that hasn't been loaded yet.
|
||||
if perm is None:
|
||||
postponed_combined_permissions.append(p)
|
||||
postponed = True
|
||||
|
||||
if perm is not None:
|
||||
permissions.append(perm)
|
||||
|
||||
if not postponed:
|
||||
permission = permissionManager.add_combined_permission(unionPermissions("Plugin {} {}".format(name, p["name"]), *permissions))
|
||||
|
||||
if permission is not None:
|
||||
key = permission.get_name().replace(" ", "_").upper()
|
||||
octoprint.access.permissions.Permissions.__setattr__(key, permission)
|
||||
|
||||
except:
|
||||
self._logger.exception("Error while creating permission instance/s from {}".format(name))
|
||||
|
||||
for p in postponed_combined_permissions:
|
||||
try:
|
||||
permissions = []
|
||||
for permission_name in p["permissions"]:
|
||||
perm = permissionManager.find_permission(permission_name)
|
||||
# If we can't find a permission by the name, try to add the plugins prefix and look again
|
||||
if perm is None:
|
||||
perm = permissionManager.find_permission("Plugin {} {}".format(name, permission_name))
|
||||
|
||||
# If there is still no permission postpone this, maybe it is a permission out of another Plugin that hasn't been loaded yet.
|
||||
if perm is None:
|
||||
postponed_combined_permissions.extend(p)
|
||||
raise UnknownPermission(permission_name)
|
||||
|
||||
permission = permissionManager.add_combined_permission(
|
||||
unionPermissions("Plugin {} {}".format(name, p["name"]), *permissions))
|
||||
|
||||
if permission is not None:
|
||||
key = permission.get_name().replace(" ", "_").upper()
|
||||
octoprint.access.permissions.Permissions.__setattr__(key, permission)
|
||||
except:
|
||||
self._logger.exception("Error while creating permission instance/s from {}".format(name))
|
||||
|
||||
|
||||
@@ -21,7 +21,11 @@ from octoprint.access.permissions import Permissions
|
||||
|
||||
@api.route("/access/permissions", methods=["GET"])
|
||||
def get_permissions():
|
||||
return jsonify({"permissions": permissionManager.permissions})
|
||||
return jsonify(
|
||||
{
|
||||
"permissions": permissionManager.permissions,
|
||||
"combined_permissions": permissionManager.combined_permissions
|
||||
})
|
||||
|
||||
#~~ group api
|
||||
|
||||
|
||||
@@ -420,6 +420,7 @@ $(function() {
|
||||
var self = {};
|
||||
|
||||
self.permissionsList = ko.observableArray(undefined);
|
||||
self.combinedPermissionsList = ko.observableArray(undefined);
|
||||
|
||||
self.need = function(method, value) { return {method: method, value: value}; };
|
||||
self.roleNeed = function(value) { return self.need("role", value); };
|
||||
@@ -449,6 +450,36 @@ $(function() {
|
||||
if (newValue === undefined)
|
||||
return;
|
||||
|
||||
newValue.forEach(function(p) {
|
||||
var needs = [];
|
||||
for (key in p.needs) {
|
||||
p.needs[key].forEach(function(value) {
|
||||
needs.push(self.need(key, value));
|
||||
});
|
||||
}
|
||||
|
||||
// if the permission has no need sets do not register it.
|
||||
if (needs.length > 0) {
|
||||
self.registerPermission(p.name, needs);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// used to delete all the permissions before registering new ones
|
||||
self.combinedPermissionsList.subscribe(function(oldValue) {
|
||||
if (oldValue === undefined || oldValue.length == 0)
|
||||
return;
|
||||
|
||||
oldValue.forEach(function (p) {
|
||||
delete self[self.sanitizeName(p.name).toUpperCase()];
|
||||
});
|
||||
}, null, "beforeChange");
|
||||
|
||||
// used to register new permission
|
||||
self.combinedPermissionsList.subscribe(function(newValue) {
|
||||
if (newValue === undefined)
|
||||
return;
|
||||
|
||||
newValue.forEach(function(p) {
|
||||
var needs = [];
|
||||
for (key in p.needs) {
|
||||
@@ -467,6 +498,7 @@ $(function() {
|
||||
self.requestData = function() {
|
||||
OctoPrint.access.permissions.list().done(function(response) {
|
||||
self.permissionsList(response.permissions);
|
||||
self.combinedPermissionsList(response.combined_permissions);
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
Reference in new issue
Block a user