entityTypeManager = $entity_type_manager; $this->domainElementManager = $domain_element_manager; $this->userStorage = $user_storage; } /** * {@inheritdoc} */ public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) { return new static( $entity_type, $container->get('entity_type.manager'), $container->get('domain.element_manager'), $container->get('entity_type.manager')->getStorage('user') ); } /** * {@inheritdoc} */ public function checkAccess(EntityInterface $entity, $operation, AccountInterface $account = NULL) { $account = $this->prepareUser($account); // Check the global permission. if ($account->hasPermission('administer domains')) { return AccessResult::allowed(); } // @TODO: This may not be relevant. if ($operation == 'create' && $account->hasPermission('create domains')) { return AccessResult::allowed(); } // For view, we allow admins unless the domain is inactive. $is_admin = $this->isDomainAdmin($entity, $account); if ($operation == 'view' && ($entity->status() || $account->hasPermission('access inactive domains')) && ($is_admin || $account->hasPermission('view domain list'))) { return AccessResult::allowed(); } // For other operations, check that the user is a domain admin. if ($operation == 'update' && $account->hasPermission('edit assigned domains') && $is_admin) { return AccessResult::allowed(); } if ($operation == 'delete' && $account->hasPermission('delete assigned domains') && $is_admin) { return AccessResult::allowed(); } return AccessResult::forbidden(); } /** * Checks if a user can administer a specific domain. * * @param \Drupal\Core\Entity\EntityInterface $entity * The entity to retrieve field data from. * @param \Drupal\Core\Session\AccountInterface $account * The user account. * * @return bool * TRUE if a user can administer a specific domain, or FALSE. */ public function isDomainAdmin(EntityInterface $entity, AccountInterface $account) { $user = $this->userStorage->load($account->id()); $user_domains = $this->domainElementManager->getFieldValues($user, DOMAIN_ADMIN_FIELD); return isset($user_domains[$entity->id()]); } }