LayoutBuilderLockAccessCheck.php in Layout Builder Lock 8
File
src/Access/LayoutBuilderLockAccessCheck.php
View source
<?php
namespace Drupal\layout_builder_lock\Access;
use Drupal\Core\Access\AccessResultAllowed;
use Drupal\Core\Access\AccessResultForbidden;
use Drupal\Core\Cache\RefinableCacheableDependencyInterface;
use Drupal\Core\Routing\Access\AccessInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\layout_builder\DefaultsSectionStorageInterface;
use Drupal\layout_builder\Plugin\SectionStorage\OverridesSectionStorage;
use Drupal\layout_builder\SectionStorageInterface;
use Drupal\layout_builder_lock\LayoutBuilderLock;
use Symfony\Component\Routing\Route;
class LayoutBuilderLockAccessCheck implements AccessInterface {
protected $routeMatch;
public function __construct(RouteMatchInterface $route_match) {
$this->routeMatch = $route_match;
}
public function access(SectionStorageInterface $section_storage, AccountInterface $account, Route $route) {
$operation = $route
->getRequirement('_layout_builder_lock_access');
if ($section_storage instanceof DefaultsSectionStorageInterface && $account
->hasPermission('manage lock settings on sections')) {
return new AccessResultAllowed();
}
if ($section_storage instanceof OverridesSectionStorage && $account
->hasPermission('bypass lock settings on layout overrides')) {
return new AccessResultAllowed();
}
$delta = $this->routeMatch
->getRawParameter('delta');
if (!isset($delta)) {
return new AccessResultAllowed();
}
$check_before_and_after = FALSE;
$lock_settings = $lock_settings_before = $lock_settings_after = [];
if ($operation == 'section_add' && $delta > 0) {
$check_before_and_after = TRUE;
try {
$lock_settings_before = array_filter($section_storage
->getSection($delta - 1)
->getThirdPartySetting('layout_builder_lock', 'lock', LayoutBuilderLock::NO_LOCK));
} catch (\OutOfBoundsException $ignored) {
}
try {
$lock_settings_after = array_filter($section_storage
->getSection($delta + 1)
->getThirdPartySetting('layout_builder_lock', 'lock', LayoutBuilderLock::NO_LOCK));
} catch (\OutOfBoundsException $ignored) {
}
}
elseif ($operation == 'section_edit' && $this->routeMatch
->getRawParameter('plugin_id')) {
return new AccessResultAllowed();
}
else {
try {
$lock_settings = array_filter($section_storage
->getSection($delta)
->getThirdPartySetting('layout_builder_lock', 'lock', LayoutBuilderLock::NO_LOCK));
} catch (\OutOfBoundsException $ignored) {
}
if (empty($lock_settings)) {
return new AccessResultAllowed();
}
}
$default_components = [];
try {
if ($section_storage instanceof OverridesSectionStorage) {
$default_components = $section_storage
->getDefaultSectionStorage()
->getSection($delta)
->getComponents();
}
else {
$default_components = $section_storage
->getSection($delta)
->getComponents();
}
} catch (\OutOfBoundsException $ignored) {
}
$access = $section_storage
->access($operation, $account, TRUE);
switch ($operation) {
case 'block_add':
if (isset($lock_settings[LayoutBuilderLock::LOCKED_BLOCK_ADD])) {
return new AccessResultForbidden();
}
break;
case 'block_config':
$uuid = $this->routeMatch
->getRawParameter('uuid');
if (isset($lock_settings[LayoutBuilderLock::LOCKED_BLOCK_UPDATE]) && isset($default_components[$uuid])) {
$access = new AccessResultForbidden();
}
break;
case 'block_remove':
$uuid = $this->routeMatch
->getRawParameter('uuid');
if (isset($lock_settings[LayoutBuilderLock::LOCKED_BLOCK_DELETE]) && isset($default_components[$uuid])) {
$access = new AccessResultForbidden();
}
break;
case 'block_reorder':
$uuid = $this->routeMatch
->getRawParameter('uuid');
if (isset($lock_settings[LayoutBuilderLock::LOCKED_BLOCK_MOVE]) && isset($default_components[$uuid])) {
$access = new AccessResultForbidden();
}
if (isset($lock_settings[LayoutBuilderLock::LOCKED_SECTION_BLOCK_MOVE])) {
try {
if (count($section_storage
->getSection($delta)
->getComponents()) == count($default_components)) {
$access = new AccessResultForbidden();
}
} catch (\OutOfBoundsException $ignored) {
}
}
break;
case 'section_add':
if ($check_before_and_after) {
if (isset($lock_settings_before[LayoutBuilderLock::LOCKED_SECTION_AFTER]) || isset($lock_settings_after[LayoutBuilderLock::LOCKED_SECTION_BEFORE]) || isset($lock_settings_after[LayoutBuilderLock::LOCKED_SECTION_BEFORE])) {
$access = new AccessResultForbidden();
}
}
else {
if (isset($lock_settings[LayoutBuilderLock::LOCKED_SECTION_BEFORE])) {
$access = new AccessResultForbidden();
}
}
break;
case 'section_edit':
if (isset($lock_settings[LayoutBuilderLock::LOCKED_SECTION_CONFIGURE]) && !$account
->hasPermission('manage lock settings on overrides')) {
$access = new AccessResultForbidden();
}
break;
case 'section_remove':
$access = new AccessResultForbidden();
break;
}
if ($access instanceof RefinableCacheableDependencyInterface) {
$access
->addCacheableDependency($section_storage);
}
return $access;
}
}