If I understand correctly, IEF completely bypasses access checks and relies on access to the content creating the reference to provide any semblance of access permissions.

This is just a placeholder to discuss and attempt to provide access checks if, when & where sane.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jpstrikesback’s picture

I have some rudimentary code that checks create access in entity_access() for each bundle in the widget setup, unsettling any that return false and then wraps the add new/existing form to check if no bundles are left (it replaces the form with an access warning message) it works for node creation but I imagine only for nodes and other access callbacks that follow the pattern of allowing an entity bundle to be passed in place of the entity object. That doesn't take in account update access for existing entities. Makes me think it should be done at the controller level so each entity with proper support can specify a callback that can check per entity type, bundle and entity using whatever means necessary...unless entity API should be getting beefed up here...

bojanz’s picture

Category: feature » task
Status: Active » Fixed

Committed:
http://drupalcode.org/project/inline_entity_form.git/commitdiff/60fd2db?...

Summary:
1) The product reference autocomplete now checks for "view" access the same way the entityreference one does.
2) The edit button checks for the "update" access.
3) If "allow_existing" is off, the default remove operation is delete, so the remove button checks for "delete" access.
4) If "allow_existing" is on, the default remove operation is unlink, so the remove button is shown, but the "Delete this entity from the system" checkbox is only shown if the user has "delete access".
5) The bundles listed for "Add new" are now checked for "create" access. If none are available, the "Add new" button is not shown at all.

jpstrikesback’s picture

Wow, nice!

bojanz’s picture

jpstrikesback’s picture

Status: Fixed » Needs review
FileSize
2.09 KB

Opening this up again for one little issue.

When I added my rudimentary code as mentioned above, I wrapped the Create & Add Existing form elements in a check for "create_bundles". That is use-case specific as I needed to restrict create & add to those with create access, and a good default is of course to allow referencing even when create access is denied which I found when updating to the current dev.

This makes me think an additional setting would be nice along with "Allow Existing", something along the lines of "Allow users to add existing @label without requiring @singular_label bundle create access.". Then in the form check for any create_bundles OR this setting in addition to the current check for allow_existing.

Patch attached does this.

jpstrikesback’s picture

Here's a version that keeps the current behaviour by default.

bojanz’s picture

Status: Needs review » Needs work

The "create" permission is not supposed to be needed for referencing existing entities at all.
That sounds like a bug.

jpstrikesback’s picture

Status: Needs review » Needs work

You're right that the create entity access is not currently needed to reference an entity, so there is no bug. What I'm saying is that I needed this restriction for my use case and others might as well in a content context so I've added the setting above to enable restricting referencing if a user does not have create access, or that the need for access has not been bypassed.

jpstrikesback’s picture

Status: Needs work » Needs review
jpstrikesback’s picture

Here is the same thing with reversed logic. In this version we add a setting that allows a user to "Require entity bundle create access to reference existing entities", the logic may seem ever so slightly convoluted, but the UX makes more sense I guess.

Screen Shot 2012-12-21 at 11.04.18 AM.png

To explain my use-case a little more and why I think this is relevant to the module in general. The site allows a user to add a node while anonymous (it triggers a purchase). On this node there is an inline entity form to add another node of a different type, but this type of node has create access restrictions until they have created an account and are logged in. Furthermore they may only reference nodes they have created (Entity Reference is using the views select to get a list of user owned items). Thus it makes sense to only allow referencing existing that they have created and if not logged in not present the add existing form. I don't think this is an edge case, or wont be once IEF goes multi platinum :)

Also if you come around to liking this :) then I think it might be nice if when a user doesn't have create or reference access (because it has been denied when there is no create access) that there be a little check on the form that puts a little warning in the field markup along the lines of:

      // If the user does not have entity create access and we have restricted
      // referencing existing entities then we present a warning explaining
      // why they cannot add or reference anything.
      if (!count($settings['create_bundles']) && $controller->getSetting('existing_require_create_access')) {
        $element['actions'] = array(
          '#type' => 'container',
          '#suffix' => t('<div class="messages warning">* You do not yet have access to add or reference any @type_plural.</div>', array('@type_plural' => $labels['plural'])),
        );
      }

Screen Shot 2012-12-21 at 10.45.29 AM.png

jpstrikesback’s picture

*cough* :)

Mile23’s picture

Just an FYI... I started getting error notices about accessing properties of non-objects when I started using IEF. They were in Entity API and relate to access checks for user and node entities.

My scenario: I added an entity reference field to a node with the IEF widget, and then tried to allow the user to add nodes or users through it.

Here are a couple of (hopefully) relevant issues.

#1237014: Notices in entity_metadata_user_access() when loading an anonymous node

#1780646: entity_access() fails to check node type specific create access

bojanz’s picture

So, I'm 6 months late with a review. I'm truly sorry about that.

I don't think we should be adding to warning to IEF itself, since Drupal in general doesn't do that in other places (you just don't see what you can't access or do).

I like the idea of a patch that adds a setting that requires "create" permissions.
I've cleaned up yours, and am attaching it.
However, the patch is not complete so I can't commit it with this release.
We also need to alter the autocomplete queries to respect the "create" permission, otherwise you'd get this situation:
- "I can view node types: book, article, product"
- "I can create node type: book"
- "I IEF-manage a field that accepts all three node types. Since I can create book, it allows me to open the add existing form. But then in the dropdown I can still select article and product too, nothing stops me".

bojanz’s picture

Title: Add entity & bundle access checks » Add entity & bundle access checks [FOLLOWUP]
Status: Needs review » Needs work

Setting appropriate status.

jpstrikesback’s picture

No worries, glad one of the ideas is useful! Thanks for the thorough review and a direction forward as well!

  • bojanz committed 60fd2db on 8.x-1.x
    Issue #1859834: Add entity & bundle access checks.