abstract class DrushRole

Hierarchy

Expanded class hierarchy of DrushRole

Members

Contains filters are case sensitive
Namesort descending Modifiers Type Description
DrushRole::$name public property Drupal 6 and Drupal 7: 'rid' is numeric 'name' is machine name (e.g. 'anonymous user')
DrushRole::$rid public property
DrushRole::$roles public property This is initialized to the result of the user_roles() function, which returns an associative array of rid => name pairs.
DrushRole::add public function
DrushRole::delete public function
DrushRole::getAllModulePerms public function
DrushRole::getModulePerms public function
DrushRole::getPerms public function
DrushRole::grant_permissions public function
DrushRole::remove public function
DrushRole::revoke_permissions public function
DrushRole::role_create public function
DrushRole::__construct public function This constructor will allow the role to be selected either via the role id or via the role name.

File

classes/DrushRole.php, line 3

View source
abstract class DrushRole {
  /**
   * Drupal 6 and Drupal 7:
   *   'rid' is numeric
   *   'name' is machine name (e.g. 'anonymous user')
   *
   * Drupal 8:
   *   'rid' is machine name (e.g. 'anonymous')
   *   'name' is human-readable name (e.g. 'Anonymous user').
   *
   * c.f. http://drupal.org/node/1619504
   */
  public $name;
  public $rid;

  /**
   * This is initialized to the result of the user_roles()
   * function, which returns an associative array of
   * rid => name pairs.
   */
  public $roles;

  /**
   * This constructor will allow the role to be selected either
   * via the role id or via the role name.
   */
  public function __construct($rid = DRUPAL_ANONYMOUS_RID) {
    $this->roles = user_roles();
    if (!is_numeric($rid)) {
      $role_name = $rid;
      if (in_array($role_name, $this->roles)) {
        $rid = array_search($role_name, $this->roles);
      }
    }

    if (isset($this->roles[$rid])) {
      $this->rid = $rid;
      $this->name = $this->roles[$rid];
    }
    else {
      throw new DrushRoleException(dt('Could not find the role: !role', array('!role' => $rid)));
    }
  }

  public function getPerms() {
    return array();
  }

  public function getModulePerms($module) {
    return array();
  }

  public function getAllModulePerms() {
    $permissions = array();
    $module_list = module_list();
    ksort($module_list);
    foreach ($module_list as $module) {
      if ($perms = $this->getModulePerms($module)) {
        $permissions = array_merge($permissions, $perms);
      }
    }
    return $permissions;
  }

  public function role_create($role_machine_name, $role_human_readable_name = '') {
  }

  public function delete() {
  }

  public function add($perm) {
    $perms = $this->getPerms();
    if (!in_array($perm, $perms)) {
      $this->grant_permissions(array($perm));
      return TRUE;
    }
    else {
      drush_log(dt('"!role" already has the permission "!perm"', array(
        '!perm' => $perm,
        '!role' => $this->name,
      )), 'ok');
      return FALSE;
    }
  }

  public function remove($perm) {
    $perms = $this->getPerms();
    if (in_array($perm, $perms)) {
      $this->revoke_permissions(array($perm));
      return TRUE;
    }
    else {
      drush_log(dt('"!role" does not have the permission "!perm"', array(
        '!perm' => $perm,
        '!role' => $this->name,
      )), 'ok');
      return FALSE;
    }
  }

  public function grant_permissions($perms) {
  }

  public function revoke_permissions($perms) {
  }
}