More bugfixes, additionally add a can() function to the PluginIdentityContextManager, and replace the dicts with OrderedDicts
This commit is contained in:
1 parent
d0d1da3456
commit
15c8bd7d6d
3 files changed
+23
-10
No files matched your search
@@ -10,6 +10,7 @@ from flask import g, abort
|
||||
from flask_babel import gettext
|
||||
from flask_principal import Permission, PermissionDenied, RoleNeed
|
||||
from functools import wraps
|
||||
from collections import OrderedDict
|
||||
import logging
|
||||
|
||||
|
||||
@@ -72,6 +73,7 @@ class OctoPrintPermission(Permission):
|
||||
|
||||
return '{0}("{1}", "{2}", {3})'.format(self.__class__.__name__, self.get_name(), self.get_description(), ', '.join(needs))
|
||||
|
||||
|
||||
class PluginIdentityContext(object):
|
||||
"""Identity context for not initialized Permissions
|
||||
|
||||
@@ -91,6 +93,15 @@ class PluginIdentityContext(object):
|
||||
"""
|
||||
return g.identity
|
||||
|
||||
def can(self):
|
||||
"""Whether the identity has access to the permission
|
||||
"""
|
||||
permission = Permissions.__getattr__(self.key)
|
||||
if permission is None or isinstance(permission, PluginPermissionDecorator):
|
||||
raise UnknownPermission(self.key)
|
||||
|
||||
return permission.can()
|
||||
|
||||
def __call__(self, f):
|
||||
@wraps(f)
|
||||
def _decorated(*args, **kw):
|
||||
@@ -102,7 +113,7 @@ class PluginIdentityContext(object):
|
||||
|
||||
def __enter__(self):
|
||||
permission = Permissions.__getattr__(self.key)
|
||||
if isinstance(permission, PluginPermissionDecorator):
|
||||
if permission is None or isinstance(permission, PluginPermissionDecorator):
|
||||
raise UnknownPermission(self.key)
|
||||
|
||||
# check the permission here
|
||||
@@ -114,6 +125,7 @@ class PluginIdentityContext(object):
|
||||
def __exit__(self, *args):
|
||||
return False
|
||||
|
||||
|
||||
class PluginPermissionDecorator(Permission):
|
||||
"""Decorator Class for not initialized Permissions
|
||||
|
||||
@@ -128,7 +140,8 @@ class PluginPermissionDecorator(Permission):
|
||||
|
||||
class PermissionManager(object):
|
||||
def __init__(self):
|
||||
self._permissions = dict()
|
||||
self._permissions = OrderedDict()
|
||||
|
||||
self.logger = logging.getLogger(__name__)
|
||||
|
||||
import yaml
|
||||
@@ -192,8 +205,7 @@ class PermissionManager(object):
|
||||
|
||||
class Permissions:
|
||||
class PluginPermissionMetaclass(type):
|
||||
|
||||
plugin_permissions = dict()
|
||||
plugin_permissions = OrderedDict()
|
||||
|
||||
@classmethod
|
||||
def __setattr__(cls, key, value):
|
||||
@@ -216,6 +228,7 @@ class Permissions:
|
||||
|
||||
__metaclass__ = PluginPermissionMetaclass
|
||||
|
||||
|
||||
# Special permission
|
||||
ADMIN = OctoPrintPermission("Admin", gettext("Admin is allowed to do everything"), RoleNeed("admin"))
|
||||
|
||||
|
||||
@@ -35,9 +35,9 @@
|
||||
<!-- Modals for group management -->
|
||||
|
||||
<script type="text/html" id="group-permission-template">
|
||||
<div class="controls" data-bind="foreach: _.filter($root.access.permissions.permissionsList(), $root.access.groups.filterPermission)">
|
||||
<div class="controls" data-bind="foreach: $root.access.permissions.permissionsList()">
|
||||
<label class="checkbox">
|
||||
<input type="checkbox" data-bind="checkedValue: $data, checked: $root.access.groups.editorPermissions"><span data-bind="text: name, attr: { title: description }"></span><br>
|
||||
<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>
|
||||
</label>
|
||||
</div>
|
||||
|
||||
@@ -38,17 +38,17 @@
|
||||
<!-- Modals for user management -->
|
||||
|
||||
<script type="text/html" id="users-permission-template">
|
||||
<!-- ko foreach: _.filter($root.access.permissions.permissionsList(), $root.access.users.filterPermission) -->
|
||||
<!-- ko foreach: $root.access.permissions.permissionsList() -->
|
||||
<label class="checkbox">
|
||||
<input type="checkbox" data-bind="checkedValue: $data, checked: $root.access.users.editorPermissions"><span data-bind="text: name"></span><br>
|
||||
<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>
|
||||
</label>
|
||||
<!-- /ko -->
|
||||
</script>
|
||||
<script type="text/html" id="users-group-template">
|
||||
<!-- ko foreach: _.filter($root.access.groups.groupsList(), $root.access.users.filterGroup) -->
|
||||
<!-- ko foreach: $root.access.groups.groupsList() -->
|
||||
<label class="checkbox">
|
||||
<input type="checkbox" data-bind="checkedValue: $data, checked: $root.access.users.editorGroups"><span data-bind="text: name"></span><br>
|
||||
<input type="checkbox" data-bind="checkedValue: name, checked: $root.access.users.editorGroups"><span data-bind="text: name"></span><br>
|
||||
<span class="description" data-bind="text: description"></span>
|
||||
</label>
|
||||
<!-- /ko -->
|
||||
|
||||
Reference in new issue
Block a user