Skip to content
This repository has been archived by the owner on Sep 28, 2022. It is now read-only.

Commit

Permalink
Merge pull request #99 from brandicted/acl-refactor
Browse files Browse the repository at this point in the history
Acl refactor
  • Loading branch information
postatum committed Aug 28, 2015
2 parents f895523 + f930c29 commit 8914220
Show file tree
Hide file tree
Showing 5 changed files with 213 additions and 218 deletions.
183 changes: 84 additions & 99 deletions nefertari/acl.py
Original file line number Diff line number Diff line change
@@ -1,134 +1,119 @@
from pyramid.security import ALL_PERMISSIONS, Allow, Everyone, Authenticated
from pyramid.security import(
ALL_PERMISSIONS,
Allow,
Everyone,
Authenticated,
)


class SelfParamMixin(object):
""" ACL mixin that implements method to translate input key value
to a user ID field, when key value equals :param_value:
class Contained(object):
"""Contained base class resource
Value is only converted if user is logged in and :request.user:
is an instance of :__context_class__:, thus for routes that display
auth users.
Can inherit its acl from its parent.
"""
param_value = 'self'

def resolve_self_key(self, key):
if key != self.param_value:
return key
user = getattr(self.request, 'user', None)
if not user or not self.__context_class__:
return key
if not isinstance(user, self.__context_class__):
return key
obj_id = getattr(user, user.pk_field()) or key
return obj_id


class BaseACL(SelfParamMixin):
""" Base ACL class.
Grants:
* all collection and item access to admins.
"""
__context_class__ = None

def __init__(self, request):
self.__acl__ = [(Allow, 'g:admin', ALL_PERMISSIONS)]
self.__context_acl__ = [(Allow, 'g:admin', ALL_PERMISSIONS)]
def __init__(self, request, name='', parent=None):
self.request = request
self.__name__ = name
self.__parent__ = parent

@property
def acl(self):
return self.__acl__

@acl.setter
def acl(self, val):
assert(isinstance(val, tuple))
self.__acl__.append(val)
class CollectionACL(Contained):
"""Collection resource.
def context_acl(self, obj):
return self.__context_acl__
You must specify the ``item_model``. It should be a nefertari.engine
document class. It is the model class for collection items.
def __getitem__(self, key):
assert(self.__context_class__)
key = self.resolve_self_key(key)
Define a ``__acl__`` attribute on this class to define the container's
permissions, and default child permissions. Inherits its acl from the
root, if no acl is set.
pk_field = self.__context_class__.pk_field()
obj = self.__context_class__.get(
__raise=True, **{pk_field: key})
obj.__acl__ = self.context_acl(obj)
obj.__parent__ = self
obj.__name__ = key
return obj
Override the `item_acl` method if you wish to provide custom acls for
collection items.
Override the `item_db_id` method if you wish to transform the collection
item db id, e.g. to support a ``self`` item on a user collection.
"""

class RootACL(object):
__acl__ = [
__acl__ = (
(Allow, 'g:admin', ALL_PERMISSIONS),
]
)

def __init__(self, request):
self.request = request
item_model = None

def __getitem__(self, key):
db_id = self.item_db_id(key)
pk_field = self.item_model.pk_field()
try:
item = self.item_model.get(
__raise=True, **{pk_field: db_id}
)
except AttributeError:
# strangely we get an AttributeError when the item isn't found
raise KeyError(key)
acl = self.item_acl(item)
if acl is not None:
item.__acl__ = acl
item.__parent__ = self
item.__name__ = key
return item

def item_acl(self, item):
return None

def item_db_id(self, key):
return key


def authenticated_userid(request):
"""Helper function that can be used in ``db_key`` to support `self`
as a collection key.
"""
user = request.user
key = user.pk_field()
return getattr(user, key)


class AdminACL(BaseACL):
""" Admin level ACL. Gives all access to all actions.
# Example ACL classes and base classes
#

May be used as a default factory for root resource.
"""
def __getitem__(self, key):
return 1
class RootACL(Contained):
__acl__ = (
(Allow, 'g:admin', ALL_PERMISSIONS),
)


class GuestACL(BaseACL):
""" Guest level ACL.
class GuestACL(CollectionACL):
"""Guest level ACL base class
Gives read permissions to everyone.
"""
def __init__(self, request):
super(GuestACL, self).__init__(request)
self.acl = (
Allow, Everyone, ['index', 'show', 'collection_options',
'item_options'])

