Advertisement
Guest User

Untitled

a guest
Nov 29th, 2015
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * @file
  5. * Contains \Drupal\entity\EntityPermissions.
  6. */
  7.  
  8. namespace Drupal\entity;
  9.  
  10. use Drupal\Core\Entity\EntityManagerInterface;
  11.  
  12. /**
  13. * Provides dynamic permissions for participating entity types.
  14. */
  15. class EntityPermissions {
  16.  
  17. /**
  18. * The entity manager.
  19. *
  20. * @var \Drupal\Core\Entity\EntityManagerInterface
  21. */
  22. protected $entityManager;
  23.  
  24. /**
  25. * Creates a new EntityPermissions object.
  26. *
  27. * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
  28. * The entity manager.
  29. */
  30. public function __construct(EntityManagerInterface $entity_manager) {
  31. $this->entityManager = $entity_manager;
  32. }
  33.  
  34. /**
  35. * Builds a list of permissions for the participating entity types.
  36. *
  37. * @return array
  38. * The permissions.
  39. * @see \Drupal\user\PermissionHandlerInterface::getPermissions()
  40. */
  41. public function buildPermissions() {
  42. $permissions = [];
  43. foreach ($this->getParticipatingEntityTypes() as $entity_type) {
  44. $access_handler_class = $entity_type->getHandlerClass('access');
  45. $access_handler = new $access_handler_class($entity_type);
  46. $permissions += $access_handler->buildPermissions();
  47. }
  48.  
  49. return $permissions;
  50. }
  51.  
  52. /**
  53. * Gets a list of participating entity types.
  54. *
  55. * This list includes all entity types that declare access handlers
  56. * implementing this module's ExtendedEntityAccessControlHandlerInterface.
  57. *
  58. * @return \Drupal\Core\Entity\EntityTypeInterface[]
  59. * The participating entity types.
  60. */
  61. protected function getParticipatingEntityTypes() {
  62. $entity_types = $this->entityManager->getDefinitions();
  63. $entity_types = array_filter($entity_types, function ($entity_type) {
  64. $access_handler_class = $entity_type->getHandlerClass('access');
  65. $interface = '\Drupal\entity\ExtendedEntityAccessControlHandlerInterface';
  66. return is_subclass_of($access_handler_class, $interface);
  67. });
  68.  
  69. return $entity_types;
  70. }
  71.  
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement