-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidator.php
More file actions
233 lines (201 loc) · 7.47 KB
/
Copy pathValidator.php
File metadata and controls
233 lines (201 loc) · 7.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php
/**
* Copyright (C) InnoCraft Ltd - All rights reserved.
*
* NOTICE: All information contained herein is, and remains the property of InnoCraft Ltd.
* The intellectual and technical concepts contained herein are protected by trade secret or copyright law.
* Redistribution of this information or reproduction of this material is strictly forbidden
* unless prior written permission is obtained from InnoCraft Ltd.
*
* You shall use this code only in accordance with the license agreement obtained from InnoCraft Ltd.
*
* @link https://www.innocraft.com/
* @license For license details see https://www.innocraft.com/license
*/
namespace Piwik\Plugins\HeatmapSessionRecording\Input;
use Piwik\Piwik;
use Piwik\Plugins\HeatmapSessionRecording\Configuration;
use Piwik\Plugins\HeatmapSessionRecording\Dao\SiteHsrDao;
use Piwik\Plugins\HeatmapSessionRecording\Settings\DisableSessionRecording;
use Piwik\Plugins\HeatmapSessionRecording\Settings\DisableHeatmapRecording;
use Piwik\Site;
use Piwik\Plugins\HeatmapSessionRecording\SystemSettings;
use Piwik\Container\StaticContainer;
use Piwik\Exception\DI\NotFoundException;
use Piwik\Plugins\FeatureFlags\FeatureFlagManager;
class Validator
{
/**
* @var Configuration
*/
private $configuration;
/**
* @var SystemSettings
*/
private $systemSettings;
public function __construct(SystemSettings $systemSettings)
{
$this->configuration = new Configuration();
$this->systemSettings = $systemSettings;
}
private function supportsMethod($method)
{
return method_exists('Piwik\Piwik', $method);
}
public function checkHasSomeWritePermission()
{
Piwik::checkUserHasSomeWriteAccess();
}
public function checkWritePermission($idSite)
{
$this->checkSiteExists($idSite);
Piwik::checkUserIsNotAnonymous();
Piwik::checkUserHasWriteAccess($idSite);
}
public function checkHeatmapReportViewPermission($idSite)
{
$this->checkSiteExists($idSite);
Piwik::checkUserHasViewAccess($idSite);
$this->checkHeatmapRecordingEnabled($idSite);
}
public function checkSessionReportViewPermission($idSite)
{
$this->checkSiteExists($idSite);
$this->checkUserIsNotAnonymousForView($idSite);
Piwik::checkUserHasViewAccess($idSite);
$this->checkSessionRecordingEnabled($idSite);
}
public function checkSessionReportWritePermission($idSite)
{
$this->checkWritePermission($idSite);
$this->checkSessionRecordingEnabled($idSite);
}
public function checkHeatmapReportWritePermission($idSite)
{
$this->checkWritePermission($idSite);
$this->checkHeatmapRecordingEnabled($idSite);
}
public function checkSessionRecordingEnabled(?int $idSite = null)
{
if ($this->isSessionRecordingDisabled($idSite)) {
throw new \Exception(Piwik::translate('HeatmapSessionRecording_ErrorSessionRecordingDisabled'));
}
}
public function checkHeatmapRecordingEnabled(?int $idSite = null)
{
if ($this->isHeatmapRecordingDisabled($idSite)) {
throw new \Exception(Piwik::translate('HeatmapSessionRecording_ErrorHeatmapRecordingDisabled'));
}
}
/**
* Check whether the current user is allowed to duplicate to specified sites.
*
* @param int[] $idSites
* @param int $hsrType Optional type indicating the permission check is for Heatmaps (1) or SessionRecordings (2).
* The default is Heatmaps.
* @return void
* @throws \Exception If heatmaps aren't enabled, the user doesn't have write permission, or the site is a rollup
*/
public function checkSitesDuplicationPermission(array $idSites, int $hsrType = SiteHsrDao::RECORD_TYPE_HEATMAP)
{
if ($hsrType === SiteHsrDao::RECORD_TYPE_HEATMAP) {
foreach ($idSites as $idSite) {
$this->checkHeatmapRecordingEnabled($idSite);
}
} elseif ($hsrType === SiteHsrDao::RECORD_TYPE_SESSION) {
foreach ($idSites as $idSite) {
$this->checkSessionRecordingEnabled($idSite);
}
} else {
throw new \Exception('Unsupported HSR Type: ' . $hsrType);
}
if (count($idSites) < 1) {
throw new \Exception(Piwik::translate('General_Required', ['idSites']));
}
Piwik::checkUserHasWriteAccess($idSites);
// Check if any of the specified sites are rollups
$rollupSiteNames = [];
foreach ($idSites as $idSite) {
if (Site::getTypeFor($idSite) === 'rollup') {
$rollupSiteNames[] = Site::getNameFor($idSite);
}
}
if (count($rollupSiteNames) > 0) {
throw new \Exception(Piwik::translate(
'HeatmapSessionRecording_HeatmapDuplicationSiteTypeError',
['\'' . implode('\', \'', $rollupSiteNames) . '\'']
));
}
}
private function checkUserIsNotAnonymousForView($idSite)
{
if ($this->configuration->isAnonymousSessionRecordingAccessEnabled($idSite)) {
Piwik::checkUserHasViewAccess($idSite);
return;
}
Piwik::checkUserIsNotAnonymous();
}
private function checkSiteExists($idSite)
{
new Site($idSite);
}
public function canViewSessionReport(int $idSite)
{
if (empty($idSite) || $this->isSessionRecordingDisabled($idSite)) {
return false;
}
if (
!$this->configuration->isAnonymousSessionRecordingAccessEnabled($idSite)
&& Piwik::isUserIsAnonymous()
) {
return false;
}
return Piwik::isUserHasViewAccess($idSite);
}
public function canViewHeatmapReport(int $idSite)
{
if (empty($idSite) || $this->isHeatmapRecordingDisabled($idSite)) {
return false;
}
return Piwik::isUserHasViewAccess($idSite);
}
public function canWrite($idSite)
{
if (empty($idSite)) {
return false;
}
return Piwik::isUserHasWriteAccess($idSite);
}
public function isSessionRecordingDisabled(?int $idSite = null)
{
try {
$featureFlagManager = StaticContainer::get(FeatureFlagManager::class);
if ($featureFlagManager->isFeatureActive('Piwik\Plugins\PrivacyManager\FeatureFlags\PrivacyCompliance')) {
return DisableSessionRecording::getInstance($idSite)->getValue();
}
} catch (NotFoundException $e) {
/*
if the feature flag manager cannot be loaded,
just move on to backup/original setting
value retrieval
*/
}
return $this->systemSettings->disableSessionRecording->getValue();
}
public function isHeatmapRecordingDisabled(?int $idSite = null)
{
try {
$featureFlagManager = StaticContainer::get(FeatureFlagManager::class);
if ($featureFlagManager->isFeatureActive('Piwik\Plugins\PrivacyManager\FeatureFlags\PrivacyCompliance')) {
return DisableHeatmapRecording::getInstance($idSite)->getValue();
}
} catch (NotFoundException $e) {
/*
if the feature flag manager cannot be loaded,
just move on to backup/original setting
value retrieval
*/
}
return $this->systemSettings->disableHeatmapRecording->getValue();
}
}