Commit 43883931 authored by Gorodkov Denis's avatar Gorodkov Denis

access for role 'Editor'

parent 9e34105d
......@@ -4,6 +4,7 @@
* Main file for hooks and custom functions.
*/
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Cache\CacheableMetadata;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Drupal\Core\Entity\EntityInterface;
......@@ -107,3 +108,93 @@ function template_preprocess_mymodule_example_first(&$variables) {
$date_formatter = \Drupal::service('date.formatter');
$variables['date'] = $date_formatter->format(time());
}
/**
* Implements hook_ENTITY_TYPE_access()
*/
function mymodule_node_access(\Drupal\Core\Entity\EntityInterface $node, $operation, \Drupal\Core\Session\AccountInterface $account) {
$user_roles = $account->getRoles();
$isRedactor = false;
foreach ($user_roles as $role) {
if ($role == 'redactor') {
$isRedactor = true;
}
}
if ($operation == 'delete') {
if ($isRedactor) {
return AccessResult::forbidden();
}
}
if ($operation == 'update') {
if ($node->getOwnerId() == $account->id()) {
/**
* Если это создатель
*/
return AccessResult::allowed();
}
if ($node->hasField('field_editor')) {
$allow_redactors = $node->get('field_editor')->referencedEntities();
} else {
/**
* Если поля field_editor нету
*/
return AccessResult::allowed();
}
if (empty($allow_redactors)) {
/**
* Если поля field_editor не заполнено
*/
return AccessResult::allowed();
} else {
if ($isRedactor) {
foreach ($allow_redactors as $allow_redactor) {
if ($allow_redactor->id() == $account->id()) {
/**
* Если роли Redactor разрешено редактирование
*/
return AccessResult::allowed();
}
}
/**
* Если роли Redactor НЕ разрешено редактирование
*/
return AccessResult::forbidden();
} else {
/**
* Если у пользователя нет роли Redactor
*/
return AccessResult::allowed();
}
}
} else {
/**
* Если операция не update
*/
}
}
function mymodule_entity_form_display_alter(\Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display, array $context) {
if ($context['entity_type'] == 'node') {
$user = Drupal::currentUser();
$user_roles = $user->getRoles();
$isRedactor = false;
foreach ($user_roles as $role) {
if ($role == 'redactor') {
$isRedactor = true;
}
}
if ($isRedactor) {
$form_display->setComponent('field_editor', array(
'type' => 'hidden',
));
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment