DomainAccessPermissions.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace Drupal\domain_access;
  3. use Drupal\node\Entity\NodeType;
  4. use Drupal\Core\StringTranslation\StringTranslationTrait;
  5. /**
  6. * Dynamic permissions class for Domain Access.
  7. */
  8. class DomainAccessPermissions {
  9. use StringTranslationTrait;
  10. /**
  11. * Define permissions.
  12. */
  13. public function permissions() {
  14. $permissions = [
  15. 'assign domain editors' => [
  16. 'title' => $this->t('Assign additional editors to assigned domains'),
  17. 'restrict access' => TRUE,
  18. ],
  19. 'assign editors to any domain' => [
  20. 'title' => $this->t('Assign additional editors to any domains'),
  21. 'restrict access' => TRUE,
  22. ],
  23. 'publish to any domain' => [
  24. 'title' => $this->t('Publish to any domain'),
  25. ],
  26. 'publish to any assigned domain' => [
  27. 'title' => $this->t('Publish content to any assigned domain'),
  28. ],
  29. 'create domain content' => [
  30. 'title' => $this->t('Create any content on assigned domains'),
  31. ],
  32. 'edit domain content' => [
  33. 'title' => $this->t('Edit any content on assigned domains'),
  34. ],
  35. 'delete domain content' => [
  36. 'title' => $this->t('Delete any content on assigned domains'),
  37. ],
  38. 'view unpublished domain content' => [
  39. 'title' => $this->t('View unpublished content on assigned domains'),
  40. 'restrict access' => TRUE,
  41. ],
  42. ];
  43. // Generate standard node permissions for all applicable node types.
  44. foreach (NodeType::loadMultiple() as $type) {
  45. $permissions += $this->nodePermissions($type);
  46. }
  47. return $permissions;
  48. }
  49. /**
  50. * Helper method to generate standard node permission list for a given type.
  51. *
  52. * Shamelessly lifted from node_list_permissions().
  53. *
  54. * @param \Drupal\node\Entity\NodeType $type
  55. * The node type object.
  56. *
  57. * @return array
  58. * An array of permission names and descriptions.
  59. */
  60. private function nodePermissions(NodeType $type) {
  61. // Build standard list of node permissions for this type.
  62. $id = $type->id();
  63. $perms = [
  64. "create $id content on assigned domains" => [
  65. 'title' => $this->t('%type_name: Create new content on assigned domains', ['%type_name' => $type->label()]),
  66. ],
  67. "update $id content on assigned domains" => [
  68. 'title' => $this->t('%type_name: Edit any content on assigned domains', ['%type_name' => $type->label()]),
  69. ],
  70. "delete $id content on assigned domains" => [
  71. 'title' => $this->t('%type_name: Delete any content on assigned domains', ['%type_name' => $type->label()]),
  72. ],
  73. ];
  74. return $perms;
  75. }
  76. }