-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCacheExclusionChecker.php
executable file
·176 lines (151 loc) · 5.05 KB
/
CacheExclusionChecker.php
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
<?php
namespace Statamic\Addons\SuperStaticCache\Service;
use Illuminate\Http\Request;
use Statamic\API\Role;
use Statamic\API\Str;
use Statamic\Contracts\Data\Users\User;
use Statamic\Data\Services\UserGroupsService;
/**
* Check if the current request or user should be excluded from static caching.
*/
class CacheExclusionChecker
{
/**
* @var array
*/
private $config;
/**
* @var UserGroupsService
*/
private $userGroupsService;
/**
* @param array $config
* @param UserGroupsService $userGroupsService
*/
public function __construct(array $config, UserGroupsService $userGroupsService)
{
$this->config = collect($config);
$this->userGroupsService = $userGroupsService;
}
/**
* Check if the given request should be excluded from static caching.
*
* @param Request $request
*
* @return bool
*/
public function isExcluded(Request $request)
{
if ($this->isExcludedForQueryString($request)) {
return true;
}
// Do not exclude for anonymous users.
if (!$request->user()) {
return false;
}
return $this->isExcludedForUser($request->user());
}
/**
* Check if the given user should be excluded from static caching.
*
* @param User $user
*
* @return bool
*/
public function isExcludedForUser(User $user)
{
// Do not exclude if the cache is not disabled for authenticated users.
if (!$this->config->get('cache_disabled_authenticated')) {
return false;
}
$isExcluded = true;
// Check if the cache should only be skipped for defined user roles.
$roles = $this->config->get('cache_disabled_user_roles', []);
if (count($roles)) {
$isExcluded = false;
foreach ($roles as $role) {
if ($user->hasRole(Role::whereHandle($role))) {
return true;
}
}
}
// Check if the cache should only be skipped for defined user groups.
$groups = $this->config->get('cache_disabled_user_groups', []);
if (count($groups)) {
$isExcluded = false;
foreach ($groups as $group) {
if ($user->inGroup($this->userGroupsService->handle($group))) {
return true;
}
}
}
return $isExcluded;
}
/**
* Check if the request's query string should be excluded from static caching.
*
* Checks if the current path has whitelisted query strings. If so, verifies
* that each request query string contains a valid value according to the
* configured regex pattern.
*
* @param Request $request
*
* @return bool
*/
public function isExcludedForQueryString(Request $request)
{
$whitelistedQueryStrings = $this->config->get('whitelisted_query_strings', []);
// Do not exclude if there are no whitelisted query strings at all.
if (!count($whitelistedQueryStrings)) {
return false;
}
$requestQueryStrings = collect($request->query->all());
// If the request does not have any query strings, we do not need to check against the whitelist.
if (!$requestQueryStrings->count()) {
return false;
}
foreach ($whitelistedQueryStrings as $path => $queryStrings) {
// 1) Continue if the path does not match.
if (!$this->matchesPath($request, $path)) {
continue;
}
// 2) Make sure that all request query params are whitelisted.
$whitelistedParams = collect($queryStrings)->keys();
$diff = $requestQueryStrings->keys()->diff($whitelistedParams);
if ($diff->count()) {
return true;
}
// 3) Validate each request query param against the defined regex pattern.
$failedParams = collect($queryStrings)->map(function ($regex, $param) use ($requestQueryStrings) {
if (!$requestQueryStrings->has($param)) {
return false;
}
$pattern = sprintf('/%s/', $regex);
if (preg_match($pattern, $requestQueryStrings->get($param))) {
return false;
}
return true;
})->filter();
if ($failedParams->count()) {
return true;
}
}
return false;
}
/**
* Check if the request's path matches a path given from the whitelisted query strings config.
*
* @param Request $request
* @param string $path
* A path given from the whitelisted query strings config.
*
* @return bool
*/
private function matchesPath(Request $request, $path)
{
if (Str::endsWith($path, '*') && Str::startsWith($request->getPathInfo(), Str::substr($path, 0, -1))) {
return true;
}
return $request->getPathInfo() === $path;
}
}