Simplify permission lookup method. Add refresh class method to make sure
stored permissions are in sync with proxy permission. Add __repr__ method.
This commit is contained in:
@@ -13,7 +13,7 @@ from common.views import (
|
|||||||
AssignRemoveView, SingleObjectCreateView, SingleObjectDeleteView,
|
AssignRemoveView, SingleObjectCreateView, SingleObjectDeleteView,
|
||||||
SingleObjectListView
|
SingleObjectListView
|
||||||
)
|
)
|
||||||
from permissions import PermissionNamespace
|
from permissions import PermissionNamespace, Permission
|
||||||
from permissions.models import StoredPermission
|
from permissions.models import StoredPermission
|
||||||
|
|
||||||
from .classes import ModelPermission
|
from .classes import ModelPermission
|
||||||
@@ -228,6 +228,7 @@ class ACLPermissionsView(AssignRemoveView):
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
def left_list(self):
|
def left_list(self):
|
||||||
|
Permission.refresh()
|
||||||
return ACLPermissionsView.generate_choices(self.get_available_list())
|
return ACLPermissionsView.generate_choices(self.get_available_list())
|
||||||
|
|
||||||
def remove(self, item):
|
def remove(self, item):
|
||||||
|
|||||||
@@ -45,12 +45,15 @@ class PermissionNamespace(object):
|
|||||||
|
|
||||||
|
|
||||||
class Permission(object):
|
class Permission(object):
|
||||||
_stored_permissions_cache = {}
|
|
||||||
_permissions = {}
|
_permissions = {}
|
||||||
|
_stored_permissions_cache = {}
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def invalidate_cache(cls):
|
def all(cls):
|
||||||
cls._stored_permissions_cache = {}
|
# Return sorted permisions by namespace.name
|
||||||
|
return sorted(
|
||||||
|
cls._permissions.values(), key=lambda x: x.namespace.name
|
||||||
|
)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def check_permissions(cls, requester, permissions):
|
def check_permissions(cls, requester, permissions):
|
||||||
@@ -67,6 +70,13 @@ class Permission(object):
|
|||||||
|
|
||||||
raise PermissionDenied(_('Insufficient permissions.'))
|
raise PermissionDenied(_('Insufficient permissions.'))
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def get(cls, pk, proxy_only=False):
|
||||||
|
if proxy_only:
|
||||||
|
return cls._permissions[pk]
|
||||||
|
else:
|
||||||
|
return cls._permissions[pk].stored_permission
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_for_holder(cls, holder):
|
def get_for_holder(cls, holder):
|
||||||
StoredPermission = apps.get_model(
|
StoredPermission = apps.get_model(
|
||||||
@@ -76,19 +86,13 @@ class Permission(object):
|
|||||||
return StoredPermission.get_for_holder(holder)
|
return StoredPermission.get_for_holder(holder)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def all(cls):
|
def invalidate_cache(cls):
|
||||||
# Return sorted permisions by namespace.name
|
cls._stored_permissions_cache = {}
|
||||||
return sorted(
|
|
||||||
cls._permissions.values(), key=lambda x: x.namespace.name
|
|
||||||
)
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get(cls, get_dict, proxy_only=False):
|
def refresh(cls):
|
||||||
if 'pk' in get_dict:
|
for permission in cls.all():
|
||||||
if proxy_only:
|
permission.stored_permission
|
||||||
return cls._permissions[get_dict['pk']]
|
|
||||||
else:
|
|
||||||
return cls._permissions[get_dict['pk']].stored_permission
|
|
||||||
|
|
||||||
def __init__(self, namespace, name, label):
|
def __init__(self, namespace, name, label):
|
||||||
self.namespace = namespace
|
self.namespace = namespace
|
||||||
@@ -97,16 +101,15 @@ class Permission(object):
|
|||||||
self.pk = self.uuid
|
self.pk = self.uuid
|
||||||
self.__class__._permissions[self.uuid] = self
|
self.__class__._permissions[self.uuid] = self
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return self.pk
|
||||||
|
|
||||||
def __unicode__(self):
|
def __unicode__(self):
|
||||||
return unicode(self.label)
|
return unicode(self.label)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return str(self.__unicode__())
|
return str(self.__unicode__())
|
||||||
|
|
||||||
@property
|
|
||||||
def uuid(self):
|
|
||||||
return '%s.%s' % (self.namespace.name, self.name)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def stored_permission(self):
|
def stored_permission(self):
|
||||||
StoredPermission = apps.get_model(
|
StoredPermission = apps.get_model(
|
||||||
@@ -125,3 +128,7 @@ class Permission(object):
|
|||||||
self.uuid
|
self.uuid
|
||||||
] = stored_permission
|
] = stored_permission
|
||||||
return stored_permission
|
return stored_permission
|
||||||
|
|
||||||
|
@property
|
||||||
|
def uuid(self):
|
||||||
|
return '%s.%s' % (self.namespace.name, self.name)
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ class Command(BaseCommand):
|
|||||||
for permission in StoredPermission.objects.all():
|
for permission in StoredPermission.objects.all():
|
||||||
try:
|
try:
|
||||||
Permission.get(
|
Permission.get(
|
||||||
{'pk': '%s.%s' % (permission.namespace, permission.name)},
|
pk='{}.{}'.format(permission.namespace, permission.name),
|
||||||
proxy_only=True
|
proxy_only=True
|
||||||
)
|
)
|
||||||
except KeyError:
|
except KeyError:
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ class StoredPermission(models.Model):
|
|||||||
super(StoredPermission, self).__init__(*args, **kwargs)
|
super(StoredPermission, self).__init__(*args, **kwargs)
|
||||||
try:
|
try:
|
||||||
self.volatile_permission = Permission.get(
|
self.volatile_permission = Permission.get(
|
||||||
{'pk': '%s.%s' % (self.namespace, self.name)},
|
pk='{}.{}'.format(self.namespace, self.name),
|
||||||
proxy_only=True
|
proxy_only=True
|
||||||
)
|
)
|
||||||
except KeyError:
|
except KeyError:
|
||||||
|
|||||||
@@ -63,8 +63,7 @@ class RoleNewPermissionSerializer(serializers.Serializer):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
for pk in validated_data['permission_pk_list'].split(','):
|
for pk in validated_data['permission_pk_list'].split(','):
|
||||||
get_dict = {'pk': pk}
|
stored_permission = Permission.get(pk=pk)
|
||||||
stored_permission = Permission.get(get_dict=get_dict)
|
|
||||||
|
|
||||||
validated_data['role'].permissions.add(stored_permission)
|
validated_data['role'].permissions.add(stored_permission)
|
||||||
except KeyError as exception:
|
except KeyError as exception:
|
||||||
|
|||||||
@@ -97,7 +97,9 @@ class SetupRolePermissionsView(AssignRemoveView):
|
|||||||
return get_object_or_404(Role, pk=self.kwargs['pk'])
|
return get_object_or_404(Role, pk=self.kwargs['pk'])
|
||||||
|
|
||||||
def left_list(self):
|
def left_list(self):
|
||||||
|
Permission.refresh()
|
||||||
results = []
|
results = []
|
||||||
|
|
||||||
for namespace, permissions in itertools.groupby(StoredPermission.objects.exclude(id__in=self.get_object().permissions.values_list('pk', flat=True)), lambda entry: entry.namespace):
|
for namespace, permissions in itertools.groupby(StoredPermission.objects.exclude(id__in=self.get_object().permissions.values_list('pk', flat=True)), lambda entry: entry.namespace):
|
||||||
permission_options = [
|
permission_options = [
|
||||||
(unicode(permission.pk), permission) for permission in permissions
|
(unicode(permission.pk), permission) for permission in permissions
|
||||||
|
|||||||
Reference in New Issue
Block a user