Identify groups by a key & add group role
This commit is contained in:
1 parent
08bfae216c
commit
f7ad87449a
8 files changed
+156
-110
No files matched your search
@@ -7,18 +7,22 @@ __copyright__ = "Copyright (C) 2017 The OctoPrint Project - Released under terms
|
||||
|
||||
import logging
|
||||
import os
|
||||
from functools import partial
|
||||
|
||||
import yaml
|
||||
|
||||
from past.builtins import basestring
|
||||
|
||||
from octoprint.settings import settings
|
||||
from octoprint.util import atomic_write
|
||||
from octoprint.access.permissions import Permissions, OctoPrintPermission
|
||||
from flask_principal import Need, Permission
|
||||
|
||||
ADMIN_GROUP = "Admins"
|
||||
ADMIN_GROUP = "admins"
|
||||
DEFAULT_ADMIN_PERMISSIONS = [Permissions.ADMIN]
|
||||
"""Default admin permissions are the legacy permissions from before 1.4.0"""
|
||||
|
||||
USER_GROUP = "Users"
|
||||
USER_GROUP = "users"
|
||||
DEFAULT_USER_PERMISSIONS = [Permissions.STATUS,
|
||||
Permissions.CONNECTION,
|
||||
Permissions.WEBCAM,
|
||||
@@ -34,7 +38,7 @@ DEFAULT_USER_PERMISSIONS = [Permissions.STATUS,
|
||||
Permissions.TIMELAPSE_ADMIN]
|
||||
"""Default user permissions are the legacy permissions from before 1.4.0"""
|
||||
|
||||
GUEST_GROUP = "Guests"
|
||||
GUEST_GROUP = "guests"
|
||||
DEFAULT_GUEST_PERMISSIONS = [Permissions.STATUS,
|
||||
Permissions.WEBCAM,
|
||||
Permissions.FILES_DOWNLOAD,
|
||||
@@ -43,6 +47,16 @@ DEFAULT_GUEST_PERMISSIONS = [Permissions.STATUS,
|
||||
"""Default guest permissions are the legacy permissions from before 1.4.0"""
|
||||
|
||||
|
||||
GroupNeed = partial(Need, 'group')
|
||||
GroupNeed.__doc__ = """A need with the method preset to `"group"`."""
|
||||
|
||||
|
||||
class GroupPermission(Permission):
|
||||
def __init__(self, key):
|
||||
need = GroupNeed(key)
|
||||
super(GroupPermission, self).__init__(need)
|
||||
|
||||
|
||||
class GroupManager(object):
|
||||
def __init__(self):
|
||||
self._logger = logging.getLogger(__name__)
|
||||
@@ -67,18 +81,21 @@ class GroupManager(object):
|
||||
|
||||
def _init_defaults(self):
|
||||
self.add_group(ADMIN_GROUP,
|
||||
"Admins",
|
||||
"Administrators",
|
||||
DEFAULT_ADMIN_PERMISSIONS,
|
||||
changeable=False,
|
||||
removable=False,
|
||||
save=False)
|
||||
self.add_group(USER_GROUP,
|
||||
"Users",
|
||||
"Logged in users",
|
||||
DEFAULT_USER_PERMISSIONS,
|
||||
default=True,
|
||||
removable=False,
|
||||
save=False)
|
||||
self.add_group(GUEST_GROUP,
|
||||
"Guests",
|
||||
"Logged out guests",
|
||||
DEFAULT_GUEST_PERMISSIONS,
|
||||
removable=False,
|
||||
@@ -90,16 +107,16 @@ class GroupManager(object):
|
||||
def unregister_listener(self, listener):
|
||||
self._group_change_listeners.remove(listener)
|
||||
|
||||
def add_group(self, name, description, permissions, default=False, removable=True, changeable=True, save=True, notify=True):
|
||||
def add_group(self, key, name, description, permissions, default=False, removable=True, changeable=True, save=True, notify=True):
|
||||
pass
|
||||
|
||||
def update_group(self, groupname, description=None, permissions=None, default=None, save=True, notify=True):
|
||||
def update_group(self, key, description=None, permissions=None, default=None, save=True, notify=True):
|
||||
pass
|
||||
|
||||
def remove_group(self, name, save=True, notify=True):
|
||||
def remove_group(self, key, save=True, notify=True):
|
||||
pass
|
||||
|
||||
def find_group(self, name):
|
||||
def find_group(self, key):
|
||||
return None
|
||||
|
||||
def _to_permissions(self, *permissions):
|
||||
@@ -110,12 +127,13 @@ class GroupManager(object):
|
||||
return [permission.key for permission in permissions]
|
||||
|
||||
def _to_group(self, group):
|
||||
# noinspection PyCompatibility
|
||||
if isinstance(group, Group):
|
||||
return group
|
||||
elif isinstance(group, basestring):
|
||||
return self.find_group(group)
|
||||
elif isinstance(group, dict):
|
||||
return self.find_group(group.get("name"))
|
||||
return self.find_group(group.get("key"))
|
||||
else:
|
||||
return None
|
||||
|
||||
@@ -156,30 +174,33 @@ class FilebasedGroupManager(GroupManager):
|
||||
|
||||
def _load(self):
|
||||
if os.path.exists(self._groupfile) and os.path.isfile(self._groupfile):
|
||||
# TODO: error handling
|
||||
with open(self._groupfile, "r") as f:
|
||||
data = yaml.safe_load(f)
|
||||
for name, attributes in data.items():
|
||||
if name in self._groups and not self._groups[name].is_changable():
|
||||
# group is already there (from the defaults most likely) and may not be changed -> bail
|
||||
continue
|
||||
try:
|
||||
with open(self._groupfile, "r") as f:
|
||||
data = yaml.safe_load(f)
|
||||
for key, attributes in data.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
|
||||
|
||||
self._groups[name] = Group(name,
|
||||
description=attributes.get("description", ""),
|
||||
permissions=self._to_permissions(*attributes.get("permissions", [])),
|
||||
default=attributes.get("default", False),
|
||||
removable=attributes.get("removable",
|
||||
not attributes.get("specialGroup", False)),
|
||||
changeable=attributes.get("changeable", True))
|
||||
self._groups[key] = Group(key, attributes.get("name", ""),
|
||||
description=attributes.get("description", ""),
|
||||
permissions=self._to_permissions(*attributes.get("permissions", [])),
|
||||
default=attributes.get("default", False),
|
||||
removable=attributes.get("removable",
|
||||
not attributes.get("specialGroup", False)),
|
||||
changeable=attributes.get("changeable", True))
|
||||
except:
|
||||
self._logger.exception("Error while loading groups from file {}".format(self._groupfile))
|
||||
|
||||
def _save(self, force=False):
|
||||
if self._groupfile is None or not self._dirty and not force:
|
||||
return
|
||||
|
||||
data = dict()
|
||||
for name in self._groups.keys():
|
||||
group = self._groups[name]
|
||||
data[name] = dict(
|
||||
for key in self._groups.keys():
|
||||
group = self._groups[key]
|
||||
data[key] = dict(
|
||||
name=group._name,
|
||||
description=group._description,
|
||||
permissions=self._from_permissions(*group._permissions),
|
||||
default=group._default,
|
||||
@@ -201,15 +222,15 @@ class FilebasedGroupManager(GroupManager):
|
||||
def default_groups(self):
|
||||
return [group for group in self._groups.values() if group.is_default()]
|
||||
|
||||
def find_group(self, name):
|
||||
if name is None:
|
||||
def find_group(self, key):
|
||||
if key is None:
|
||||
return None
|
||||
return self._groups.get(name)
|
||||
return self._groups.get(key)
|
||||
|
||||
def add_group(self, groupname, description, permissions, default=False, removable=True,
|
||||
def add_group(self, key, name, description, permissions, default=False, removable=True,
|
||||
changeable=True, overwrite=False, notify=True, save=True):
|
||||
if groupname in self._groups.keys() and not overwrite:
|
||||
raise GroupAlreadyExists(groupname)
|
||||
if key in self._groups.keys() and not overwrite:
|
||||
raise GroupAlreadyExists(key)
|
||||
|
||||
if not permissions:
|
||||
permissions = []
|
||||
@@ -217,13 +238,13 @@ class FilebasedGroupManager(GroupManager):
|
||||
permissions = self._to_permissions(*permissions)
|
||||
assert(all(map(lambda p: isinstance(p, OctoPrintPermission), permissions)))
|
||||
|
||||
group = Group(groupname,
|
||||
group = Group(key, name,
|
||||
description=description,
|
||||
permissions=permissions,
|
||||
default=default,
|
||||
changeable=changeable,
|
||||
removable=removable)
|
||||
self._groups[groupname] = group
|
||||
self._groups[key] = group
|
||||
|
||||
if save:
|
||||
self._dirty = True
|
||||
@@ -232,16 +253,16 @@ class FilebasedGroupManager(GroupManager):
|
||||
if notify:
|
||||
self._notify_listeners("added", group)
|
||||
|
||||
def remove_group(self, groupname, save=True, notify=True):
|
||||
"""Removes a Group by name"""
|
||||
group = self._to_group(groupname)
|
||||
def remove_group(self, key, save=True, notify=True):
|
||||
"""Removes a Group by key"""
|
||||
group = self._to_group(key)
|
||||
if group is None:
|
||||
raise UnknownGroup(groupname)
|
||||
raise UnknownGroup(key)
|
||||
|
||||
if not group.is_removeable():
|
||||
raise GroupUnremovable(groupname)
|
||||
raise GroupUnremovable(key)
|
||||
|
||||
del self._groups[groupname]
|
||||
del self._groups[key]
|
||||
self._dirty = True
|
||||
|
||||
if save:
|
||||
@@ -250,13 +271,13 @@ class FilebasedGroupManager(GroupManager):
|
||||
if notify:
|
||||
self._notify_listeners("removed", group)
|
||||
|
||||
def update_group(self, groupname, description=None, permissions=None, default=None, save=True, notify=True):
|
||||
group = self._to_group(groupname)
|
||||
def update_group(self, key, description=None, permissions=None, default=None, save=True, notify=True):
|
||||
group = self._to_group(key)
|
||||
if group is None:
|
||||
raise UnknownGroup(groupname)
|
||||
raise UnknownGroup(key)
|
||||
|
||||
if not group.is_changable():
|
||||
raise GroupCantBeChanged(groupname)
|
||||
raise GroupCantBeChanged(key)
|
||||
|
||||
if description is not None and description != group.get_description():
|
||||
group.change_description(description)
|
||||
@@ -293,27 +314,28 @@ class FilebasedGroupManager(GroupManager):
|
||||
|
||||
|
||||
class GroupAlreadyExists(Exception):
|
||||
def __init__(self, groupname):
|
||||
Exception.__init__(self, "Group %s already exists" % groupname)
|
||||
def __init__(self, key):
|
||||
Exception.__init__(self, "Group %s already exists" % key)
|
||||
|
||||
|
||||
class UnknownGroup(Exception):
|
||||
def __init__(self, groupname):
|
||||
Exception.__init__(self, "Unknown group: %s" % groupname)
|
||||
def __init__(self, key):
|
||||
Exception.__init__(self, "Unknown group: %s" % key)
|
||||
|
||||
|
||||
class GroupUnremovable(Exception):
|
||||
def __init__(self, groupname):
|
||||
Exception.__init__(self, "Group can't be removed: %s" % groupname)
|
||||
def __init__(self, key):
|
||||
Exception.__init__(self, "Group can't be removed: %s" % key)
|
||||
|
||||
|
||||
class GroupCantBeChanged(Exception):
|
||||
def __init__(self, groupname):
|
||||
Exception.__init__(self, "Group can't be changed: %s" % groupname)
|
||||
def __init__(self, key):
|
||||
Exception.__init__(self, "Group can't be changed: %s" % key)
|
||||
|
||||
|
||||
class Group(object):
|
||||
def __init__(self, name, description="", permissions=None, default=False, removable=True, changeable=True):
|
||||
def __init__(self, key, name, description="", permissions=None, default=False, removable=True, changeable=True):
|
||||
self._key = key
|
||||
self._name = name
|
||||
self._description = description
|
||||
self._permissions = permissions
|
||||
@@ -324,6 +346,7 @@ class Group(object):
|
||||
def as_dict(self):
|
||||
from octoprint.access.permissions import OctoPrintPermission
|
||||
return dict(
|
||||
key=self.key,
|
||||
name=self.get_name(),
|
||||
description=self._description,
|
||||
permissions=map(lambda p: p.key, self._permissions),
|
||||
@@ -333,6 +356,10 @@ class Group(object):
|
||||
changeable=self._changeable
|
||||
)
|
||||
|
||||
@property
|
||||
def key(self):
|
||||
return self._key
|
||||
|
||||
def get_name(self):
|
||||
return self._name
|
||||
|
||||
@@ -351,7 +378,7 @@ class Group(object):
|
||||
def add_permissions_to_group(self, permissions):
|
||||
"""Adds a list of permissions to a group"""
|
||||
if not self.is_changable():
|
||||
raise GroupCantBeChanged(self.get_name())
|
||||
raise GroupCantBeChanged(self.key)
|
||||
|
||||
# Make sure the permissions variable is of type list
|
||||
if not isinstance(permissions, list):
|
||||
@@ -370,7 +397,7 @@ class Group(object):
|
||||
def remove_permissions_from_group(self, permissions):
|
||||
"""Removes a list of permissions from a group"""
|
||||
if not self.is_changable():
|
||||
raise GroupCantBeChanged(self.get_name())
|
||||
raise GroupCantBeChanged(self.key)
|
||||
|
||||
# Make sure the permissions variable is of type list
|
||||
if not isinstance(permissions, list):
|
||||
@@ -389,7 +416,7 @@ class Group(object):
|
||||
def change_default(self, default):
|
||||
"""Changes the default flag of a Group"""
|
||||
if not self.is_changable():
|
||||
raise GroupCantBeChanged(self.get_name())
|
||||
raise GroupCantBeChanged(self.key)
|
||||
|
||||
self._default = default
|
||||
|
||||
@@ -407,6 +434,7 @@ class Group(object):
|
||||
@property
|
||||
def needs(self):
|
||||
needs = set()
|
||||
needs.add(GroupNeed(self.key))
|
||||
for p in self.permissions:
|
||||
needs = needs.union(p.needs)
|
||||
|
||||
@@ -419,8 +447,9 @@ class Group(object):
|
||||
return permission.needs.issubset(self.needs)
|
||||
|
||||
def __repr__(self):
|
||||
return '{}("{}", description="{}", permissions={!r}, ' \
|
||||
return '{}("{}", "{}", description="{}", permissions={!r}, ' \
|
||||
'default={}, removable={}, changeable={})'.format(self.__class__.__name__,
|
||||
self._key,
|
||||
self._name,
|
||||
self._description,
|
||||
self._permissions,
|
||||
@@ -429,10 +458,10 @@ class Group(object):
|
||||
bool(self._changeable))
|
||||
|
||||
def __hash__(self):
|
||||
return self.get_name().__hash__()
|
||||
return self.key.__hash__()
|
||||
|
||||
def __eq__(self, other):
|
||||
return isinstance(other, Group) and other.get_name() == self.get_name()
|
||||
return isinstance(other, Group) and other.key == self.key
|
||||
|
||||
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
# coding=utf-8
|
||||
from __future__ import absolute_import, division, print_function
|
||||
|
||||
__author__ = "Gina Häußge <osd@foosel.net>"
|
||||
__license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html'
|
||||
__copyright__ = "Copyright (C) 2014 The OctoPrint Project - Released under terms of the AGPLv3 License"
|
||||
|
||||
from flask_login import UserMixin, AnonymousUserMixin
|
||||
from flask_principal import Identity
|
||||
from werkzeug.local import LocalProxy
|
||||
import hashlib
|
||||
import os
|
||||
@@ -16,6 +14,8 @@ import wrapt
|
||||
import time
|
||||
|
||||
import logging
|
||||
|
||||
# noinspection PyCompatibility
|
||||
from builtins import range, bytes
|
||||
|
||||
from octoprint.settings import settings
|
||||
@@ -239,7 +239,7 @@ class UserManager(GroupChangeListener, object):
|
||||
return False
|
||||
|
||||
def on_group_removed(self, group):
|
||||
self._logger.debug("Group {} got removed, removing from all users".format(group.get_name()))
|
||||
self._logger.debug("Group {} got removed, removing from all users".format(group.key))
|
||||
self.remove_groups_from_users([group])
|
||||
|
||||
def on_group_permissions_changed(self, group, added=None, removed=None):
|
||||
@@ -504,7 +504,7 @@ class FilebasedUserManager(UserManager):
|
||||
return [self._group_manager.user_group]
|
||||
|
||||
def _refresh_groups(self, user):
|
||||
user._groups = self._to_groups(*map(lambda g: g.get_name(), user.groups))
|
||||
user._groups = self._to_groups(*map(lambda g: g.key, user.groups))
|
||||
|
||||
def add_user(self, username, password, active=False, permissions=None, groups=None, apikey=None, overwrite=False):
|
||||
if not permissions:
|
||||
@@ -769,7 +769,7 @@ class FilebasedUserManager(UserManager):
|
||||
[Permissions.find(permission) for permission in permissions])
|
||||
|
||||
def _from_groups(self, *groups):
|
||||
return [group.get_name() for group in groups]
|
||||
return [group.key for group in groups]
|
||||
|
||||
def _from_permissions(self, *permissions):
|
||||
return [permission.key for permission in permissions]
|
||||
@@ -888,7 +888,7 @@ class User(UserMixin):
|
||||
"name": self._username,
|
||||
"active": bool(self.is_active),
|
||||
"permissions": map(lambda p: p.key, self._permissions),
|
||||
"groups": map(lambda g: g.get_name(), self._groups),
|
||||
"groups": map(lambda g: g.key, self._groups),
|
||||
"needs": OctoPrintPermission.convert_needs_to_dict(self.needs),
|
||||
"apikey": self._apikey,
|
||||
"settings": self._settings,
|
||||
@@ -1039,21 +1039,24 @@ class User(UserMixin):
|
||||
@property
|
||||
def needs(self):
|
||||
needs = set()
|
||||
permissions = self.permissions
|
||||
for group in self.groups:
|
||||
if group is not None:
|
||||
permissions += group.permissions
|
||||
|
||||
for permission in permissions:
|
||||
for permission in self.permissions:
|
||||
if permission is not None:
|
||||
needs = needs.union(permission.needs)
|
||||
|
||||
for group in self.groups:
|
||||
if group is not None:
|
||||
needs = needs.union(group.needs)
|
||||
|
||||
return needs
|
||||
|
||||
def has_permission(self, permission):
|
||||
return self.has_needs(*permission.needs)
|
||||
|
||||
def has_needs(self, *needs):
|
||||
if Permissions.ADMIN in self._permissions:
|
||||
return True
|
||||
return permission.needs.issubset(self.needs)
|
||||
return set(needs).issubset(self.needs)
|
||||
|
||||
def __repr__(self):
|
||||
return "User(id=%s,name=%s,active=%r,user=True,admin=%r,permissions=%s,groups=%s)" % (self.get_id(), self.get_name(), bool(self.is_active), self.has_permission(Permissions.ADMIN), self._permissions, self._groups)
|
||||
|
||||
@@ -67,17 +67,17 @@ connectivityChecker = None
|
||||
principals = Principal(app)
|
||||
|
||||
import octoprint.access.permissions as permissions
|
||||
import octoprint.access.groups as groups
|
||||
|
||||
# we set admin_permission to Permissions.ADMIN
|
||||
# we set admin_permission to a GroupPermission with the default admin group
|
||||
admin_permission = util.variable_deprecated("admin_permission has been deprecated, "
|
||||
"please use individual Permissions instead",
|
||||
since="1.4.0")(permissions.Permissions.SETTINGS)
|
||||
since="1.4.0")(groups.GroupPermission(groups.ADMIN_GROUP))
|
||||
|
||||
# we set user_permission to Permissions.STATUS, since that's the minimal permission a logged in user should
|
||||
# probably have
|
||||
# we set user_permission to a GroupPermission with the default user group
|
||||
user_permission = util.variable_deprecated("user_permission has been deprecated, "
|
||||
"please use individual Permissions instead",
|
||||
since="1.4.0")(permissions.Permissions.STATUS)
|
||||
since="1.4.0")(groups.GroupPermission(groups.USER_GROUP))
|
||||
|
||||
# only import further octoprint stuff down here, as it might depend on things defined above to be initialized already
|
||||
from octoprint import __version__, __branch__, __display_version__, __revision__
|
||||
|
||||
@@ -44,38 +44,41 @@ def add_group():
|
||||
except BadRequest:
|
||||
return make_response("Malformed JSON body in request", 400)
|
||||
|
||||
if not "key" in data:
|
||||
return make_response("Missing mandatory key field", 400)
|
||||
if not "name" in data:
|
||||
return make_response("Missing mandatory name field", 400)
|
||||
if not "permissions" in data:
|
||||
return make_response("Missing mandatory permissions field", 400)
|
||||
|
||||
key = data["key"]
|
||||
name = data["name"]
|
||||
description = data.get("description", "")
|
||||
permissions = data["permissions"]
|
||||
default = data.get("default", False)
|
||||
|
||||
try:
|
||||
groupManager.add_group(name, description=description, permissions=permissions, default=default)
|
||||
groupManager.add_group(key, name, description=description, permissions=permissions, default=default)
|
||||
except groups.GroupAlreadyExists:
|
||||
abort(409)
|
||||
return get_groups()
|
||||
|
||||
|
||||
@api.route("/access/groups/<groupname>", methods=["GET"])
|
||||
@api.route("/access/groups/<key>", methods=["GET"])
|
||||
@restricted_access
|
||||
@Permissions.SETTINGS.require(403)
|
||||
def get_group(groupname):
|
||||
group = groupManager.find_group(groupname)
|
||||
def get_group(key):
|
||||
group = groupManager.find_group(key)
|
||||
if group is not None:
|
||||
return jsonify(group)
|
||||
else:
|
||||
abort(404)
|
||||
|
||||
|
||||
@api.route("/access/groups/<groupname>", methods=["PUT"])
|
||||
@api.route("/access/groups/<key>", methods=["PUT"])
|
||||
@restricted_access
|
||||
@Permissions.SETTINGS.require(403)
|
||||
def update_group(groupname):
|
||||
def update_group(key):
|
||||
if "application/json" not in request.headers["Content-Type"]:
|
||||
return make_response("Expected content-type JSON", 400)
|
||||
|
||||
@@ -96,7 +99,7 @@ def update_group(groupname):
|
||||
if "description" in data:
|
||||
kwargs["description"] = data["description"]
|
||||
|
||||
groupManager.update_group(groupname, **kwargs)
|
||||
groupManager.update_group(key, **kwargs)
|
||||
|
||||
return get_groups()
|
||||
except groups.GroupCantBeChanged:
|
||||
@@ -105,12 +108,12 @@ def update_group(groupname):
|
||||
abort(404)
|
||||
|
||||
|
||||
@api.route("/access/groups/<groupname>", methods=["DELETE"])
|
||||
@api.route("/access/groups/<key>", methods=["DELETE"])
|
||||
@restricted_access
|
||||
@Permissions.SETTINGS.require(403)
|
||||
def remove_group(groupname):
|
||||
def remove_group(key):
|
||||
try:
|
||||
groupManager.remove_group(groupname)
|
||||
groupManager.remove_group(key)
|
||||
return get_groups()
|
||||
except groups.UnknownGroup:
|
||||
abort(404)
|
||||
|
||||
@@ -48,11 +48,15 @@
|
||||
};
|
||||
|
||||
OctoPrintAccessGroupsClient.prototype.add = function (group, opts) {
|
||||
if (!group.key) {
|
||||
throw new OctoPrintClient.InvalidArgumentError("group key must be set");
|
||||
}
|
||||
if (!group.name) {
|
||||
throw new OctoPrintClient.InvalidArgumentError("group name need to be set");
|
||||
throw new OctoPrintClient.InvalidArgumentError("group name must be set");
|
||||
}
|
||||
|
||||
var data = {
|
||||
key: group.key,
|
||||
name: group.name,
|
||||
description: group.description,
|
||||
permissions: group.permissions,
|
||||
@@ -62,17 +66,17 @@
|
||||
return this.base.postJson(this.url(), data, opts);
|
||||
};
|
||||
|
||||
OctoPrintAccessGroupsClient.prototype.get = function (name, opts) {
|
||||
if (!name) {
|
||||
throw new OctoPrintClient.InvalidArgumentError("group name must be set");
|
||||
OctoPrintAccessGroupsClient.prototype.get = function (key, opts) {
|
||||
if (!key) {
|
||||
throw new OctoPrintClient.InvalidArgumentError("group key must be set");
|
||||
}
|
||||
|
||||
return this.base.get(this.url(name), opts);
|
||||
return this.base.get(this.url(key), opts);
|
||||
};
|
||||
|
||||
OctoPrintAccessGroupsClient.prototype.update = function (group, opts) {
|
||||
if (!group.name) {
|
||||
throw new OctoPrintClient.InvalidArgumentError("group name must be set");
|
||||
if (!group.key) {
|
||||
throw new OctoPrintClient.InvalidArgumentError("group key must be set");
|
||||
}
|
||||
|
||||
var data = {
|
||||
@@ -80,15 +84,15 @@
|
||||
permissions: group.permissions,
|
||||
default: group.default
|
||||
};
|
||||
return this.base.putJson(this.url(group.name), data, opts);
|
||||
return this.base.putJson(this.url(group.key), data, opts);
|
||||
};
|
||||
|
||||
OctoPrintAccessGroupsClient.prototype.delete = function (name, opts) {
|
||||
if (!name) {
|
||||
throw new OctoPrintClient.InvalidArgumentError("group name must be set");
|
||||
OctoPrintAccessGroupsClient.prototype.delete = function (key, opts) {
|
||||
if (!key) {
|
||||
throw new OctoPrintClient.InvalidArgumentError("group key must be set");
|
||||
}
|
||||
|
||||
return this.base.delete(this.url(name), opts);
|
||||
return this.base.delete(this.url(key), opts);
|
||||
};
|
||||
|
||||
//~~ users client api
|
||||
|
||||
@@ -327,6 +327,7 @@ $(function() {
|
||||
self.currentGroup = ko.observable(self.emptyGroup);
|
||||
|
||||
self.editor = {
|
||||
key: ko.observable(undefined),
|
||||
name: ko.observable(undefined),
|
||||
description: ko.observable(undefined),
|
||||
permissions: ko.observableArray([]),
|
||||
@@ -361,7 +362,7 @@ $(function() {
|
||||
return;
|
||||
|
||||
oldValue.forEach(function (p) {
|
||||
delete self[p.name.toUpperCase()];
|
||||
delete self[p.key.toUpperCase()];
|
||||
});
|
||||
}, null, "beforeChange");
|
||||
|
||||
@@ -382,7 +383,7 @@ $(function() {
|
||||
|
||||
// if the permission has no need sets do not register it.
|
||||
if (needs.length > 0) {
|
||||
self.registerGroup(g.name.toUpperCase(), needs);
|
||||
self.registerGroup(g.key.toUpperCase(), needs);
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -398,6 +399,7 @@ $(function() {
|
||||
self.currentGroup.subscribe(function(newValue) {
|
||||
if (newValue === undefined) {
|
||||
// group add
|
||||
self.editor.key(undefined);
|
||||
self.editor.name(undefined);
|
||||
self.editor.description(undefined);
|
||||
self.editor.permissions([]);
|
||||
@@ -407,6 +409,7 @@ $(function() {
|
||||
self.editor.confirm = self.confirmAddGroup
|
||||
} else {
|
||||
// group update
|
||||
self.editor.key(newValue.key);
|
||||
self.editor.name(newValue.name);
|
||||
self.editor.description(newValue.description);
|
||||
self.editor.permissions(newValue.permissions.slice(0));
|
||||
@@ -428,9 +431,9 @@ $(function() {
|
||||
var lookup = {};
|
||||
var defaults = [];
|
||||
_.each(response.groups, function(group) {
|
||||
lookup[group.name] = group;
|
||||
lookup[group.key] = group;
|
||||
if (group.default) {
|
||||
defaults.push(group.name);
|
||||
defaults.push(group.key);
|
||||
}
|
||||
});
|
||||
self.lookup = lookup;
|
||||
@@ -444,6 +447,7 @@ $(function() {
|
||||
|
||||
self.confirmAddGroup = function() {
|
||||
var group = {
|
||||
key: self.editor.name().toLowerCase().replace(/[^a-z0-9_ ]/g, "").replace(/ /g, "_"),
|
||||
name: self.editor.name(),
|
||||
description: self.editor.description(),
|
||||
permissions: self.editor.permissions(),
|
||||
@@ -468,7 +472,7 @@ $(function() {
|
||||
self.confirmEditGroup = function() {
|
||||
var group = self.currentGroup();
|
||||
|
||||
var data = { name: group.name };
|
||||
var data = { key: group.key, name: group.name };
|
||||
|
||||
if (group.description !== self.editor.description()) {
|
||||
data.description = self.editor.description();
|
||||
@@ -520,7 +524,7 @@ $(function() {
|
||||
message: _.sprintf(gettext("You are about to delete the group \"%(name)s\"."), {name: group.name}),
|
||||
proceed: gettext("Delete"),
|
||||
onproceed: function() {
|
||||
OctoPrint.access.groups.delete(group.name).done(function(response) {
|
||||
OctoPrint.access.groups.delete(group.key).done(function(response) {
|
||||
self.fromResponse(response);
|
||||
access.users.requestData();
|
||||
});
|
||||
@@ -635,8 +639,11 @@ $(function() {
|
||||
if (data.groups === undefined)
|
||||
return "";
|
||||
|
||||
data.groups.sort();
|
||||
return data.groups.join(", ");
|
||||
var mappedGroups = _.filter(_.map(data.groups, function(g) { return access.groups.lookup[g] }), function(g) { return g !== undefined });
|
||||
mappedGroups.sort(access.groupComparator);
|
||||
return _.map(mappedGroups, function(g) {
|
||||
return g.name;
|
||||
}).join(", ");
|
||||
};
|
||||
|
||||
// Maps the permission names into a comma separated list
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
<table class="table table-condensed table-hover settings_accesscontrol_groups_list">
|
||||
<tbody data-bind="foreach: groupsList()">
|
||||
<tr>
|
||||
<td class="settings_accesscontrol_groups_list_checkbox"><input type="checkbox" data-bind="checkedValue: name, checked: $parents[1].editor.groups"></td>
|
||||
<td class="settings_accesscontrol_groups_list_name" style="cursor: pointer" data-bind="click: function() { $parents[1].editor.toggleGroup($data.name) }, attr: {title: description}">
|
||||
<td class="settings_accesscontrol_groups_list_checkbox"><input type="checkbox" data-bind="checkedValue: key, checked: $parents[1].editor.groups"></td>
|
||||
<td class="settings_accesscontrol_groups_list_name" style="cursor: pointer" data-bind="click: function() { $parents[1].editor.toggleGroup($data.key) }, attr: {title: description}">
|
||||
<span class="name" data-bind="text: name, style: { fontWeight: ($parents[1].editor.groupSelected($data.name)) ? 'bold' : 'normal' }"></span><br>
|
||||
<small class="description" data-bind="text: description"></small><br>
|
||||
<small class="permissions muted" data-bind="text: $parents[1].editor.joinedGroupPermissions($data)"></small>
|
||||
|
||||
@@ -45,13 +45,13 @@
|
||||
<div class="modal-body">
|
||||
<form class="form-horizontal">
|
||||
<div class="control-group">
|
||||
<label class="control-label" for="settings-groupsDialogAddGroupName">{{ _('Group name') }}</label>
|
||||
<label class="control-label" for="settings-groupsDialogAddGroupName">{{ _('Name') }}</label>
|
||||
<div class="controls">
|
||||
<input type="text" class="input-block-level" id="settings-groupsDialogAddGroupName" data-bind="disable: !$root.access.groups.editor.new(), value: $root.access.groups.editor.name" required>
|
||||
<input type="text" class="input-block-level" id="settings-groupsDialogAddGroupName" data-bind="disable: !$root.access.groups.editor.new(), textInput: $root.access.groups.editor.name" required>
|
||||
</div>
|
||||
</div>
|
||||
<div class="control-group">
|
||||
<label class="control-label" for="settings-groupsDialogAddGroupDescription">{{ _('Group description') }}</label>
|
||||
<label class="control-label" for="settings-groupsDialogAddGroupDescription">{{ _('Description') }}</label>
|
||||
<div class="controls">
|
||||
<input type="text" class="input-block-level" id="settings-groupsDialogAddGroupDescription" data-bind="value: $root.access.groups.editor.description">
|
||||
</div>
|
||||
|
||||
Reference in new issue
Block a user