blob: e0693b7972e9dc04ea2f404ef4f557612179c900 (
plain)
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
|
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Access;
use Exception;
use Piwik\CacheId;
use Piwik\Piwik;
use Piwik\Cache as PiwikCache;
class CapabilitiesProvider
{
/**
* @return Capability[]
* @throws Exception
*/
public function getAllCapabilities(): array
{
$cacheId = CacheId::siteAware(CacheId::languageAware('Capabilities'));
$cache = PiwikCache::getTransientCache();
if (!$cache->contains($cacheId)) {
$capabilities = [];
/**
* Triggered to add new capabilities.
*
* **Example**
*
* public function addCapabilities(&$capabilities)
* {
* $capabilities[] = new MyNewCapabilitiy();
* }
*
* @param Capability[] $reports An array of reports
* @internal
*/
Piwik::postEvent('Access.Capability.addCapabilities', array(&$capabilities));
/**
* Triggered to filter / restrict capabilities.
*
* **Example**
*
* public function filterCapabilities(&$capabilities)
* {
* foreach ($capabilities as $index => $capability) {
* if ($capability->getId() === 'tagmanager_write') {}
* unset($capabilities[$index]); // remove the given capability
* }
* }
* }
*
* @param Capability[] $reports An array of reports
* @internal
*/
Piwik::postEvent('Access.Capability.filterCapabilities', array(&$capabilities));
$capabilities = \array_values($capabilities);
$this->checkCapabilityIds($capabilities);
$cache->save($cacheId, $capabilities);
return $capabilities;
}
return $cache->fetch($cacheId);
}
/**
* @param $capabilityId
* @return Capability|null
* @throws Exception
*/
public function getCapability(string $capabilityId): ?Capability
{
foreach ($this->getAllCapabilities() as $capability) {
if ($capabilityId === $capability->getId()) {
return $capability;
}
}
return null;
}
/**
* @return string[]
* @throws Exception
*/
public function getAllCapabilityIds(): array
{
$ids = array();
foreach ($this->getAllCapabilities() as $capability) {
$ids[] = $capability->getId();
}
return $ids;
}
/**
* @param $capabilityId
* @return bool
* @throws Exception
*/
public function isValidCapability($capabilityId): bool
{
$capabilities = $this->getAllCapabilityIds();
return \in_array($capabilityId, $capabilities, true);
}
/**
* @param $capabilityId
* @throws Exception
*/
public function checkValidCapability($capabilityId): void
{
if (!$this->isValidCapability($capabilityId)) {
$capabilities = $this->getAllCapabilityIds();
throw new \Exception(Piwik::translate("UsersManager_ExceptionAccessValues", implode(", ", $capabilities)));
}
}
/**
* @param Capability[] $capabilities
* @throws Exception
*/
private function checkCapabilityIds(array $capabilities): void
{
foreach ($capabilities as $capability) {
$id = $capability->getId();
if (preg_match('/[^a-zA-Z0-9_-]/', $id)) {
throw new \Exception("Capability with invalid ID found: '$id'. Valid characters are 'a-zA-Z0-9_-'.");
}
}
}
}
|