ProfileAccessControlHandler.php 984 B

12345678910111213141516171819202122232425262728293031323334
  1. <?php
  2. namespace Drupal\profile;
  3. use Drupal\Core\Access\AccessResult;
  4. use Drupal\Core\Session\AccountInterface;
  5. use Drupal\entity\EntityAccessControlHandler as EntityApiAccessControlHandler;
  6. use Drupal\profile\Entity\ProfileType;
  7. /**
  8. * Defines the access control handler for the profile entity type.
  9. *
  10. * @see \Drupal\profile\Entity\Profile
  11. */
  12. class ProfileAccessControlHandler extends EntityApiAccessControlHandler {
  13. /**
  14. * {@inheritdoc}
  15. */
  16. protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
  17. $result = parent::checkCreateAccess($account, $context, $entity_bundle);
  18. // If access is allowed, check role restriction.
  19. if ($result->isAllowed()) {
  20. $bundle = ProfileType::load($entity_bundle);
  21. if (!empty(array_filter($bundle->getRoles()))) {
  22. $result = AccessResult::allowedIf(!empty(array_intersect($account->getRoles(), $bundle->getRoles())));
  23. }
  24. }
  25. return $result;
  26. }
  27. }