Lines: 100.00%
| Line | Hits | Source |
|---|---|---|
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Drupal\visitors\Service; | |
| 6 | ||
| 7 | use Drupal\Core\Condition\ConditionAccessResolverTrait; | |
| 8 | use Drupal\Core\Condition\ConditionPluginCollection; | |
| 9 | use Drupal\Core\Config\ConfigFactoryInterface; | |
| 10 | use Drupal\Core\Executable\ExecutableManagerInterface; | |
| 11 | use Drupal\Core\Plugin\Context\ContextHandlerInterface; | |
| 12 | use Drupal\Core\Plugin\Context\ContextRepositoryInterface; | |
| 13 | use Drupal\Core\Session\AccountInterface; | |
| 14 | use Drupal\user\UserDataInterface; | |
| 15 | use Drupal\visitors\VisitorsVisibilityInterface; | |
| 16 | ||
| 17 | /** | |
| 18 | * Service for checking visitors visibility. | |
| 19 | */ | |
| 20 | final class VisibilityService implements VisitorsVisibilityInterface { | |
| 21 | ||
| 22 | use ConditionAccessResolverTrait; | |
| 23 | ||
| 24 | /** | |
| 25 | * The config object. | |
| 26 | * | |
| 27 | * @var \Drupal\Core\Config\ImmutableConfig | |
| 28 | */ | |
| 29 | protected $config; | |
| 30 | ||
| 31 | /** | |
| 32 | * The condition plugin manager. | |
| 33 | * | |
| 34 | * @var \Drupal\Core\Executable\ExecutableManagerInterface | |
| 35 | */ | |
| 36 | protected ExecutableManagerInterface $conditionManager; | |
| 37 | ||
| 38 | /** | |
| 39 | * The context repository. | |
| 40 | * | |
| 41 | * @var \Drupal\Core\Plugin\Context\ContextRepositoryInterface | |
| 42 | */ | |
| 43 | protected ContextRepositoryInterface $contextRepository; | |
| 44 | ||
| 45 | /** | |
| 46 | * The context handler. | |
| 47 | * | |
| 48 | * @var \Drupal\Core\Plugin\Context\ContextHandlerInterface | |
| 49 | */ | |
| 50 | protected ContextHandlerInterface $contextHandler; | |
| 51 | ||
| 52 | /** | |
| 53 | * The visibility condition collection. | |
| 54 | * | |
| 55 | * @var \Drupal\Core\Condition\ConditionPluginCollection|null | |
| 56 | */ | |
| 57 | protected ?ConditionPluginCollection $conditionCollection = NULL; | |
| 58 | ||
| 59 | /** | |
| 60 | * The current user. | |
| 61 | * | |
| 62 | * @var \Drupal\Core\Session\AccountInterface | |
| 63 | */ | |
| 64 | protected AccountInterface $currentUser; | |
| 65 | ||
| 66 | /** | |
| 67 | * The user data service. | |
| 68 | * | |
| 69 | * @var \Drupal\user\UserDataInterface | |
| 70 | */ | |
| 71 | protected UserDataInterface $userData; | |
| 72 | ||
| 73 | /** | |
| 74 | * Constructs a new VisibilityService. | |
| 75 | * | |
| 76 | * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory | |
| 77 | * The config factory. | |
| 78 | * @param \Drupal\Core\Executable\ExecutableManagerInterface $condition_manager | |
| 79 | * The condition plugin manager. | |
| 80 | * @param \Drupal\Core\Plugin\Context\ContextRepositoryInterface $context_repository | |
| 81 | * The context repository. | |
| 82 | * @param \Drupal\Core\Plugin\Context\ContextHandlerInterface $context_handler | |
| 83 | * The context handler. | |
| 84 | * @param \Drupal\Core\Session\AccountInterface $current_user | |
| 85 | * The current user. | |
| 86 | * @param \Drupal\user\UserDataInterface $user_data | |
| 87 | * The user data service. | |
| 88 | */ | |
| 89 | public function __construct( | |
| 90 | ConfigFactoryInterface $config_factory, | |
| 91 | ExecutableManagerInterface $condition_manager, | |
| 92 | ContextRepositoryInterface $context_repository, | |
| 93 | ContextHandlerInterface $context_handler, | |
| 94 | AccountInterface $current_user, | |
| 95 | UserDataInterface $user_data, | |
| 96 | ) { | |
| 97 | 8 | $this->config = $config_factory->get('visitors.settings'); |
| 98 | 8 | $this->conditionManager = $condition_manager; |
| 99 | 8 | $this->contextRepository = $context_repository; |
| 100 | 8 | $this->contextHandler = $context_handler; |
| 101 | 8 | $this->currentUser = $current_user; |
| 102 | 8 | $this->userData = $user_data; |
| 103 | } | |
| 104 | ||
| 105 | /** | |
| 106 | * {@inheritdoc} | |
| 107 | */ | |
| 108 | public function isVisible(): bool { | |
| 109 | ||
| 110 | 8 | if ($this->config->get('disable_tracking')) { |
| 111 | 1 | return FALSE; |
| 112 | } | |
| 113 | ||
| 114 | 8 | if ($this->isUserExcluded()) { |
| 115 | 3 | return FALSE; |
| 116 | } | |
| 117 | ||
| 118 | 8 | return $this->evaluateTrackingConditions(); |
| 119 | } | |
| 120 | ||
| 121 | /** | |
| 122 | * Whether the current account is excluded from tracking. | |
| 123 | * | |
| 124 | * Anonymous users have no account choice. User 1 is excluded only when | |
| 125 | * that setting is saved and enabled. | |
| 126 | * | |
| 127 | * @return bool | |
| 128 | * TRUE when this account must not be tracked. | |
| 129 | */ | |
| 130 | protected function isUserExcluded(): bool { | |
| 131 | 8 | $exclude_user1 = (bool) $this->config->get('visibility.exclude_user1'); |
| 132 | 8 | if ($exclude_user1 && (int) $this->currentUser->id() === 1) { |
| 133 | 1 | return TRUE; |
| 134 | } | |
| 135 | ||
| 136 | 8 | $mode = (int) $this->config->get('visibility.user_account_mode'); |
| 137 | 8 | if ($mode === VisitorsVisibilityInterface::USER_NO_PERSONALIZATION) { |
| 138 | 4 | return FALSE; |
| 139 | } | |
| 140 | 6 | if (!$this->currentUser->isAuthenticated()) { |
| 141 | 4 | return FALSE; |
| 142 | } | |
| 143 | ||
| 144 | 2 | $stored = $this->userData->get( |
| 145 | 2 | 'visitors', |
| 146 | 2 | (int) $this->currentUser->id(), |
| 147 | 2 | 'user_account_users', |
| 148 | 2 | ); |
| 149 | // Opt-in mode tracks only an explicit choice to enable tracking. | |
| 150 | 2 | if ($stored === NULL) { |
| 151 | 1 | return $mode === VisitorsVisibilityInterface::USER_OPT_IN; |
| 152 | } | |
| 153 | ||
| 154 | 2 | return (int) $stored !== 1; |
| 155 | } | |
| 156 | ||
| 157 | /** | |
| 158 | * Evaluates tracking conditions using the condition plugin system. | |
| 159 | * | |
| 160 | * @return bool | |
| 161 | * TRUE if all conditions pass (AND logic), FALSE otherwise. | |
| 162 | */ | |
| 163 | protected function evaluateTrackingConditions(): bool { | |
| 164 | 8 | $conditions = $this->getTrackingConditions(); |
| 165 | ||
| 166 | 8 | if ($conditions->count() === 0) { |
| 167 | 8 | return TRUE; |
| 168 | } | |
| 169 | ||
| 170 | 3 | return $this->resolveConditions(iterator_to_array($conditions), 'and'); |
| 171 | } | |
| 172 | ||
| 173 | /** | |
| 174 | * Gets the tracking condition plugin collection. | |
| 175 | * | |
| 176 | * @return \Drupal\Core\Condition\ConditionPluginCollection | |
| 177 | * The condition plugin collection. | |
| 178 | */ | |
| 179 | protected function getTrackingConditions(): ConditionPluginCollection { | |
| 180 | 8 | if (!isset($this->conditionCollection)) { |
| 181 | 8 | $conditions = $this->config->get('tracking_conditions') ?: []; |
| 182 | // Theme has no empty option, so a saved value would limit tracking | |
| 183 | // to one theme. Ignore it even when older config still stores it. | |
| 184 | 8 | unset($conditions['current_theme']); |
| 185 | 8 | $this->conditionCollection = new ConditionPluginCollection( |
| 186 | 8 | $this->conditionManager, |
| 187 | 8 | $conditions |
| 188 | 8 | ); |
| 189 | } | |
| 190 | 8 | return $this->conditionCollection; |
| 191 | } | |
| 192 | ||
| 193 | } |