Add permission list API endpoint.
This commit is contained in:
@@ -5,12 +5,25 @@ from rest_framework import generics
|
||||
from rest_api.filters import MayanObjectPermissionsFilter
|
||||
from rest_api.permissions import MayanPermission
|
||||
|
||||
from .classes import Permission
|
||||
from .models import Role
|
||||
from .permissions import (
|
||||
permission_role_create, permission_role_delete, permission_role_edit,
|
||||
permission_role_view
|
||||
)
|
||||
from .serializers import RoleSerializer
|
||||
from .serializers import PermissionSerializer, RoleSerializer
|
||||
|
||||
|
||||
class APIPermissionList(generics.ListAPIView):
|
||||
serializer_class = PermissionSerializer
|
||||
queryset = Permission.all()
|
||||
|
||||
def get(self, *args, **kwargs):
|
||||
"""
|
||||
Returns a list of all the available permissions.
|
||||
"""
|
||||
|
||||
return super(APIPermissionList, self).get(*args, **kwargs)
|
||||
|
||||
|
||||
class APIRoleListView(generics.ListCreateAPIView):
|
||||
|
||||
@@ -45,12 +45,15 @@ class PermissionNamespace(object):
|
||||
|
||||
|
||||
class Permission(object):
|
||||
_stored_permissions_cache = {}
|
||||
_permissions = {}
|
||||
_stored_permissions_cache = {}
|
||||
|
||||
@classmethod
|
||||
def invalidate_cache(cls):
|
||||
cls._stored_permissions_cache = {}
|
||||
def all(cls):
|
||||
# Return sorted permisions by namespace.name
|
||||
return sorted(
|
||||
cls._permissions.values(), key=lambda x: x.namespace.name
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def check_permissions(cls, requester, permissions):
|
||||
@@ -62,6 +65,14 @@ class Permission(object):
|
||||
|
||||
raise PermissionDenied(_('Insufficient permissions.'))
|
||||
|
||||
@classmethod
|
||||
def get(cls, get_dict, proxy_only=False):
|
||||
if 'pk' in get_dict:
|
||||
if proxy_only:
|
||||
return cls._permissions[get_dict['pk']]
|
||||
else:
|
||||
return cls._permissions[get_dict['pk']].stored_permission
|
||||
|
||||
@classmethod
|
||||
def get_for_holder(cls, holder):
|
||||
StoredPermission = apps.get_model(
|
||||
@@ -71,19 +82,8 @@ class Permission(object):
|
||||
return StoredPermission.get_for_holder(holder)
|
||||
|
||||
@classmethod
|
||||
def all(cls):
|
||||
# Return sorted permisions by namespace.name
|
||||
return sorted(
|
||||
cls._permissions.values(), key=lambda x: x.namespace.name
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def get(cls, get_dict, proxy_only=False):
|
||||
if 'pk' in get_dict:
|
||||
if proxy_only:
|
||||
return cls._permissions[get_dict['pk']]
|
||||
else:
|
||||
return cls._permissions[get_dict['pk']].stored_permission
|
||||
def invalidate_cache(cls):
|
||||
cls._stored_permissions_cache = {}
|
||||
|
||||
def __init__(self, namespace, name, label):
|
||||
self.namespace = namespace
|
||||
@@ -92,16 +92,15 @@ class Permission(object):
|
||||
self.pk = self.uuid
|
||||
self.__class__._permissions[self.uuid] = self
|
||||
|
||||
def __repr__(self):
|
||||
return self.pk
|
||||
|
||||
def __unicode__(self):
|
||||
return unicode(self.label)
|
||||
|
||||
def __str__(self):
|
||||
return str(self.__unicode__())
|
||||
|
||||
@property
|
||||
def uuid(self):
|
||||
return '%s.%s' % (self.namespace.name, self.name)
|
||||
|
||||
@property
|
||||
def stored_permission(self):
|
||||
StoredPermission = apps.get_model(
|
||||
@@ -120,3 +119,7 @@ class Permission(object):
|
||||
self.uuid
|
||||
] = stored_permission
|
||||
return stored_permission
|
||||
|
||||
@property
|
||||
def uuid(self):
|
||||
return '%s.%s' % (self.namespace.name, self.name)
|
||||
|
||||
@@ -2,7 +2,23 @@ from __future__ import unicode_literals
|
||||
|
||||
from rest_framework import serializers
|
||||
|
||||
from .models import Role
|
||||
from .models import Role, StoredPermission
|
||||
|
||||
|
||||
class PermissionSerializer(serializers.Serializer):
|
||||
namespace = serializers.CharField()
|
||||
pk = serializers.CharField()
|
||||
label = serializers.CharField()
|
||||
|
||||
def to_representation(self, instance):
|
||||
if isinstance(instance, StoredPermission):
|
||||
return super(PermissionSerializer, self).to_representation(
|
||||
instance.volatile_permission
|
||||
)
|
||||
else:
|
||||
return super(PermissionSerializer, self).to_representation(
|
||||
instance
|
||||
)
|
||||
|
||||
|
||||
class RoleSerializer(serializers.ModelSerializer):
|
||||
|
||||
@@ -2,7 +2,7 @@ from __future__ import unicode_literals
|
||||
|
||||
from django.conf.urls import patterns, url
|
||||
|
||||
from .api_views import APIRoleListView, APIRoleView
|
||||
from .api_views import APIPermissionList, APIRoleListView, APIRoleView
|
||||
from .views import (
|
||||
RoleCreateView, RoleDeleteView, RoleEditView, RoleListView,
|
||||
SetupRoleMembersView, SetupRolePermissionsView
|
||||
@@ -31,4 +31,5 @@ api_urls = patterns(
|
||||
'',
|
||||
url(r'^roles/$', APIRoleListView.as_view(), name='role-list'),
|
||||
url(r'^roles/(?P<pk>[0-9]+)/$', APIRoleView.as_view(), name='role-detail'),
|
||||
url(r'^permissions/$', APIPermissionList.as_view(), name='permission-list'),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user