PermissionEvent.php in Organic groups 8
File
src/Event/PermissionEvent.php
View source
<?php
declare (strict_types=1);
namespace Drupal\og\Event;
use Drupal\og\GroupContentOperationPermission;
use Drupal\og\PermissionInterface;
use Symfony\Component\EventDispatcher\Event;
class PermissionEvent extends Event implements PermissionEventInterface {
protected $permissions = [];
protected $groupEntityTypeId;
protected $groupBundleId;
protected $groupContentBundleIds;
public function __construct($group_entity_type_id, $group_bundle_id, array $group_content_bundle_ids) {
$this->groupEntityTypeId = $group_entity_type_id;
$this->groupBundleId = $group_bundle_id;
$this->groupContentBundleIds = $group_content_bundle_ids;
}
public function getPermission($name) {
if (!isset($this->permissions[$name])) {
throw new \InvalidArgumentException("The '{$name}' permission does not exist.");
}
return $this->permissions[$name];
}
public function getGroupContentOperationPermission($entity_type_id, $bundle_id, $operation, $owner = FALSE) {
foreach ($this
->getPermissions() as $permission) {
if ($permission instanceof GroupContentOperationPermission && $permission
->getEntityType() === $entity_type_id && $permission
->getBundle() === $bundle_id && $permission
->getOperation() === $operation && $permission
->getOwner() === $owner) {
return $permission;
}
}
throw new \InvalidArgumentException('The permission with the given properties does not exist.');
}
public function getPermissions() {
return $this->permissions;
}
public function setPermission(PermissionInterface $permission) {
if (empty($permission
->getName())) {
throw new \InvalidArgumentException('Permission name is required.');
}
if (empty($permission
->getTitle())) {
throw new \InvalidArgumentException('The permission title is required.');
}
if ($permission instanceof GroupContentOperationPermission) {
if (empty($permission
->getEntityType())) {
throw new \InvalidArgumentException('The entity type ID is required.');
}
if (empty($permission
->getBundle())) {
throw new \InvalidArgumentException('The bundle ID is required.');
}
if (empty($permission
->getOperation())) {
throw new \InvalidArgumentException('The operation is required.');
}
try {
$this
->deleteGroupContentOperationPermission($permission
->getEntityType(), $permission
->getBundle(), $permission
->getOperation(), $permission
->getOwner());
} catch (\InvalidArgumentException $e) {
}
}
$this->permissions[$permission
->getName()] = $permission;
}
public function setPermissions(array $permissions) {
foreach ($permissions as $permission) {
$this
->setPermission($permission);
}
}
public function deletePermission($name) {
if ($this
->hasPermission($name)) {
unset($this->permissions[$name]);
}
}
public function deleteGroupContentOperationPermission($entity_type_id, $bundle_id, $operation, $owner = 'any') {
$permission = $this
->getGroupContentOperationPermission($entity_type_id, $bundle_id, $operation, $owner);
$this
->deletePermission($permission
->getName());
}
public function hasPermission($name) {
return isset($this->permissions[$name]);
}
public function hasGroupContentOperationPermission($entity_type_id, $bundle_id, $operation, $owner = FALSE) {
try {
$this
->getGroupContentOperationPermission($entity_type_id, $bundle_id, $operation, $owner);
} catch (\InvalidArgumentException $e) {
return FALSE;
}
return TRUE;
}
public function getGroupEntityTypeId() {
return $this->groupEntityTypeId;
}
public function getGroupBundleId() {
return $this->groupBundleId;
}
public function getGroupContentBundleIds() {
return $this->groupContentBundleIds;
}
public function offsetGet($key) {
return $this
->getPermission($key);
}
public function offsetSet($key, $value) {
if (!$value instanceof PermissionInterface) {
throw new \InvalidArgumentException('The value must be an object of type PermissionInterface.');
}
if ($value
->getName() !== $key) {
throw new \InvalidArgumentException('The key and the permission name must be identical.');
}
$this
->setpermission($value);
}
public function offsetUnset($key) {
if ($this
->hasPermission($key)) {
$this
->deletePermission($key);
}
}
public function offsetExists($key) {
return $this
->hasPermission($key);
}
public function getIterator() {
return new \ArrayIterator($this->permissions);
}
}