26
26
27
27
use Psr \Container \ContainerInterface ;
28
28
use SP \Core \Context \ContextInterface ;
29
+ use SP \Core \Context \SessionContext ;
29
30
use SP \Core \Events \Event ;
30
31
use SP \Core \UI \ThemeInterface ;
31
- use SP \DataModel \PluginData ;
32
32
use SP \Modules \Web \Plugins \Authenticator \Controllers \PreferencesController ;
33
33
use SP \Modules \Web \Plugins \Authenticator \Models \AuthenticatorData ;
34
+ use SP \Modules \Web \Plugins \Authenticator \Services \UpgradeService ;
34
35
use SP \Modules \Web \Plugins \Authenticator \Util \PluginContext ;
35
36
use SP \Mvc \Controller \ExtensibleTabControllerInterface ;
36
37
use SP \Plugin \PluginBase ;
37
- use SP \Util \Util ;
38
+ use SP \Plugin \PluginOperation ;
39
+ use SP \Repositories \NoSuchItemException ;
38
40
use SplSubject ;
39
41
40
42
/**
41
43
* Class Plugin
42
44
*
43
45
* @package SP\Modules\Web\Plugins\Authenticator
46
+ * @property AuthenticatorData $data
44
47
*/
45
48
class Plugin extends PluginBase
46
49
{
@@ -51,6 +54,14 @@ class Plugin extends PluginBase
51
54
* @var ContainerInterface
52
55
*/
53
56
private $ dic ;
57
+ /**
58
+ * @var PluginOperation
59
+ */
60
+ private $ pluginOperation ;
61
+ /**
62
+ * @var SessionContext
63
+ */
64
+ private $ session ;
54
65
55
66
/**
56
67
* Receive update from subject
@@ -75,16 +86,14 @@ public function update(SplSubject $subject)
75
86
*/
76
87
public function init (ContainerInterface $ dic )
77
88
{
78
- if (!is_array ($ this ->data )) {
79
- $ this ->data = [];
80
- }
81
-
82
89
$ this ->base = dirname (__DIR__ );
83
90
$ this ->themeDir = $ this ->base . DIRECTORY_SEPARATOR . 'themes ' . DIRECTORY_SEPARATOR . $ dic ->get (ThemeInterface::class)->getThemeName ();
84
91
85
92
$ this ->setLocales ();
86
93
87
94
$ this ->dic = $ dic ;
95
+
96
+ $ this ->session = $ this ->dic ->get (ContextInterface::class);
88
97
}
89
98
90
99
/**
@@ -103,15 +112,36 @@ public function updateEvent($eventType, Event $event)
103
112
/** @var ExtensibleTabControllerInterface $source */
104
113
$ source = $ event ->getSource (ExtensibleTabControllerInterface::class);
105
114
115
+ $ this ->loadData ();
106
116
(new PreferencesController ($ source , $ this , $ this ->dic ))
107
117
->setUp ();
108
118
break ;
109
119
case 'login.finish ' :
120
+ $ this ->loadData ();
110
121
$ this ->checkLogin ($ event );
111
122
break ;
112
123
}
113
124
}
114
125
126
+ /**
127
+ * Load plugin's data for current user
128
+ */
129
+ private function loadData ()
130
+ {
131
+ try {
132
+ $ this ->data = $ this ->pluginOperation ->get (
133
+ $ this ->session ->getUserData ()->getId (),
134
+ AuthenticatorData::class
135
+ );
136
+ } catch (NoSuchItemException $ e ) {
137
+ $ this ->data = new AuthenticatorData ();
138
+ } catch (\Exception $ e ) {
139
+ processException ($ e );
140
+
141
+ $ this ->data = new AuthenticatorData ();
142
+ }
143
+ }
144
+
115
145
/**
116
146
* Comprobar 2FA en el login
117
147
*
@@ -121,48 +151,40 @@ public function updateEvent($eventType, Event $event)
121
151
*/
122
152
private function checkLogin (Event $ event )
123
153
{
124
- $ session = $ this ->dic ->get (ContextInterface::class);
125
154
$ pluginContext = $ this ->dic ->get (PluginContext::class);
126
155
127
- $ data = $ this ->getDataForId ($ session ->getUserData ()->getId ());
128
-
129
- if ($ data !== null && $ data ->isTwofaEnabled ()) {
156
+ if ($ this ->data !== null && $ this ->data ->isTwofaEnabled ()) {
130
157
$ pluginContext ->setTwoFApass (false );
131
- $ session ->setAuthCompleted (false );
158
+ $ this -> session ->setAuthCompleted (false );
132
159
133
160
$ eventData = $ event ->getEventMessage ()->getExtra ();
134
161
135
162
if (isset ($ eventData ['redirect ' ][0 ])
136
163
&& is_callable ($ eventData ['redirect ' ][0 ])
137
164
) {
138
- $ session ->setTrasientKey ('redirect ' , $ eventData ['redirect ' ][0 ]('authenticatorLogin/index ' ));
165
+ $ this -> session ->setTrasientKey ('redirect ' , $ eventData ['redirect ' ][0 ]('authenticatorLogin/index ' ));
139
166
} else {
140
- $ session ->setTrasientKey ('redirect ' , 'index.php?r=authenticatorLogin/index ' );
167
+ $ this -> session ->setTrasientKey ('redirect ' , 'index.php?r=authenticatorLogin/index ' );
141
168
}
142
169
} else {
143
170
$ pluginContext ->setTwoFApass (true );
144
- $ session ->setAuthCompleted (true );
171
+ $ this -> session ->setAuthCompleted (true );
145
172
}
146
173
}
147
174
148
175
/**
149
- * Devolver los datos de un Id
150
- *
151
- * @param $id
152
- *
153
- * @return AuthenticatorData|null
154
- */
155
- public function getDataForId ($ id )
156
- {
157
- return isset ($ this ->data [$ id ]) ? $ this ->data [$ id ] : null ;
158
- }
159
-
160
- /**
161
- * @return array|AuthenticatorData[]
176
+ * @return AuthenticatorData
162
177
*/
163
178
public function getData ()
164
179
{
165
- return (array )parent ::getData ();
180
+ if ($ this ->data === null
181
+ && $ this ->session ->isLoggedIn ()
182
+ && $ this ->pluginOperation !== null
183
+ ) {
184
+ $ this ->loadData ();
185
+ }
186
+
187
+ return parent ::getData ();
166
188
}
167
189
168
190
/**
@@ -202,7 +224,7 @@ public function getAuthor()
202
224
*/
203
225
public function getVersion ()
204
226
{
205
- return [2 , 0 , 1 ];
227
+ return [2 , 1 , 0 ];
206
228
}
207
229
208
230
/**
@@ -212,7 +234,7 @@ public function getVersion()
212
234
*/
213
235
public function getCompatibleVersion ()
214
236
{
215
- return [3 , 0 ];
237
+ return [3 , 1 ];
216
238
}
217
239
218
240
/**
@@ -239,13 +261,15 @@ public function getName()
239
261
* Establecer los datos de un Id
240
262
*
241
263
* @param $id
242
- * @param AuthenticatorData $AuthenticatorData
264
+ * @param AuthenticatorData $authenticatorData
243
265
*
244
266
* @return Plugin
267
+ * @throws \SP\Core\Exceptions\ConstraintException
268
+ * @throws \SP\Core\Exceptions\QueryException
245
269
*/
246
- public function setDataForId ($ id , AuthenticatorData $ AuthenticatorData )
270
+ public function setDataForId ($ id , AuthenticatorData $ authenticatorData )
247
271
{
248
- $ this ->data [ $ id] = $ AuthenticatorData ;
272
+ $ this ->pluginOperation -> update ( $ id, $ authenticatorData ) ;
249
273
250
274
return $ this ;
251
275
}
@@ -254,22 +278,37 @@ public function setDataForId($id, AuthenticatorData $AuthenticatorData)
254
278
* Eliminar los datos de un Id
255
279
*
256
280
* @param $id
281
+ *
282
+ * @throws \SP\Core\Exceptions\ConstraintException
283
+ * @throws \SP\Core\Exceptions\QueryException
284
+ * @throws \SP\Repositories\NoSuchItemException
257
285
*/
258
286
public function deleteDataForId ($ id )
259
287
{
260
- if (isset ($ this ->data [$ id ])) {
261
- unset($ this ->data [$ id ]);
262
- }
288
+ $ this ->pluginOperation ->delete ($ id );
263
289
}
264
290
265
291
/**
266
- * @param mixed $pluginData
292
+ * @param mixed $pluginOperation
267
293
*/
268
- public function onLoadData ( PluginData $ pluginData )
294
+ public function onLoad ( PluginOperation $ pluginOperation )
269
295
{
270
- $ this ->data = Util::unserialize (
271
- AuthenticatorData::class,
272
- $ pluginData ->getData ()
273
- );
296
+ $ this ->pluginOperation = $ pluginOperation ;
297
+ }
298
+
299
+ /**
300
+ * @param string $version
301
+ * @param PluginOperation $pluginOperation
302
+ * @param mixed $extra
303
+ *
304
+ * @throws Services\AuthenticatorException
305
+ */
306
+ public function upgrade (string $ version , PluginOperation $ pluginOperation , $ extra = null )
307
+ {
308
+ switch ($ version ) {
309
+ case '310.19012201 ' :
310
+ (new UpgradeService ($ pluginOperation ))->upgrade_310_19012201 ($ extra );
311
+ break ;
312
+ }
274
313
}
275
314
}
0 commit comments