SnippetAccess.php in Hotjar 8.2
File
src/SnippetAccess.php
View source
<?php
namespace Drupal\hotjar;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\path_alias\AliasManagerInterface;
use Drupal\Core\Path\CurrentPathStack;
use Drupal\Core\Path\PathMatcherInterface;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class SnippetAccess implements SnippetAccessInterface, ContainerInjectionInterface {
const ACCESS_ALLOW = TRUE;
const ACCESS_DENY = FALSE;
const ACCESS_IGNORE = NULL;
protected $settings;
protected $moduleHandler;
protected $configFactory;
protected $currentPath;
protected $aliasManager;
protected $pathMatcher;
protected $requestStack;
protected $currentUser;
protected $pageMatch;
public function __construct(HotjarSettingsInterface $hotjar_settings, ModuleHandlerInterface $module_handler, ConfigFactoryInterface $config_factory, CurrentPathStack $current_path, AliasManagerInterface $alias_manager, PathMatcherInterface $path_matcher, AccountInterface $current_user, RequestStack $request_stack) {
$this->settings = $hotjar_settings;
$this->moduleHandler = $module_handler;
$this->configFactory = $config_factory;
$this->currentPath = $current_path;
$this->aliasManager = $alias_manager;
$this->pathMatcher = $path_matcher;
$this->currentUser = $current_user;
$this->requestStack = $request_stack;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('hotjar.settings'), $container
->get('module_handler'), $container
->get('config.factory'), $container
->get('path.current'), $container
->get('path_alias.manager'), $container
->get('path.matcher'), $container
->get('current_user'), $container
->get('request_stack'));
}
public function check() {
if (!$this->settings
->getSetting('account')) {
return FALSE;
}
$result = AccessResult::neutral()
->andIf($this
->statusCheckResult())
->andIf($this
->pathCheckResult())
->andIf($this
->roleCheck())
->andIf($this
->cookieConstentCheck());
$access = [];
foreach ($this->moduleHandler
->getImplementations('hotjar_access') as $module) {
$module_result = $this->moduleHandler
->invoke($module, 'hotjar_access');
if (is_bool($module_result)) {
$access[$module] = $module_result;
}
elseif ($module_result instanceof AccessResult) {
$access[$module] = !$module_result
->isForbidden();
}
}
$this->moduleHandler
->alter('hotjar_access', $access);
foreach ($access as $module_result) {
if (is_bool($module_result)) {
$result = $result
->andIf(AccessResult::forbiddenIf(!$module_result));
}
elseif ($module_result instanceof AccessResult) {
$result = $result
->andIf($module_result);
}
}
return !$result
->isForbidden();
}
protected function statusCheckResult() {
$request = $this->requestStack
->getCurrentRequest();
$status = NULL;
if ($exception = $request->attributes
->get('exception')) {
$status = $exception
->getStatusCode();
}
$not_tracked_status_codes = [
'403',
'404',
];
$result = !in_array($status, $not_tracked_status_codes);
return AccessResult::forbiddenIf(!$result);
}
protected function pathCheckResult() {
if (!isset($this->pageMatch)) {
$visibility = $this->settings
->getSetting('visibility_pages');
$setting_pages = $this->settings
->getSetting('pages');
if (!$setting_pages) {
$this->pageMatch = TRUE;
return AccessResult::allowed();
}
$pages = _hotjar_clean_pages_value(mb_strtolower($setting_pages));
if ($visibility < 2) {
$path = $this->currentPath
->getPath();
$path_alias = mb_strtolower($this->aliasManager
->getAliasByPath($path));
$path_match = $this->pathMatcher
->matchPath($path_alias, $pages);
$alias_match = $path != $path_alias && $this->pathMatcher
->matchPath($path, $pages);
$this->pageMatch = $path_match || $alias_match;
$this->pageMatch = !($visibility xor $this->pageMatch);
}
else {
$this->pageMatch = FALSE;
}
}
return AccessResult::forbiddenIf(!$this->pageMatch);
}
protected function roleCheck() {
$visibility = $this->settings
->getSetting('visibility_roles');
$enabled = $visibility;
$roles = $this->settings
->getSetting('roles');
$checked_roles = array_filter($roles);
if (empty($checked_roles)) {
return AccessResult::allowed();
}
if (count(array_intersect($this->currentUser
->getRoles(), $checked_roles))) {
$enabled = !$visibility;
}
return AccessResult::forbiddenIf(!$enabled);
}
protected function cookieConstentCheck() {
if ($this->moduleHandler
->moduleExists('eu_cookie_compliance')) {
$config = $this->configFactory
->get('eu_cookie_compliance.settings');
$disabled_javascripts = $config
->get('disabled_javascripts');
$disabled_javascripts = _eu_cookie_compliance_explode_multiple_lines($disabled_javascripts);
$snippet_path = $this->settings
->getSetting('snippet_path');
_eu_cookie_compliance_convert_relative_uri($snippet_path);
if (in_array($snippet_path, $disabled_javascripts)) {
return AccessResult::forbidden();
}
}
return AccessResult::neutral();
}
}