# Buggy code
def permissions_get(request):
    # Invert the permissions inheritance tree.
    perms_descending_tree = {}
    for obtained, obtained_from in PERMISSIONS_INHERITANCE_TREE.items():
        on_resource, obtained_perm = obtained.split(":", 1)
        for from_resource, perms in obtained_from.items():
            for perm in perms:
                perms_descending_tree.setdefault(from_resource, {}).setdefault(
                    perm, {}
                ).setdefault(on_resource, set()).add(obtained_perm)

    # Obtain current principals.
    principals = request.effective_principals
    if Authenticated in principals:
        # Since this view does not require any permission (can be used to
        # obtain public users permissions), we have to add the prefixed userid
        # among the principals (see :mode:`kinto.core.authentication`)
        userid = request.prefixed_userid
        principals.append(userid)

    # Query every possible permission of the current user from backend.
    backend = request.registry.permission
    perms_by_object_uri = backend.get_accessible_objects(principals)

    entries = []
    for object_uri, perms in perms_by_object_uri.items():
        # Obtain associated resource from object URI
        resource_name, matchdict = core_utils.view_lookup(request, object_uri)
        # For consistency with events payloads, prefix id with resource name
        matchdict[resource_name + "_id"] = matchdict.get("id")

        # Expand implicit permissions using descending tree.
        permissions = set(perms)
        for perm in perms:
            obtained = perms_descending_tree[resource_name][perm]
            # Related to same resource only and not every sub-objects.
            # (e.g "bucket:write" gives "bucket:read" but not "group:read")
            permissions |= obtained[resource_name]

        entry = dict(
            uri=object_uri,
            resource_name=resource_name,
            permissions=list(permissions),
            **matchdict,
        )
        entries.append(entry)

    return {"data": entries}


# Traceback (AttributeError)
ERROR  "GET   /v1/permissions" ? (? ms) 'NoneType' object has no attribute 'name' agent=node-fetch/1.0 (+https://github.com/bitinn/node-fetch) authn_type=basicauth errno=None exception=Traceback (most recent call last):
File "/home/mathieu/Code/Mozilla/kinto-client/.venv/local/lib/python2.7/site-packages/pyramid/tweens.py", line 22, in excview_tween
response = handler(request)
File "/home/mathieu/Code/Mozilla/kinto-client/.venv/local/lib/python2.7/site-packages/pyramid_tm/__init__.py", line 101, in tm_tween
reraise(*exc_info)
File "/home/mathieu/Code/Mozilla/kinto-client/.venv/local/lib/python2.7/site-packages/pyramid_tm/__init__.py", line 83, in tm_tween
response = handler(request)
File "/home/mathieu/Code/Mozilla/kinto-client/.venv/local/lib/python2.7/site-packages/pyramid/router.py", line 158, in handle_request
view_name
File "/home/mathieu/Code/Mozilla/kinto-client/.venv/local/lib/python2.7/site-packages/pyramid/view.py", line 547, in _call_view
response = view_callable(context, request)
File "/home/mathieu/Code/Mozilla/kinto-client/.venv/local/lib/python2.7/site-packages/pyramid/config/views.py", line 182, in __call__
return view(context, request)
File "/home/mathieu/Code/Mozilla/kinto-client/.venv/local/lib/python2.7/site-packages/pyramid/viewderivers.py", line 393, in attr_view
return view(context, request)
File "/home/mathieu/Code/Mozilla/kinto-client/.venv/local/lib/python2.7/site-packages/pyramid/viewderivers.py", line 371, in predicate_wrapper
return view(context, request)
File "/home/mathieu/Code/Mozilla/kinto-client/.venv/local/lib/python2.7/site-packages/pyramid/viewderivers.py", line 442, in rendered_view
result = view(context, request)
File "/home/mathieu/Code/Mozilla/kinto-client/.venv/local/lib/python2.7/site-packages/pyramid/viewderivers.py", line 147, in _requestonly_view
response = view(request)
File "/home/mathieu/Code/Mozilla/kinto-client/.venv/local/lib/python2.7/site-packages/cornice/service.py", line 573, in wrapper
response = view_(request)
File "/home/mathieu/Code/Mozilla/kinto/kinto/views/permissions.py", line 41, in permissions_get
resource_name, matchdict = core_utils.view_lookup(request, object_uri)
File "/home/mathieu/Code/Mozilla/kinto/kinto/core/utils.py", line 390, in view_lookup
resource_name = route.name.replace('-record', '')\
AttributeError: 'NoneType' object has no attribute 'name' lang=None uid=a9d81e1b56f2b77103e87601621a99b3d82a15abf7c72ce57a032d0964317c7a

# Fixed code
def permissions_get(request):
    # Invert the permissions inheritance tree.
    perms_descending_tree = {}
    for obtained, obtained_from in PERMISSIONS_INHERITANCE_TREE.items():
        on_resource, obtained_perm = obtained.split(":", 1)
        for from_resource, perms in obtained_from.items():
            for perm in perms:
                perms_descending_tree.setdefault(from_resource, {}).setdefault(
                    perm, {}
                ).setdefault(on_resource, set()).add(obtained_perm)

    # Obtain current principals.
    principals = request.effective_principals
    if Authenticated in principals:
        # Since this view does not require any permission (can be used to
        # obtain public users permissions), we have to add the prefixed userid
        # among the principals (see :mode:`kinto.core.authentication`)
        userid = request.prefixed_userid
        principals.append(userid)

    # Query every possible permission of the current user from backend.
    backend = request.registry.permission
    perms_by_object_uri = backend.get_accessible_objects(principals)

    entries = []
    for object_uri, perms in perms_by_object_uri.items():
        try:
            # Obtain associated resource from object URI
            resource_name, matchdict = core_utils.view_lookup(request, object_uri)
        except ValueError:
            # Skip permissions entries that are not linked to an object URI.
            continue

        # For consistency with events payloads, prefix id with resource name
        matchdict[resource_name + "_id"] = matchdict.get("id")

        # Expand implicit permissions using descending tree.
        permissions = set(perms)
        for perm in perms:
            obtained = perms_descending_tree[resource_name][perm]
            # Related to same resource only and not every sub-objects.
            # (e.g "bucket:write" gives "bucket:read" but not "group:read")
            permissions |= obtained[resource_name]

        entry = dict(
            uri=object_uri,
            resource_name=resource_name,
            permissions=list(permissions),
            **matchdict,
        )
        entries.append(entry)

    return {"data": entries}