View source
<?php
namespace Drupal\workbench_access;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\State\StateInterface;
use Drupal\workbench_access\Entity\AccessSchemeInterface;
class RoleSectionStorage implements RoleSectionStorageInterface {
use DependencySerializationTrait;
protected $state;
protected $roleStorage;
public function __construct(StateInterface $state, EntityTypeManagerInterface $entityTypeManager) {
$this->state = $state;
$this->entityTypeManager = $entityTypeManager;
$this->roleStorage = $entityTypeManager
->getStorage('user_role');
}
protected function sectionStorage() {
return $this->entityTypeManager
->getStorage('section_association');
}
public function addRole(AccessSchemeInterface $scheme, $role_id, array $sections = []) {
foreach ($sections as $id) {
if ($section_association = $this
->sectionStorage()
->loadSection($scheme
->id(), $id)) {
if ($new_values = $section_association
->getCurrentRoleIds()) {
$new_values[] = $role_id;
$section_association
->set('role_id', array_unique($new_values));
}
else {
$section_association
->set('role_id', [
$role_id,
]);
}
$section_association
->setNewRevision();
}
else {
$values = [
'access_scheme' => $scheme
->id(),
'section_id' => $id,
'role_id' => [
$role_id,
],
];
$section_association = $this
->sectionStorage()
->create($values);
}
$section_association
->save();
\Drupal::service('workbench_access.user_section_storage')
->resetCache($scheme);
}
}
public function removeRole(AccessSchemeInterface $scheme, $role_id, array $sections = []) {
foreach ($sections as $id) {
$new_values = [];
if ($section_association = $this
->sectionStorage()
->loadSection($scheme
->id(), $id)) {
if ($values = $section_association
->getCurrentRoleIds()) {
foreach ($values as $delta => $value) {
if ($value != $role_id) {
$new_values[] = $value;
}
}
$section_association
->set('role_id', array_unique($new_values));
}
$section_association
->save();
}
\Drupal::service('workbench_access.user_section_storage')
->resetCache($scheme);
}
}
public function getRoleSections(AccessSchemeInterface $scheme, AccountInterface $account = NULL) {
$sections = [];
if ($account) {
$results = $this
->sectionStorage()
->loadByProperties([
'role_id' => $account
->getRoles(),
'access_scheme' => $scheme
->id(),
]);
foreach ($results as $result) {
$sections[] = $result
->get('section_id')->value;
}
}
return $sections;
}
public function getPotentialRoles($id) {
$list = [];
$roles = $this->roleStorage
->loadMultiple();
foreach ($roles as $rid => $role) {
$list[$rid] = $role
->label();
}
return $list;
}
public function getPotentialRolesFiltered($id) {
$list = [];
$roles = $this->roleStorage
->loadMultiple();
foreach ($roles as $rid => $role) {
if ($role
->hasPermission('use workbench access')) {
$list[$rid] = $role
->label();
}
}
return $list;
}
public function getRoles(AccessSchemeInterface $scheme, $id) {
$roles = [];
$query = $this
->sectionStorage()
->getAggregateQuery()
->condition('access_scheme', $scheme
->id())
->condition('section_id', $id)
->groupBy('role_id.target_id')
->execute();
$rids = array_column($query, 'role_id_target_id');
if (!empty(current($rids))) {
$roles = $this->roleStorage
->loadMultiple($rids);
}
return array_keys($roles);
}
public function saveRoleSections(AccessSchemeInterface $scheme, $role_id, array $settings = []) {
}
public function deleteRoleSections(AccessSchemeInterface $scheme, $role_id) {
$section_ids = [];
if ($sections = $this
->sectionStorage()
->loadByProperties([
'role_id' => $role_id,
])) {
foreach ($sections as $section) {
$section_ids[] = $section
->get('section_id')->value;
}
$this
->removeRole($scheme, $role_id, $section_ids);
}
}
}