View source
<?php
namespace Drupal\book_access;
use Drupal\book_access\Entity\BookAccessDefaultsInterface;
use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\user\RoleInterface;
use Psr\Log\LoggerInterface;
class BookAccessHelper {
use StringTranslationTrait;
protected $entityTypeManager;
protected $logger;
public function __construct(EntityTypeManagerInterface $entity_type_manager, LoggerInterface $logger) {
$this->entityTypeManager = $entity_type_manager;
$this->logger = $logger;
}
public function grantIds() {
return [
'grant_view',
'grant_update',
'grant_delete',
'grant_admin_access',
'grant_add_child',
'grant_edit_outline',
];
}
public function defaultGrants(string $grant_type) {
switch ($grant_type) {
case '__author__':
return [
'grant_view' => TRUE,
'grant_update' => TRUE,
'grant_delete' => TRUE,
'grant_admin_access' => FALSE,
'grant_add_child' => TRUE,
'grant_edit_outline' => TRUE,
];
case 'administrator':
return [
'grant_view' => TRUE,
'grant_update' => TRUE,
'grant_delete' => TRUE,
'grant_admin_access' => TRUE,
'grant_add_child' => TRUE,
'grant_edit_outline' => TRUE,
];
default:
return [
'grant_view' => TRUE,
'grant_update' => FALSE,
'grant_delete' => FALSE,
'grant_admin_access' => FALSE,
'grant_add_child' => FALSE,
'grant_edit_outline' => FALSE,
];
}
}
public function setDefaultForRole(RoleInterface $role) {
$role_id = $role
->id();
if ($role_id == 'administrator') {
$defaultToUse = $this
->defaultGrants('administrator');
}
elseif ($role_id == '__author__') {
$this->logger
->error($this
->t("Could not set default book_access grants for role: @rid", [
'@rid' => $role_id,
]));
return;
}
else {
$defaultToUse = $this
->defaultGrants($role_id);
}
$values = [
'id' => $role_id,
'label' => $this
->t('%role_label role default grants', [
'%role_label' => $role
->label(),
]),
'grant_type' => 'role',
'role_id' => $role_id,
'grants' => $defaultToUse,
];
try {
$this->entityTypeManager
->getStorage('book_access_defaults')
->create($values)
->save();
} catch (InvalidPluginDefinitionException $e) {
$this->logger
->error($this
->t("Could not set default book_access grants for role: @rid", [
'@rid' => $role_id,
]));
} catch (PluginNotFoundException $e) {
$this->logger
->error($this
->t("Could not set default book_access grants for role: @rid", [
'@rid' => $role_id,
]));
} catch (EntityStorageException $e) {
$this->logger
->error($this
->t("Could not set default book_access grants for role: @rid", [
'@rid' => $role_id,
]));
}
}
public function getAuthorBookAccessDefaults() : ?BookAccessDefaultsInterface {
return $this->entityTypeManager
->getStorage('book_access_defaults')
->load('book_access_author_defaults');
}
public function getRoleBookAccessDefaults() : array {
$results = [];
$query = $this->entityTypeManager
->getStorage('user_role')
->getQuery();
$role_results = $query
->sort('weight', 'ASC')
->execute();
$roles = $this->entityTypeManager
->getStorage('user_role')
->loadMultiple(array_keys($role_results));
$query = $this->entityTypeManager
->getStorage('book_access_defaults')
->getQuery();
$defaults_ids = $query
->execute();
$defaults = $this->entityTypeManager
->getStorage('book_access_defaults')
->loadMultiple(array_keys($defaults_ids));
foreach ($roles as $role) {
$results[$role
->id()] = [
'label' => $role
->label(),
'default_grants' => $defaults[$role
->id()] ?? $this
->setDefaultForRole($role),
];
}
return $results;
}
public function updateDefaultsSettings(array $book_access_defaults) : bool {
$result = TRUE;
foreach ($book_access_defaults as $id => $book_access_default) {
$grant_values = [];
foreach ($book_access_default as $key => $item) {
$grant_values[$key] = boolval($item);
}
try {
$accessDefault = $this->entityTypeManager
->getStorage('book_access_defaults')
->load($id);
$accessDefault
->set('grants', $grant_values)
->save();
} catch (EntityStorageException|InvalidPluginDefinitionException|PluginNotFoundException $e) {
$result = FALSE;
break;
}
}
return $result;
}
}