isAdmin()) { return []; } return $this->permissions; } /** * {@inheritdoc} */ public function getWeight() { return $this->get('weight'); } /** * {@inheritdoc} */ public function setWeight($weight) { $this->set('weight', $weight); return $this; } /** * {@inheritdoc} */ public function hasPermission($permission) { if ($this->isAdmin()) { return TRUE; } return in_array($permission, $this->permissions); } /** * {@inheritdoc} */ public function grantPermission($permission) { if ($this->isAdmin()) { return $this; } if (!$this->hasPermission($permission)) { $this->permissions[] = $permission; } return $this; } /** * {@inheritdoc} */ public function revokePermission($permission) { if ($this->isAdmin()) { return $this; } $this->permissions = array_diff($this->permissions, [$permission]); return $this; } /** * {@inheritdoc} */ public function isAdmin() { return (bool) $this->is_admin; } /** * {@inheritdoc} */ public function setIsAdmin($is_admin) { $this->is_admin = $is_admin; return $this; } /** * {@inheritdoc} */ public static function postLoad(EntityStorageInterface $storage, array &$entities) { parent::postLoad($storage, $entities); // Sort the queried roles by their weight. // See \Drupal\Core\Config\Entity\ConfigEntityBase::sort(). uasort($entities, 'static::sort'); } /** * {@inheritdoc} */ public function preSave(EntityStorageInterface $storage) { parent::preSave($storage); if (!isset($this->weight) && ($roles = $storage->loadMultiple())) { // Set a role weight to make this new role last. $max = array_reduce($roles, function ($max, $role) { return $max > $role->weight ? $max : $role->weight; }); $this->weight = $max + 1; } if (!$this->isSyncing()) { // Permissions are always ordered alphabetically to avoid conflicts in the // exported configuration. sort($this->permissions); } } }