1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ONGR package. |
5
|
|
|
* |
6
|
|
|
* (c) NFQ Technologies UAB <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace ONGR\SettingsBundle\Settings\Personal; |
13
|
|
|
|
14
|
|
|
use Stash\Pool; |
15
|
|
|
use ONGR\SettingsBundle\Settings\Personal\SettingsStructure; |
16
|
|
|
use Symfony\Component\Security\Core\Authorization\AuthorizationChecker; |
17
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage; |
18
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
19
|
|
|
use ONGR\SettingsBundle\Event\SettingChangeEvent; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Service responsible as a gateway to user settings. |
23
|
|
|
*/ |
24
|
|
|
class PersonalSettingsManager |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
const ROLE_GRANTED = 'ROLE_ADMIN'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
const STASH_NAME = 'ongr_settings'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var EventDispatcherInterface |
38
|
|
|
*/ |
39
|
|
|
protected $eventDispatcher; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
private $userParam; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Stash name for the current user |
48
|
|
|
* |
49
|
|
|
* @var string |
50
|
|
|
*/ |
51
|
|
|
private $stash; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var AuthorizationChecker |
55
|
|
|
*/ |
56
|
|
|
private $securityContext; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var TokenStorage |
60
|
|
|
*/ |
61
|
|
|
private $token; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var SettingsStructure |
65
|
|
|
*/ |
66
|
|
|
private $settingsStructure; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @var array; |
70
|
|
|
*/ |
71
|
|
|
private $userSettings = []; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Stash for storing personal settings |
75
|
|
|
* |
76
|
|
|
* @var \Pool |
77
|
|
|
*/ |
78
|
|
|
private $pool; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param String $userParam |
82
|
|
|
* @param SettingsStructure $settingsStructure |
83
|
|
|
* @param EventDispatcherInterface $eventDispatcher |
84
|
|
|
* @param AuthorizationChecker $authorization |
85
|
|
|
* @param TokenStorage $token |
86
|
|
|
* @param Pool $pool |
87
|
|
|
*/ |
88
|
|
|
public function __construct($userParam, $settingsStructure, $eventDispatcher, $authorization, $token, $pool) |
89
|
|
|
{ |
90
|
|
|
$this->settingsStructure = $settingsStructure; |
91
|
|
|
$this->eventDispatcher = $eventDispatcher; |
92
|
|
|
$this->securityContext = $authorization; |
93
|
|
|
$this->token = $token; |
94
|
|
|
$this->pool = $pool; |
|
|
|
|
95
|
|
|
$this->userParam = $userParam; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Sets settings for the current user from stash |
100
|
|
|
*/ |
101
|
|
|
public function setSettingsFromStash() |
102
|
|
|
{ |
103
|
|
|
$this->stash = $this->getStashName($this->token->getToken()->getUser()); |
104
|
|
|
$stashedSettings = $this->pool->getItem($this->stash)->get(); |
105
|
|
|
if (is_array($stashedSettings)) { |
106
|
|
|
$this->userSettings = $stashedSettings; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param array $settings |
112
|
|
|
*/ |
113
|
|
|
public function setSettings(array $settings) |
114
|
|
|
{ |
115
|
|
|
$this->userSettings = $settings; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* If user logged in, returns setting value from cookie. Else, returns false. |
120
|
|
|
* |
121
|
|
|
* @param string $settingName |
122
|
|
|
* @param bool $mustAuthorize |
123
|
|
|
* |
124
|
|
|
* @return bool |
125
|
|
|
*/ |
126
|
|
|
public function getSettingEnabled($settingName, $mustAuthorize = true) |
127
|
|
|
{ |
128
|
|
|
if ($mustAuthorize && !$this->isAuthenticated()) { |
129
|
|
|
return false; |
130
|
|
|
} |
131
|
|
|
$this->setSettingsFromStash(); |
132
|
|
|
if (isset($this->userSettings[$settingName])) { |
133
|
|
|
return $this->userSettings[$settingName]; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return false; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Saves the current settings to stash |
141
|
|
|
* |
142
|
|
|
* @throws \BadMethodCallException |
143
|
|
|
*/ |
144
|
|
|
public function save() |
145
|
|
|
{ |
146
|
|
|
$stashedSettings = $this->pool->getItem($this->stash); |
147
|
|
|
$stashedSettings->set($this->userSettings); |
148
|
|
|
$this->pool->save($stashedSettings); |
149
|
|
|
|
150
|
|
|
$this->eventDispatcher->dispatch('ongr_settings.setting_change', new SettingChangeEvent('save')); |
|
|
|
|
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Clears the stash |
155
|
|
|
*/ |
156
|
|
|
public function stashClear() |
157
|
|
|
{ |
158
|
|
|
$this->pool->deleteItem($this->stash); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @return bool |
163
|
|
|
*/ |
164
|
|
|
public function isAuthenticated() |
165
|
|
|
{ |
166
|
|
|
return $this->securityContext->isGranted(self::ROLE_GRANTED); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @return array |
171
|
|
|
*/ |
172
|
|
|
public function getSettings() |
173
|
|
|
{ |
174
|
|
|
return $this->userSettings; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Gets active profiles |
179
|
|
|
* |
180
|
|
|
* @return array |
181
|
|
|
*/ |
182
|
|
|
public function getActiveProfiles() |
183
|
|
|
{ |
184
|
|
|
$profiles = []; |
185
|
|
|
$this->setSettingsFromStash(); |
186
|
|
|
foreach ($this->userSettings as $name => $userSetting) { |
187
|
|
|
if (preg_match('/^ongr_settings_profile_.*/', $name) && $userSetting) { |
188
|
|
|
$escapedProfile = mb_substr($name, 22, null, 'UTF-8'); |
189
|
|
|
$profiles[] = UnderscoreEscaper::unescape($escapedProfile); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
return $profiles; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @return array |
197
|
|
|
*/ |
198
|
|
|
public function getSettingsMap() |
199
|
|
|
{ |
200
|
|
|
return $this->settingsStructure->getStructure(); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @return array |
205
|
|
|
*/ |
206
|
|
|
public function getCategoryMap() |
207
|
|
|
{ |
208
|
|
|
return $this->settingsStructure->getCategoriesStructure(); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Returns the full name of the stash for the current |
213
|
|
|
* User. If the unique user property value cant be determined |
214
|
|
|
* returns false. |
215
|
|
|
* |
216
|
|
|
* @param $user |
217
|
|
|
* |
218
|
|
|
* @return string |
219
|
|
|
* @throws \BadMethodCallException |
220
|
|
|
*/ |
221
|
|
|
private function getStashName($user) |
222
|
|
|
{ |
223
|
|
|
$property = $this->guessPropertyOrMethodName($user); |
224
|
|
|
$stashName = self::STASH_NAME.'_'; |
225
|
|
|
try { |
226
|
|
|
if ($property[0] == 'public') { |
227
|
|
|
$stashName = $stashName . $user->$property[1]; |
228
|
|
|
} else { |
229
|
|
|
$method = $property[1]; |
230
|
|
|
$stashName = $stashName . $user->$method(); |
231
|
|
|
} |
232
|
|
|
$this->stash = $stashName; |
233
|
|
|
return $stashName; |
234
|
|
|
} catch (\Exception $e) { |
235
|
|
|
throw new \BadMethodCallException( |
236
|
|
|
'Ongr could not guess the getter method for your defined user property.' |
237
|
|
|
); |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Returns the property visibility and if its |
243
|
|
|
* private, guesses the name of the getter |
244
|
|
|
* |
245
|
|
|
* @param $user |
246
|
|
|
* |
247
|
|
|
* @return array |
248
|
|
|
*/ |
249
|
|
|
private function guessPropertyOrMethodName($user) |
250
|
|
|
{ |
251
|
|
|
$property = $this->userParam; |
252
|
|
|
if (isset($user->$property)) { |
253
|
|
|
return ['public', $this->userParam]; |
254
|
|
|
} else { |
255
|
|
|
return ['private', $this->toCamelCase($this->userParam)]; |
256
|
|
|
} |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Converts a string to camel case |
261
|
|
|
* |
262
|
|
|
* @param string $name |
263
|
|
|
* |
264
|
|
|
* @return string |
265
|
|
|
*/ |
266
|
|
|
private function toCamelCase($name) |
267
|
|
|
{ |
268
|
|
|
$return = explode('_', $name); |
269
|
|
|
foreach ($return as $key => $item) { |
270
|
|
|
$return[$key] = ucfirst($item); |
271
|
|
|
} |
272
|
|
|
return 'get'.implode('', $return); |
273
|
|
|
} |
274
|
|
|
} |
275
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..