View source
<?php
namespace Drupal\workbench_access;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\workbench_access\Entity\AccessSchemeInterface;
class UserSectionStorage implements UserSectionStorageInterface {
protected $userSectionCache;
protected $currentUser;
protected $roleSectionStorage;
protected $entityTypeManager;
public function __construct(EntityTypeManagerInterface $entityTypeManager, AccountInterface $currentUser, RoleSectionStorageInterface $role_section_storage) {
$this->currentUser = $currentUser;
$this->entityTypeManager = $entityTypeManager;
$this->roleSectionStorage = $role_section_storage;
}
protected function sectionStorage() {
return $this->entityTypeManager
->getStorage('section_association');
}
public function getUserSections(AccessSchemeInterface $scheme, AccountInterface $account = NULL, $add_roles = TRUE) {
if (!$account) {
$account = $this->currentUser;
}
if (!isset($this->userSectionCache[$scheme
->id()][$account
->id()])) {
$user_sections = $this
->loadUserSections($scheme, $account);
if ($add_roles) {
$user_sections = array_merge($user_sections, $this->roleSectionStorage
->getRoleSections($scheme, $account));
}
$this->userSectionCache[$scheme
->id()][$account
->id()] = $user_sections;
}
return $this->userSectionCache[$scheme
->id()][$account
->id()];
}
protected function loadUserSections(AccessSchemeInterface $scheme, AccountInterface $account) {
$query = $this
->sectionStorage()
->getAggregateQuery()
->condition('access_scheme', $scheme
->id())
->condition('user_id', $account
->id())
->groupBy('section_id')
->execute();
$list = array_column($query, 'section_id');
return $list;
}
public function addUser(AccessSchemeInterface $scheme, AccountInterface $account, array $sections = []) {
foreach ($sections as $id) {
if ($section_association = $this
->sectionStorage()
->loadSection($scheme
->id(), $id)) {
if ($new_values = $section_association
->getCurrentUserIds()) {
$new_values[] = $account
->id();
$section_association
->set('user_id', array_unique($new_values));
}
else {
$section_association
->set('user_id', [
$account
->id(),
]);
}
$section_association
->setNewRevision();
}
else {
$values = [
'access_scheme' => $scheme
->id(),
'section_id' => $id,
'user_id' => [
$account
->id(),
],
];
$new_values[] = $account
->id();
$section_association = $this
->sectionStorage()
->create($values);
}
$section_association
->save();
$this
->resetCache($scheme, $account
->id());
}
return $this
->userStorage()
->load($account
->id());
}
public function removeUser(AccessSchemeInterface $scheme, AccountInterface $account, array $sections = []) {
foreach ($sections as $id) {
$new_values = [];
if ($section_association = $this
->sectionStorage()
->loadSection($scheme
->id(), $id)) {
if ($values = $section_association
->getCurrentUserIds()) {
foreach ($values as $delta => $value) {
if ($value != $account
->id()) {
$new_values[] = $value;
}
}
$section_association
->set('user_id', array_unique($new_values));
}
$section_association
->save();
}
}
$this
->resetCache($scheme, $account
->id());
return $this
->userStorage()
->load($account
->id());
}
public function getEditors(AccessSchemeInterface $scheme, $id) {
$query = $this
->sectionStorage()
->getAggregateQuery()
->condition('access_scheme', $scheme
->id())
->condition('section_id', $id)
->groupBy('user_id.target_id')
->groupBy('user_id.entity.name');
$data = $query
->execute();
$list = array_column($data, 'name', 'user_id_target_id');
if (current($list)) {
return $list;
}
return [];
}
public function getPotentialEditors($id) {
$roles = user_role_names(FALSE, 'use workbench access');
$rids = array_keys($roles);
if (empty($rids)) {
return [];
}
$query = $this
->userStorage()
->getQuery();
$query
->condition('status', 1)
->sort('name');
if (!in_array(AccountInterface::AUTHENTICATED_ROLE, $rids, TRUE)) {
$query
->condition('roles', $rids, 'IN');
}
$users = $query
->execute();
return $users;
}
public function getPotentialEditorsRoles($id) {
return $this->roleSectionStorage
->getPotentialRolesFiltered($id);
}
public function resetCache(AccessSchemeInterface $scheme, $user_id = NULL) {
if ($user_id && isset($this->userSectionCache[$scheme
->id()][$user_id])) {
unset($this->userSectionCache[$scheme
->id()][$user_id]);
}
elseif (isset($this->userSectionCache[$scheme
->id()])) {
unset($this->userSectionCache[$scheme
->id()]);
}
Cache::invalidateTags([
'config:workbench_access.access_scheme.' . $scheme
->id(),
]);
}
protected function userStorage() {
return $this->entityTypeManager
->getStorage('user');
}
}