Commit dd513670 authored by Steven Ayers's avatar Steven Ayers
Browse files

Issue #1220744 by bluegeek9: Exclude users with role

parent 3c7c0fca
Loading
Loading
Loading
Loading

composer.json

0 → 100644
+11 −0
Original line number Diff line number Diff line
{
    "name": "drupal/visitors",
    "description": "Visitors is a native drupal web analytics software.",
    "type": "drupal-module",
    "license": "GPL-2.0+",
    "require": {
        "drupal/core": "^8 || ^9 || ^10"
    },
    "require-dev": {
    }
}
+2 −1
Original line number Diff line number Diff line
chart_height: 430
chart_width: 700
exclude_administer_users: 0
exclude_user1: 0
excluded_roles: []
flush_log_timer: 0
items_per_page: 10
show_last_registered_user: 1
+5 −1
Original line number Diff line number Diff line
@@ -6,8 +6,12 @@ visitors.config:
      type: integer
    chart_width:
      type: integer
    exclude_administer_users:
    exclude_user1:
      type: boolean
    excluded_roles:
      sequence:
        type: string
        label: 'roles'
    flush_log_timer:
      type: integer
    items_per_page:
+47 −41
Original line number Diff line number Diff line
@@ -12,7 +12,6 @@ use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\PostResponseEvent;
use Symfony\Component\HttpKernel\Event\TerminateEvent;
use Symfony\Component\HttpKernel\KernelEvents;

@@ -113,14 +112,22 @@ class KernelTerminateSubscriber implements EventSubscriberInterface {
  public function onTerminate(TerminateEvent $event) {
    $this->request = $event->getRequest();
    $config = $this->configFactory->get('visitors.config');
    if (!is_null($this->currentUser)) {
      $not_admin = !in_array('administrator', $this->currentUser->getRoles());
    $exclude_user1 = $config->get('exclude_user1');
    $excluded_roles = array_values($config->get('excluded_roles'));

    $skip_because_user1 = $this->currentUser->id() == 1 && $exclude_user1;
    if ($skip_because_user1) {
      return NULL;
    }

    $user_roles = $this->currentUser->getRoles();
    foreach ($excluded_roles as $role) {
      $skip_because_role = in_array($role, $user_roles, TRUE);
      if ($skip_because_role) {
        return NULL;
      }
    else {
      $not_admin = TRUE;
    }
    $log_admin = !$config->get('exclude_administer_users');
    if ($log_admin || $not_admin) {

    $ip_str = $this->getIpStr();
    $fields = [
      'visitors_uid'        => $this->currentUser->id(),
@@ -155,8 +162,7 @@ class KernelTerminateSubscriber implements EventSubscriberInterface {
        ->execute();
    }
    catch (\Exception $e) {

      }
      watchdog_exception('visitors', $e);
    }
  }

+33 −7
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@
namespace Drupal\visitors\Form;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ThemeExtensionList;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
@@ -29,6 +30,13 @@ class Settings extends ConfigFormBase {
   */
  protected $themeList;

  /**
   * An extension discovery instance.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Constructs a Visitors Settings Form.
   *
@@ -36,10 +44,13 @@ class Settings extends ConfigFormBase {
   *   The config factory.
   * @param \Drupal\Core\Extension\ThemeExtensionList $theme_list
   *   The theme extension list.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The enttyity_type.manager service.
   */
  public function __construct(ConfigFactoryInterface $config_factory, ThemeExtensionList $theme_list) {
  public function __construct(ConfigFactoryInterface $config_factory, ThemeExtensionList $theme_list, EntityTypeManagerInterface $entity_type_manager) {
    parent::__construct($config_factory);
    $this->themeList = $theme_list;
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
@@ -48,7 +59,8 @@ class Settings extends ConfigFormBase {
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('config.factory'),
      $container->get('extension.list.theme')
      $container->get('extension.list.theme'),
      $container->get('entity_type.manager')
    );
  }

@@ -149,11 +161,24 @@ class Settings extends ConfigFormBase {
      '#description' => $this->t('Visitors statistics settings'),
    ];

    $form['statistics']['exclude_administer_users'] = [
    $form['statistics']['exclude_user1'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Exclude administer users from statistics'),
      '#default_value' => $config->get('exclude_administer_users'),
      '#description' => $this->t('Exclude hits of administer users from statistics.'),
      '#title' => $this->t('Exclude user1 from statistics'),
      '#default_value' => $config->get('exclude_user1'),
      '#description' => $this->t('Exclude hits of user1 from statistics.'),
    ];

    $roles = [];
    foreach ($this->entityTypeManager->getStorage('user_role')->loadMultiple() as $name => $role) {
      $roles[$name] = $role->label();
    }

    $form['statistics']['excluded_roles'] = [
      '#type' => 'checkboxes',
      '#options' => $roles,
      '#title' => $this->t('Exclude users with roles from statistics'),
      '#default_value' => $config->get('excluded_roles'),
      '#description' => $this->t('Exclude hits of users with role(s) from statistics.'),
    ];

    $all_themes = $this->themeList->getList();
@@ -270,7 +295,8 @@ class Settings extends ConfigFormBase {
      ->set('show_published_nodes', $values['show_published_nodes'])
      ->set('show_user_ip', $values['show_user_ip'])
      ->set('show_since_date', $values['show_since_date'])
      ->set('exclude_administer_users', $values['exclude_administer_users'])
      ->set('exclude_user1', $values['exclude_user1'])
      ->set('excluded_roles', $values['excluded_roles'])
      ->set('theme', $values['theme'])
      ->set('items_per_page', $values['items_per_page'])
      ->set('flush_log_timer', $values['flush_log_timer'])
Loading