ergo / ziggurat_foundations

Framework agnostic set of sqlalchemy classes that make building applications that require permissions an easy task.
BSD 3-Clause "New" or "Revised" License
71 stars 22 forks source link

Exception: The model should have implemented __acl__ #40

Closed peletiah closed 8 years ago

peletiah commented 8 years ago

I'm running into this exception when following the documentation regarding "resource based pyramid context factory"

It's caused by this line from the example:

# append basic resource acl that gives all permissions to owner
self.__acl__ = self.resource.__acl__

Where am I supposed to define these acl and what's the purpose? Would this be a static acl, similar to RootFactory? DB-based permissions are not affected when I remove this line, so I'm not sure if I need to worry about it?! Please kindly clarify!

ergo commented 8 years ago

Hi @peletiah, can you paste the exception for me to see?

https://github.com/ergo/ziggurat_foundations/blob/master/ziggurat_foundations/models/resource.py#L94

Normally this should return something like [(Allow, ownerid/groupid, ALL_PERMISSIONS)] so it auto appends basic set of permissions without additional sql queries. If you want - you can make it empty list.

peletiah commented 8 years ago

Here's the traceback:

2016-06-15 09:26:21,942 ERROR [pyramid_debugtoolbar:227][waitress] Exception at http://localhost:6543/route/1/1
traceback url: http://localhost:6543/_debug_toolbar/exception?token=62273c5c7864655c7864665c7862375c7861357e5c7830365c7864372c7227&tb=139854886359840
Traceback (most recent call last):
  File "/home/benke/myproject/lib/python3.4/site-packages/pyramid_debugtoolbar/toolbar.py", line 209, in toolbar_tween
    response = _handler(request)
  File "/home/benke/myproject/lib/python3.4/site-packages/pyramid_debugtoolbar/panels/performance.py", line 57, in resource_timer_handler
    result = handler(request)
  File "/home/benke/myproject/lib/python3.4/site-packages/pyramid/tweens.py", line 51, in excview_tween
    request_iface=request_iface.combined
  File "/home/benke/myproject/lib/python3.4/site-packages/pyramid/view.py", line 547, in _call_view
    response = view_callable(context, request)
  File "/home/benke/myproject/lib/python3.4/site-packages/pyramid/viewderivers.py", line 413, in viewresult_to_response
    result = view(context, request)
  File "/home/benke/myproject/lib/python3.4/site-packages/pyramid/tweens.py", line 22, in excview_tween
    response = handler(request)
  File "/home/benke/myproject/lib/python3.4/site-packages/pyramid_tm/__init__.py", line 101, in tm_tween
    reraise(*exc_info)
  File "/home/benke/myproject/lib/python3.4/site-packages/pyramid_tm/compat.py", line 15, in reraise
    raise value
  File "/home/benke/myproject/lib/python3.4/site-packages/pyramid_tm/__init__.py", line 83, in tm_tween
    response = handler(request)
  File "/home/benke/myproject/lib/python3.4/site-packages/pyramid/router.py", line 127, in handle_request
    root = root_factory(request)
  File "/home/benke/myproject/myproject_api/myproject_api/security.py", line 82, in __init__
    self.__acl__ = self.resource.__acl__
  File "/home/benke/myproject/lib/python3.4/site-packages/ziggurat_foundations/models/resource.py", line 96, in __acl__
    raise Exception("The model should have implemented __acl__")
Exception: The model should have implemented __acl__

ResourceFactory in security.py is a copy of your's in the aforementioned documentation:

class ResourceFactory(object):
    def __init__(self, request):
        self.__acl__ = []
        rid = request.matchdict.get("resource_id")

        if not rid:
            raise HTTPBadRequest()
        self.resource = Resource.by_resource_id(rid,db_session=request.dbsession)
        if not self.resource:
            raise HTTPNotFound()
        if self.resource and request.user:
            # append basic resource acl that gives all permissions to owner
            self.__acl__ = self.resource.__acl__
            # append permissions that current user may have for this context resource
            permissions = self.resource.perms_for_user(request.user)
            for outcome, perm_user, perm_name in permission_to_pyramid_acls(
                    permissions):
                self.__acl__.append((outcome, perm_user, perm_name,))

I'm running Pyramid 1.7b4 and ziggurat-foundations 0.6.7

ergo commented 8 years ago

@peletiah thanks, you should implement something on your model you can return [] if you don't want anything better - or just dont access resource.__acl__ at all.

ergo commented 8 years ago

I think if the problem is clear I can close this for now. Feel free to reopen if you have any questions.