RestPermissions.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace Drupal\rest;
  3. use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
  4. use Drupal\Core\Entity\EntityTypeManagerInterface;
  5. use Drupal\rest\Plugin\Type\ResourcePluginManager;
  6. use Symfony\Component\DependencyInjection\ContainerInterface;
  7. /**
  8. * Provides rest module permissions.
  9. */
  10. class RestPermissions implements ContainerInjectionInterface {
  11. /**
  12. * The rest resource plugin manager.
  13. *
  14. * @var \Drupal\rest\Plugin\Type\ResourcePluginManager
  15. */
  16. protected $restPluginManager;
  17. /**
  18. * The REST resource config storage.
  19. *
  20. * @var \Drupal\Core\Entity\EntityManagerInterface
  21. */
  22. protected $resourceConfigStorage;
  23. /**
  24. * Constructs a new RestPermissions instance.
  25. *
  26. * @param \Drupal\rest\Plugin\Type\ResourcePluginManager $rest_plugin_manager
  27. * The rest resource plugin manager.
  28. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
  29. * The entity type manager.
  30. */
  31. public function __construct(ResourcePluginManager $rest_plugin_manager, EntityTypeManagerInterface $entity_type_manager) {
  32. $this->restPluginManager = $rest_plugin_manager;
  33. $this->resourceConfigStorage = $entity_type_manager->getStorage('rest_resource_config');
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public static function create(ContainerInterface $container) {
  39. return new static($container->get('plugin.manager.rest'), $container->get('entity_type.manager'));
  40. }
  41. /**
  42. * Returns an array of REST permissions.
  43. *
  44. * @return array
  45. */
  46. public function permissions() {
  47. $permissions = [];
  48. /** @var \Drupal\rest\RestResourceConfigInterface[] $resource_configs */
  49. $resource_configs = $this->resourceConfigStorage->loadMultiple();
  50. foreach ($resource_configs as $resource_config) {
  51. $plugin = $resource_config->getResourcePlugin();
  52. $permissions = array_merge($permissions, $plugin->permissions());
  53. }
  54. return $permissions;
  55. }
  56. }