class OgRoleManager in Organic groups 8
Defines a manager of an OG role.
Hierarchy
- class \Drupal\og\OgRoleManager implements OgRoleManagerInterface
Expanded class hierarchy of OgRoleManager
1 file declares its use of OgRoleManager
- OgRoleManagerTest.php in tests/
src/ Unit/ OgRoleManagerTest.php
1 string reference to 'OgRoleManager'
1 service uses OgRoleManager
File
- src/
OgRoleManager.php, line 15
Namespace
Drupal\ogView source
class OgRoleManager implements OgRoleManagerInterface {
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The entity storage for OgRole entities.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $ogRoleStorage;
/**
* The event dispatcher.
*
* @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
*/
protected $eventDispatcher;
/**
* The OG permission manager.
*
* @var \Drupal\og\PermissionManagerInterface
*/
protected $permissionManager;
/**
* Constructs an OgRoleManager object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
* The event dispatcher.
* @param \Drupal\og\PermissionManagerInterface $permission_manager
* The OG permission manager.
*/
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;
}
/**
* {@inheritdoc}
*/
public function createPerBundleRoles($entity_type_id, $bundle_id) {
$roles = [];
foreach ($this
->getDefaultRoles() as $role) {
$role
->setGroupType($entity_type_id);
$role
->setGroupBundle($bundle_id);
// Populate the default roles with a set of default permissions.
$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;
}
/**
* {@inheritdoc}
*/
public function getDefaultRoles() {
// Provide the required default roles: 'member' and 'non-member'.
$roles = $this
->getRequiredDefaultRoles();
$event = new DefaultRoleEvent();
$this->eventDispatcher
->dispatch(DefaultRoleEventInterface::EVENT_NAME, $event);
// Use the array union operator '+=' to ensure the default roles cannot be
// altered by event subscribers.
$roles += $event
->getRoles();
return $roles;
}
/**
* {@inheritdoc}
*/
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;
}
/**
* {@inheritdoc}
*/
public function getRolesByBundle($entity_type_id, $bundle) {
$properties = [
'group_type' => $entity_type_id,
'group_bundle' => $bundle,
];
return $this
->ogRoleStorage()
->loadByProperties($properties);
}
/**
* {@inheritdoc}
*/
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) {
// If all permissions are requested, we need to add an AND condition for
// each permission because there is not an easy way to explicitly request
// a subset of an array.
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);
}
/**
* {@inheritdoc}
*/
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();
}
}
/**
* Retrieves the OG Role storage.
*
* @return \Drupal\Core\Entity\EntityStorageInterface
* The OG Role storage
*/
protected function ogRoleStorage() {
if (!$this->ogRoleStorage) {
$this->ogRoleStorage = $this->entityTypeManager
->getStorage('og_role');
}
return $this->ogRoleStorage;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
OgRoleManager:: |
protected | property | The entity type manager. | |
OgRoleManager:: |
protected | property | The event dispatcher. | |
OgRoleManager:: |
protected | property | The entity storage for OgRole entities. | |
OgRoleManager:: |
protected | property | The OG permission manager. | |
OgRoleManager:: |
public | function |
Creates the roles for the given group type, based on the default roles. Overrides OgRoleManagerInterface:: |
|
OgRoleManager:: |
public | function |
Returns the default roles. Overrides OgRoleManagerInterface:: |
|
OgRoleManager:: |
public | function |
Returns the roles which every group type requires. Overrides OgRoleManagerInterface:: |
|
OgRoleManager:: |
public | function |
Returns all the roles of a provided group. Overrides OgRoleManagerInterface:: |
|
OgRoleManager:: |
public | function |
Returns all the roles that have a specific permission. Overrides OgRoleManagerInterface:: |
|
OgRoleManager:: |
protected | function | Retrieves the OG Role storage. | |
OgRoleManager:: |
public | function |
Deletes the roles associated with a group type. Overrides OgRoleManagerInterface:: |
|
OgRoleManager:: |
public | function | Constructs an OgRoleManager object. |