Move permissions into JS init script & socket
No need for an AJAX request that might produce a race condition.
This commit is contained in:
1 parent
bf8301bd6a
commit
bd95dad85e
11 files changed
+59
-74
No files matched your search
@@ -75,6 +75,12 @@ class OctoPrintPermission(Permission):
|
||||
|
||||
|
||||
class CombinedOctoPrintPermission(OctoPrintPermission):
|
||||
|
||||
def as_dict(self):
|
||||
result = OctoPrintPermission.as_dict(self)
|
||||
result["combined"] = True
|
||||
return result
|
||||
|
||||
@classmethod
|
||||
def from_permissions(cls, name, *permissions, **kwargs):
|
||||
if len(permissions) == 0:
|
||||
|
||||
@@ -14,7 +14,7 @@ from octoprint.server.util.flask import restricted_access, get_json_command_from
|
||||
from octoprint.access.permissions import Permissions
|
||||
|
||||
@api.route("/connection", methods=["GET"])
|
||||
@Permissions.CONNECTION_ACCESS.require(403)
|
||||
@Permissions.STATUS.require(403)
|
||||
def connectionState():
|
||||
state, port, baudrate, printer_profile = printer.get_current_connection()
|
||||
current = {
|
||||
|
||||
@@ -18,6 +18,7 @@ import octoprint.plugin
|
||||
|
||||
from octoprint.events import Events
|
||||
from octoprint.settings import settings
|
||||
from octoprint.access.permissions import Permissions
|
||||
|
||||
import octoprint.printer
|
||||
|
||||
@@ -106,7 +107,8 @@ class PrinterStateConnection(sockjs.tornado.SockJSConnection, octoprint.printer.
|
||||
plugin_hash=plugin_hash.hexdigest(),
|
||||
config_hash=config_hash,
|
||||
debug=octoprint.server.debug,
|
||||
safe_mode=octoprint.server.safe_mode
|
||||
safe_mode=octoprint.server.safe_mode,
|
||||
permissions=[permission.as_dict() for permission in Permissions.all()]
|
||||
))
|
||||
|
||||
self._printer.register_callback(self)
|
||||
|
||||
@@ -19,6 +19,7 @@ import octoprint.plugin
|
||||
from octoprint.server import app, userManager, groupManager, pluginManager, gettext, \
|
||||
debug, LOCALES, VERSION, DISPLAY_VERSION, UI_API_KEY, BRANCH, preemptiveCache, \
|
||||
NOT_MODIFIED
|
||||
from octoprint.access.permissions import Permissions
|
||||
from octoprint.settings import settings
|
||||
from octoprint.filemanager import get_all_extensions
|
||||
from octoprint.util import to_unicode
|
||||
@@ -421,6 +422,7 @@ def _get_render_kwargs(templates, plugin_names, plugin_vars, now):
|
||||
|
||||
first_run = settings().getBoolean(["server", "firstRun"])
|
||||
locales = dict((l.language, dict(language=l.language, display=l.display_name, english=l.english_name)) for l in LOCALES)
|
||||
permissions = [permission.as_dict() for permission in Permissions.all()]
|
||||
extensions = map(lambda ext: ".{}".format(ext), get_all_extensions())
|
||||
|
||||
#~~ prepare full set of template vars for rendering
|
||||
@@ -433,6 +435,7 @@ def _get_render_kwargs(templates, plugin_names, plugin_vars, now):
|
||||
templates=templates,
|
||||
pluginNames=plugin_names,
|
||||
locales=locales,
|
||||
permissions=permissions,
|
||||
supportedExtensions=extensions
|
||||
)
|
||||
render_kwargs.update(plugin_vars)
|
||||
|
||||
@@ -133,6 +133,9 @@ function DataUpdater(allViewModels, connectCallback, disconnectCallback) {
|
||||
|
||||
var data = event.data;
|
||||
|
||||
// update permissions
|
||||
PERMISSIONS = data["permissions"];
|
||||
|
||||
// update version information
|
||||
var oldVersion = VERSION;
|
||||
VERSION = data["version"];
|
||||
|
||||
@@ -553,6 +553,10 @@ $(function() {
|
||||
throw new Error("settingsViewModel is missing, can't run UI");
|
||||
}
|
||||
|
||||
if (!_.has(viewModelMap, "accessViewModel") || !viewModelMap["accessViewModel"].permissions) {
|
||||
throw new Error("accessViewmodel is missing or incomplete, can't run UI");
|
||||
}
|
||||
|
||||
if (!_.has(viewModelMap, "loginStateViewModel")) {
|
||||
throw new Error("loginStateViewModel is missing, can't run UI");
|
||||
}
|
||||
@@ -728,6 +732,9 @@ $(function() {
|
||||
*/
|
||||
|
||||
var onServerConnect = function() {
|
||||
// Initialize our permissions
|
||||
viewModelMap["accessViewModel"].permissions.initialize();
|
||||
|
||||
// Always perform a passive login on server (re)connect. No need for
|
||||
// onServerConnect/onServerReconnect on the LoginStateViewModel with this in place.
|
||||
return viewModelMap["loginStateViewModel"].requestData()
|
||||
|
||||
@@ -426,91 +426,50 @@ $(function() {
|
||||
access.permissions = (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); };
|
||||
|
||||
self.sanitizeName = function(name) { return name.replace(new RegExp(" ", 'g'), "_"); }
|
||||
self.permissionList = ko.observableArray([]);
|
||||
|
||||
self.registerPermission = function(name, permission) {
|
||||
Object.defineProperty(self, self.sanitizeName(name).toUpperCase(), {
|
||||
var sanitizeName = function(name) { return name.replace(new RegExp(" ", 'g'), "_"); };
|
||||
|
||||
var registeredPermissions = [];
|
||||
var registerPermission = function(name, permission) {
|
||||
var sanitized = sanitizeName(name).toUpperCase();
|
||||
Object.defineProperty(self, sanitized, {
|
||||
value: permission,
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
registeredPermissions.push(sanitized);
|
||||
};
|
||||
var clearAllRegisteredPermissions = function() {
|
||||
_.each(registeredPermissions, function(name) {
|
||||
delete self[name];
|
||||
});
|
||||
registeredPermissions = [];
|
||||
};
|
||||
|
||||
// used to delete all the permissions before registering new ones
|
||||
self.permissionsList.subscribe(function(oldValue) {
|
||||
if (oldValue === undefined || oldValue.length === 0)
|
||||
return;
|
||||
self.initialize = function() {
|
||||
clearAllRegisteredPermissions();
|
||||
|
||||
oldValue.forEach(function (p) {
|
||||
delete self[self.sanitizeName(p.name).toUpperCase()];
|
||||
});
|
||||
}, null, "beforeChange");
|
||||
|
||||
// used to register new permission
|
||||
self.permissionsList.subscribe(function(newValue) {
|
||||
if (newValue === undefined)
|
||||
return;
|
||||
|
||||
newValue.forEach(function(p) {
|
||||
var permissionList = [];
|
||||
_.each(PERMISSIONS, function(permission) {
|
||||
var needs = [];
|
||||
for (var key in p.needs) {
|
||||
p.needs[key].forEach(function(value) {
|
||||
needs.push(self.need(key, value));
|
||||
});
|
||||
}
|
||||
_.each(permission.needs, function(value, key) {
|
||||
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 (var key in p.needs) {
|
||||
p.needs[key].forEach(function(value) {
|
||||
needs.push(self.need(key, value));
|
||||
});
|
||||
registerPermission(permission.name, needs);
|
||||
}
|
||||
|
||||
// if the permission has no need sets do not register it.
|
||||
if (needs.length > 0) {
|
||||
self.registerPermission(p.name, needs);
|
||||
if (!permission.combined) {
|
||||
permissionList.push(permission);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
self.requestData = function() {
|
||||
OctoPrint.access.permissions.list().done(function(response) {
|
||||
self.permissionsList(response.permissions);
|
||||
self.combinedPermissionsList(response.combined_permissions);
|
||||
});
|
||||
};
|
||||
|
||||
self.onStartup = self.onServerConnect = self.onServerReconnect = function() {
|
||||
self.requestData();
|
||||
self.permissionList(permissionList);
|
||||
};
|
||||
|
||||
return self;
|
||||
@@ -534,17 +493,16 @@ $(function() {
|
||||
|
||||
//~~ API Calls
|
||||
access.onStartup = function() {
|
||||
access.permissions.onStartup();
|
||||
access.groups.onStartup();
|
||||
access.users.onStartup();
|
||||
};
|
||||
|
||||
access.onServerConnect = function() {
|
||||
access.permissions.onServerConnect();
|
||||
access.permissions.initialize();
|
||||
};
|
||||
|
||||
access.onServerReconnect = function() {
|
||||
access.permissions.onServerReconnect();
|
||||
access.permissions.initialize();
|
||||
};
|
||||
|
||||
access.onUserLoggedIn = function(user) {
|
||||
|
||||
@@ -229,7 +229,11 @@ $(function() {
|
||||
return false;
|
||||
}
|
||||
|
||||
return _.any(permission, function(need) { return _.has(userneeds, need.method) && _.contains(userneeds[need.method], need.value); });
|
||||
return _.any(permission, function(need) {
|
||||
return _.has(userneeds, need.method) && _.all(need.value, function(value) {
|
||||
return _.contains(userneeds[need.method], value);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
// Knockout wrapper for the hasPermission function
|
||||
|
||||
@@ -34,6 +34,8 @@
|
||||
var AVAILABLE_LOCALES = {{ locales|tojson }};
|
||||
var SUPPORTED_EXTENSIONS = {{ supportedExtensions|tojson }};
|
||||
|
||||
var PERMISSIONS = {{ permissions|tojson }};
|
||||
|
||||
var OCTOPRINT_VIEWMODELS = [];
|
||||
var ADDITIONAL_VIEWMODELS = [];
|
||||
var OCTOPRINT_ADDITIONAL_BINDINGS = [];
|
||||
|
||||
@@ -35,7 +35,7 @@
|
||||
<!-- Modals for group management -->
|
||||
|
||||
<script type="text/html" id="group-permission-template">
|
||||
<div class="controls" data-bind="foreach: $root.access.permissions.permissionsList()">
|
||||
<div class="controls" data-bind="foreach: $root.access.permissions.permissionList()">
|
||||
<label class="checkbox">
|
||||
<input type="checkbox" data-bind="checkedValue: name, checked: $root.access.groups.editorPermissions"><span data-bind="text: name, attr: { title: description }"></span><br>
|
||||
<span class="description" data-bind="text: gettext(description)"></span>
|
||||
|
||||
@@ -37,7 +37,7 @@
|
||||
<!-- Modals for user management -->
|
||||
|
||||
<script type="text/html" id="users-permission-template">
|
||||
<!-- ko foreach: $root.access.permissions.permissionsList() -->
|
||||
<!-- ko foreach: $root.access.permissions.permissionList() -->
|
||||
<label class="checkbox">
|
||||
<input type="checkbox" data-bind="checkedValue: name, checked: $root.access.users.editorPermissions"><span data-bind="text: name"></span><br>
|
||||
<span class="description" data-bind="text: description"></span>
|
||||
|
||||
Reference in new issue
Block a user