You are here

protected function SnippetAccess::roleCheck in Hotjar 8.2

Same name and namespace in other branches
  1. 8 src/SnippetAccess.php \Drupal\hotjar\SnippetAccess::roleCheck()

Check Hotjar code should be added for user.

Return value

\Drupal\Core\Access\AccessResult Result.

File

src/SnippetAccess.php, line 248

Class

SnippetAccess
Snippet access service.

Namespace

Drupal\hotjar

Code

protected function roleCheck() {
  $visibility = $this->settings
    ->getSetting('visibility_roles');
  $enabled = $visibility;
  $roles = $this->settings
    ->getSetting('roles');

  // The hotjar_roles stores the selected roles as an array where
  // the keys are the role IDs. When the role is not selected the
  // value is 0. If a role is selected the value is the role ID.
  $checked_roles = array_filter($roles);
  if (empty($checked_roles)) {

    // No role is selected for tracking, therefore all roles be tracked.
    return AccessResult::allowed();
  }
  if (count(array_intersect($this->currentUser
    ->getRoles(), $checked_roles))) {
    $enabled = !$visibility;
  }
  return AccessResult::forbiddenIf(!$enabled);
}