You are here

abstract class Permission in Organic groups 8

Base class for OG permissions.

Hierarchy

Expanded class hierarchy of Permission

1 string reference to 'Permission'
og.schema.yml in config/schema/og.schema.yml
config/schema/og.schema.yml

File

src/Permission.php, line 10

Namespace

Drupal\og
View source
abstract class Permission implements PermissionInterface {

  /**
   * The name of the permission.
   *
   * @var string
   */
  protected $name;

  /**
   * The human readable permission title.
   *
   * @var string
   */
  protected $title;

  /**
   * A short description of the permission.
   *
   * @var string
   */
  protected $description = '';

  /**
   * The default roles to which this permission applies.
   *
   * @var array
   */
  protected $defaultRoles = [];

  /**
   * If the permission is security sensitive and should be limited to admins.
   *
   * @var bool
   */
  protected $restrictAccess = FALSE;

  /**
   * Constructs a Permission object.
   *
   * @param array $values
   *   An associative array of values, keyed by property.
   */
  public function __construct(array $values = []) {
    foreach ($values as $property => $value) {
      $this
        ->set($property, $value);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function get($property) {
    $property = $this
      ->lowerCamelize($property);
    if (!property_exists($this, $property)) {
      throw new \InvalidArgumentException("Invalid property {$property}.");
    }
    return $this->{$property};
  }

  /**
   * {@inheritdoc}
   */
  public function set($property, $value) {
    $property = $this
      ->lowerCamelize($property);
    $this
      ->validate($property, $value);
    $this->{$property} = $value;
  }

  /**
   * {@inheritdoc}
   */
  public function getName() {
    return $this
      ->get('name');
  }

  /**
   * {@inheritdoc}
   */
  public function setName($name) {
    $this
      ->set('name', $name);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getTitle() {
    return $this
      ->get('title');
  }

  /**
   * {@inheritdoc}
   */
  public function setTitle($title) {
    $this
      ->set('title', $title);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getDescription() {
    return $this
      ->get('description');
  }

  /**
   * {@inheritdoc}
   */
  public function setDescription($description) {
    $this
      ->set('description', $description);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getDefaultRoles() {
    return $this
      ->get('default roles');
  }

  /**
   * {@inheritdoc}
   */
  public function setDefaultRoles(array $default_roles) {
    $this
      ->set('default roles', $default_roles);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getRestrictAccess() {
    return $this
      ->get('restrict access');
  }

  /**
   * {@inheritdoc}
   */
  public function setRestrictAccess($access) {
    $this
      ->set('restrict access', $access);
    return $this;
  }

  /**
   * Validates the given property and value.
   *
   * @param string $property
   *   The property to validate.
   * @param mixed $value
   *   The value to validate.
   */
  protected function validate($property, $value) {
    if (!property_exists($this, $property)) {
      throw new \InvalidArgumentException("Invalid property {$property}.");
    }
    if ($property === 'restrictAccess' && !is_bool($value)) {
      throw new \InvalidArgumentException('The value for the "restrict access" property should be a boolean.');
    }
  }

  /**
   * Converts the given string in a lowerCamelCase version.
   *
   * @param string $string
   *   The string to convert.
   *
   * @return string
   *   The converted string.
   */
  protected static function lowerCamelize($string) {
    return lcfirst(str_replace(' ', '', ucwords($string)));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Permission::$defaultRoles protected property The default roles to which this permission applies.
Permission::$description protected property A short description of the permission.
Permission::$name protected property The name of the permission.
Permission::$restrictAccess protected property If the permission is security sensitive and should be limited to admins.
Permission::$title protected property The human readable permission title.
Permission::get public function Returns the value for the given property. Overrides PermissionInterface::get
Permission::getDefaultRoles public function Returns the default roles. Overrides PermissionInterface::getDefaultRoles
Permission::getDescription public function Returns the description. Overrides PermissionInterface::getDescription
Permission::getName public function Returns the machine name of the permission. Overrides PermissionInterface::getName
Permission::getRestrictAccess public function Returns whether or not access is restricted. Overrides PermissionInterface::getRestrictAccess
Permission::getTitle public function Returns the human readable permission title. Overrides PermissionInterface::getTitle
Permission::lowerCamelize protected static function Converts the given string in a lowerCamelCase version.
Permission::set public function Sets the value for the given property. Overrides PermissionInterface::set
Permission::setDefaultRoles public function Sets the default roles. Overrides PermissionInterface::setDefaultRoles
Permission::setDescription public function Sets the description. Overrides PermissionInterface::setDescription
Permission::setName public function Sets the machine name of the permission. Overrides PermissionInterface::setName
Permission::setRestrictAccess public function Sets the access restriction. Overrides PermissionInterface::setRestrictAccess
Permission::setTitle public function Sets the human readable permission title. Overrides PermissionInterface::setTitle
Permission::validate protected function Validates the given property and value. 1
Permission::__construct public function Constructs a Permission object.