def context_acl(self, obj):
return [
(Allow, 'g:admin', ALL_PERMISSIONS),
(Allow, Everyone, ['index', 'show', 'collection_options',
'item_options']),
]
__acl__ = (
(Allow, 'g:admin', ALL_PERMISSIONS),
(Allow, Everyone, ('view', 'options')),
)


class AuthenticatedReadACL(BaseACL):
""" Authenticated users' ACL.
class AuthenticatedReadACL(CollectionACL):
""" Authenticated users ACL base class
Gives read access to all Authenticated users.
Gives delete, create, update access to admin only.
"""
def __init__(self, request):
super(AuthenticatedReadACL, self).__init__(request)
self.acl = (Allow, Authenticated, ['index', 'collection_options'])

def context_acl(self, obj):
return [
(Allow, 'g:admin', ALL_PERMISSIONS),
(Allow, Authenticated, ['show', 'item_options']),
]
__acl__ = (
(Allow, 'g:admin', ALL_PERMISSIONS),
(Allow, Authenticated, ('view', 'options')),
)


class AuthenticationACL(BaseACL):
class AuthenticationACL(Contained):
""" Special ACL factory to be used with authentication views
(login, logout, register, etc.)
Allows 'create', 'show' and option methods to everyone.
Allows create, view and option methods to everyone.
"""
def __init__(self, request):
super(AuthenticationACL, self).__init__(request)
self.acl = (Allow, Everyone, [
'create', 'show', 'collection_options', 'item_options'])

def context_acl(self, obj):
return [
(Allow, 'g:admin', ALL_PERMISSIONS),
(Allow, Everyone, [
'create', 'show', 'collection_options', 'item_options']),
]

__acl__ = (
(Allow, 'g:admin', ALL_PERMISSIONS),
(Allow, Everyone, ('create', 'view', 'options')),
)
13 changes: 7 additions & 6 deletions nefertari/polymorphic.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
Polymorphic endpoints support all the read functionality regular ES
endpoint supports: query, search, filter, sort, aggregation, etc.
"""
from pyramid.security import DENY_ALL, Allow
from pyramid.security import DENY_ALL, Allow, ALL_PERMISSIONS

from nefertari.view import BaseView
from nefertari.acl import BaseACL
from nefertari.acl import CollectionACL
from nefertari.utils import dictset


Expand Down Expand Up @@ -75,7 +75,7 @@ def get_resources(self, collections):
return set(resources)


class PolymorphicACL(PolymorphicHelperMixin, BaseACL):
class PolymorphicACL(PolymorphicHelperMixin, CollectionACL):
""" ACL used by PolymorphicESView.
Generates ACEs checking whether current request user has 'index'
Expand Down Expand Up @@ -118,14 +118,15 @@ def set_collections_acl(self):
DENY_ALL is added to ACL to make sure no access rules are
inherited.
"""
self.__acl__ = []
acl = [(Allow, 'g:admin', ALL_PERMISSIONS)]
collections = self.get_collections()
resources = self.get_resources(collections)
aces = self._get_least_permissions_aces(resources)
if aces is not None:
for ace in aces:
self.acl = ace
self.acl = DENY_ALL
acl.append(ace)
acl.append(DENY_ALL)
self.__acl__ = tuple(acl)


class PolymorphicESView(PolymorphicHelperMixin, BaseView):
Expand Down
18 changes: 17 additions & 1 deletion nefertari/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@
ACTIONS = ['index', 'show', 'create', 'update',
'delete', 'update_many', 'delete_many',
'replace']
PERMISSIONS = {
'index': 'view',
'show': 'view',
'create': 'create',
'update': 'update',
'update_many': 'update',
'delete': 'delete',
'delete_many': 'delete',
'replace': 'update',
'collection_options': 'options',
'item_options': 'options',
}
DEFAULT_ID_NAME = 'id'


Expand Down Expand Up @@ -103,9 +115,13 @@ def add_route_and_view(config, action, route_name, path, request_method,

action_route[action] = route_name

if _auth:
permission = PERMISSIONS[action]
else:
permission = None
config.add_view(view=view, attr=action, route_name=route_name,
request_method=request_method,
permission=action if _auth else None,
permission=permission,
**kwargs)
config.commit()

Expand Down

0 comments on commit 8914220

Please sign in to comment.