View source
<?php
declare (strict_types=1);
namespace Drupal\og;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\og\Event\DefaultRoleEvent;
use Drupal\og\Event\DefaultRoleEventInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class OgRoleManager implements OgRoleManagerInterface {
protected $entityTypeManager;
protected $ogRoleStorage;
protected $eventDispatcher;
protected $permissionManager;
public function __construct(EntityTypeManagerInterface $entity_type_manager, EventDispatcherInterface $event_dispatcher, PermissionManagerInterface $permission_manager) {
$this->entityTypeManager = $entity_type_manager;
$this->eventDispatcher = $event_dispatcher;
$this->permissionManager = $permission_manager;
}
public function createPerBundleRoles($entity_type_id, $bundle_id) {
$roles = [];
foreach ($this
->getDefaultRoles() as $role) {
$role
->setGroupType($entity_type_id);
$role
->setGroupBundle($bundle_id);
$permissions = $this->permissionManager
->getDefaultGroupPermissions($entity_type_id, $bundle_id, $role
->getName());
foreach (array_keys($permissions) as $permission) {
$role
->grantPermission($permission);
}
$role
->save();
$roles[] = $role;
}
return $roles;
}
public function getDefaultRoles() {
$roles = $this
->getRequiredDefaultRoles();
$event = new DefaultRoleEvent();
$this->eventDispatcher
->dispatch(DefaultRoleEventInterface::EVENT_NAME, $event);
$roles += $event
->getRoles();
return $roles;
}
public function getRequiredDefaultRoles() {
$roles = [];
$role_properties = [
[
'role_type' => OgRoleInterface::ROLE_TYPE_REQUIRED,
'label' => 'Non-member',
'name' => OgRoleInterface::ANONYMOUS,
],
[
'role_type' => OgRoleInterface::ROLE_TYPE_REQUIRED,
'label' => 'Member',
'name' => OgRoleInterface::AUTHENTICATED,
],
];
foreach ($role_properties as $properties) {
$roles[$properties['name']] = $this
->ogRoleStorage()
->create($properties);
}
return $roles;
}
public function getRolesByBundle($entity_type_id, $bundle) {
$properties = [
'group_type' => $entity_type_id,
'group_bundle' => $bundle,
];
return $this
->ogRoleStorage()
->loadByProperties($properties);
}
public function getRolesByPermissions(array $permissions, $entity_type_id = NULL, $bundle = NULL, $require_all = TRUE) : array {
$role_storage = $this
->ogRoleStorage();
$query = $role_storage
->getQuery();
if ($require_all) {
foreach ($permissions as $permission) {
$query
->condition('permissions.*', $permission);
}
}
else {
$query
->condition('permissions.*', $permissions, 'IN');
}
if (!empty($entity_type_id)) {
$query
->condition('group_type', $entity_type_id);
}
if (!empty($bundle)) {
$query
->condition('group_bundle', $bundle);
}
$role_ids = $query
->execute();
return $role_storage
->loadMultiple($role_ids);
}
public function removeRoles($entity_type_id, $bundle_id) {
$properties = [
'group_type' => $entity_type_id,
'group_bundle' => $bundle_id,
];
foreach ($this
->ogRoleStorage()
->loadByProperties($properties) as $role) {
$role
->delete();
}
}
protected function ogRoleStorage() {
if (!$this->ogRoleStorage) {
$this->ogRoleStorage = $this->entityTypeManager
->getStorage('og_role');
}
return $this->ogRoleStorage;
}
}