Role8.php

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

Namespace

Drush\Role

Classes

Namesort descending Description
Role8

File

lib/Drush/Role/Role8.php
View source
  1. <?php
  2. namespace Drush\Role;
  3. use Drupal\user\Entity\Role;
  4. class Role8 extends Role7 {
  5. public function role_create($role_machine_name, $role_human_readable_name = '') {
  6. // In D6 and D7, when we create a new role, the role
  7. // machine name is specified, and the numeric rid is
  8. // auto-assigned (next available id); in D8, when we
  9. // create a new role, we need to specify both the rid,
  10. // which is now the role machine name, and also a human-readable
  11. // role name. If the client did not provide a human-readable
  12. // name, then we'll use the role machine name in its place.
  13. if (empty($role_human_readable_name)) {
  14. $role_human_readable_name = ucfirst($role_machine_name);
  15. }
  16. $role = new Role(array(
  17. 'id' => $role_machine_name,
  18. 'label' => $role_human_readable_name,
  19. ), 'user_role');
  20. $role->save();
  21. return $role;
  22. }
  23. public function getPerms() {
  24. $role = entity_load('user_role', $this->rid);
  25. $perms = $role->getPermissions();
  26. // $perms = user_role_permissions(array($this->rid => $this->name));
  27. return $perms;
  28. }
  29. public function getAllModulePerms() {
  30. $perms = \Drupal::service('user.permissions')->getPermissions();
  31. return array_keys($perms);
  32. }
  33. public function getModulePerms($module) {
  34. $module_perms = array();
  35. $perms = \Drupal::service('user.permissions')->getPermissions();
  36. foreach ($perms as $name => $perm) {
  37. if ($perm['provider'] == $module) {
  38. $module_perms[] = $name;
  39. }
  40. }
  41. return $module_perms;
  42. }
  43. public function delete() {
  44. $role = entity_load('user_role', $this->rid);
  45. $role->delete();
  46. }
  47. public function grant_permissions($perms) {
  48. return drush_op('user_role_grant_permissions', $this->rid, $perms);
  49. }
  50. public function revoke_permissions($perms) {
  51. return drush_op('user_role_revoke_permissions', $this->rid, $perms);
  52. }
  53. }