RoleBase.php

  1. 8.0.x lib/Drush/Role/RoleBase.php
  2. 7.x lib/Drush/Role/RoleBase.php
  3. master lib/Drush/Role/RoleBase.php

Namespace

Drush\Role

Classes

Namesort descending Description
RoleBase

File

lib/Drush/Role/RoleBase.php
View source
  1. <?php
  2. namespace Drush\Role;
  3. abstract class RoleBase {
  4. /**
  5. * Drupal 7:
  6. * 'rid' is numeric
  7. * 'name' is machine name (e.g. 'anonymous user')
  8. *
  9. * Drupal 8:
  10. * 'rid' is machine name (e.g. 'anonymous')
  11. * 'name' is human-readable name (e.g. 'Anonymous user').
  12. *
  13. * c.f. http://drupal.org/node/1619504
  14. */
  15. public $name;
  16. public $rid;
  17. /**
  18. * This is initialized to the result of the user_roles()
  19. * function, which returns an associative array of
  20. * rid => name pairs.
  21. */
  22. public $roles;
  23. /**
  24. * This constructor will allow the role to be selected either
  25. * via the role id or via the role name.
  26. */
  27. public function __construct($rid = DRUPAL_ANONYMOUS_RID) {
  28. $this->roles = user_roles();
  29. if (!is_numeric($rid)) {
  30. $role_name = $rid;
  31. if (in_array($role_name, $this->roles)) {
  32. $rid = array_search($role_name, $this->roles);
  33. }
  34. }
  35. if (isset($this->roles[$rid])) {
  36. $this->rid = $rid;
  37. // In D8+ Role is an object.
  38. $this->name = is_object($this->roles[$rid]) ? $this->roles[$rid]->label() : $this->roles[$rid];
  39. }
  40. else {
  41. throw new RoleException(dt('Could not find the role: !role', array('!role' => $rid)));
  42. }
  43. }
  44. /*
  45. * Get all perms for a given Role.
  46. */
  47. public function getPerms() {
  48. return array();
  49. }
  50. /*
  51. * Get all perms for a given module.
  52. */
  53. public function getModulePerms($module) {
  54. return array();
  55. }
  56. /*
  57. * Get all permissions site-wide.
  58. */
  59. public function getAllModulePerms() {
  60. $permissions = array();
  61. drush_include_engine('drupal', 'environment');
  62. $module_list = drush_module_list();
  63. ksort($module_list);
  64. foreach ($module_list as $module) {
  65. if ($perms = $this->getModulePerms($module)) {
  66. $permissions = array_merge($permissions, $perms);
  67. }
  68. }
  69. return $permissions;
  70. }
  71. public function role_create($role_machine_name, $role_human_readable_name = '') {
  72. }
  73. public function delete() {
  74. }
  75. public function add($perm) {
  76. $perms = $this->getPerms();
  77. if (!in_array($perm, $perms)) {
  78. $this->grant_permissions(array($perm));
  79. return TRUE;
  80. }
  81. else {
  82. drush_log(dt('"!role" already has the permission "!perm"', array(
  83. '!perm' => $perm,
  84. '!role' => $this->name,
  85. )), 'ok');
  86. return FALSE;
  87. }
  88. }
  89. public function remove($perm) {
  90. $perms = $this->getPerms();
  91. if (in_array($perm, $perms)) {
  92. $this->revoke_permissions(array($perm));
  93. return TRUE;
  94. }
  95. else {
  96. drush_log(dt('"!role" does not have the permission "!perm"', array(
  97. '!perm' => $perm,
  98. '!role' => $this->name,
  99. )), 'ok');
  100. return FALSE;
  101. }
  102. }
  103. public function grant_permissions($perms) {
  104. }
  105. public function revoke_permissions($perms) {
  106. }
  107. }