I want to handle my eck entity types access trough hook_entity_access() because I need dynamic permissions. It works on operations "view" and "update". But on creating... the hook doesn't fire. It works with nodes for example.

Steps:
1. Create eck entity type.
2. Create eck entity bundle
3. Create eck entity bundle text field.
4. Add hook_entity_access() to a custom module and check if the hook is working.
5. Try to add a eck entity. (/admin/content/entity_type/add/entity_bundle)
6. Result: No access control over the hook.

My question: Why? Is it a bug?

Drupal: 8.5.0
ECK: 8.x-1.0-alpha3+8-dev

Comments

handkerchief created an issue. See original summary.

handkerchief’s picture

Category: Support request » Bug report
handkerchief’s picture

anyone with a hint?

handkerchief’s picture

Title: hook_entity_access not fire on add operation » hook_entity_access doesn't fire on add operation
handkerchief’s picture

Issue summary: View changes
murz’s picture

Seems I have same issue, did you find any workaround?
In Router I try to check ECK entity access permission via:

mymodule.mycustomentity.print:
  path: '/mycustomentity/{mycustomentity}/print'
  defaults:
    _controller: '\Drupal\mycustomentity\Controller\MymoduleController::print'
    _title: 'Print'
  requirements:
    _entity_access: 'mycustomentity.view'

and this page always return permission denied, even for Admin user, that have all permissions.

handkerchief’s picture

My workaround: Handle the permission in a hook_form_alter
If the user has no permission, set: $form['#access'] = FALSE;

alex.ksis’s picture

I tried to use hook_entity_access and it is really does not work. However I do not think that this is a bug as there is a separated hook: hook_entity_create_access()

matroskeen’s picture

Status: Active » Postponed (maintainer needs more info)

For create operations use hook_entity_create_access(), and hook_entity_access() for others.
See: https://www.drupal.org/node/2095227.

fox mulder’s picture

A reminder to those who do similar work: access hooks doesn't fire for admin ( uid=1 )

andres.torres’s picture

Im experiencing the same issue, hook_entity_access or hook_eck_entity_access dont work on drupal 8.7.1. Eck entities just wont show up on View state

zebda’s picture

Having the same problem, what can I do to solve this?

dieterholvoet’s picture

Status: Postponed (maintainer needs more info) » Closed (works as designed)

Having the same problem, what can I do to solve this?

Check the responses by Matroskeen and fox mulder. Everything seems to be working as expected here. If you're still having problems, please create a new issue with a specific description of the problem and clear steps to reproduce.

garethhallnz’s picture

I just encountered this problem too. After some debugging, I discovered that the issue was due to having the "bypass ECK entity access" permission. When you have this permission, the hook_entity_access is never called.

As per /src/EckEntityAccessControlHandler.php#L52

  public function access(EntityInterface $entity, $operation, AccountInterface $account = NULL, $return_as_object = FALSE) {
    if ($this->canBypassAccessCheck($account)) {
      return $this->getBypassAccessResult($return_as_object);
    }
    else {
      return parent::access($entity, $operation, $account, $return_as_object);
    }
  }