Skip to content

Commit 14bd6d5

Browse files
committed
* [MOD] Improved plugins handling by moving items' data to new database table.
* [MOD] Added plugins upgrade process Signed-off-by: nuxsmin <nuxsmin@syspass.org>
1 parent 32b7ded commit 14bd6d5

File tree

5 files changed

+167
-68
lines changed

5 files changed

+167
-68
lines changed

src/lib/Controllers/AuthenticatorController.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
use SP\Services\Mail\MailService;
4040
use SP\Services\Track\TrackService;
4141
use SP\Services\User\UserLoginResponse;
42-
use SP\Util\ArrayUtil;
4342

4443
/**
4544
* Class ActionController
@@ -248,13 +247,7 @@ public function checkCodeAction()
248247
$pin = $this->request->analyzeString('pin');
249248
$codeReset = $this->request->analyzeBool('code_reset', false);
250249

251-
// Buscar al usuario en los datos del plugin
252-
/** @var AuthenticatorData $authenticatorData */
253-
$authenticatorData = ArrayUtil::searchInObject(
254-
$this->plugin->getData(),
255-
'userId',
256-
$this->userData->getId()
257-
);
250+
$authenticatorData = $this->plugin->getData();
258251

259252
if ($authenticatorData === null) {
260253
$this->pluginContext->setTwoFApass(false);
@@ -360,7 +353,7 @@ private function sendResetEmail(AuthenticatorData $authenticatorData)
360353
public function showRecoveryCodesAction()
361354
{
362355
try {
363-
$authenticatorData = $this->plugin->getDataForId($this->userData->getId());
356+
$authenticatorData = $this->plugin->getData();
364357

365358
if ($authenticatorData === null) {
366359
throw new AuthenticatorException(__u('User not found'));
@@ -412,7 +405,7 @@ protected function initialize()
412405
$this->pluginContext = $this->dic->get(PluginContext::class);
413406
$this->trackService = $this->dic->get(TrackService::class);
414407
$this->plugin = $this->dic->get(PluginManager::class)
415-
->getPluginInfo(Plugin::PLUGIN_NAME);
408+
->getPlugin(Plugin::PLUGIN_NAME);
416409
$this->userData = $this->session->getUserData();
417410
$this->trackRequest = $this->trackService->getTrackRequest(__CLASS__);
418411
}

src/lib/Controllers/AuthenticatorLoginController.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function indexAction()
7070
$this->view->assign('useMenu', false);
7171
$this->view->assign('route', 'authenticator/checkCode');
7272

73-
$this->checkExpireTime($this->userData->getId());
73+
$this->checkExpireTime();
7474

7575
$this->prepareSignedUriOnView();
7676

@@ -79,12 +79,10 @@ public function indexAction()
7979

8080
/**
8181
* Comprobar la caducidad del código
82-
*
83-
* @param $userId
8482
*/
85-
protected function checkExpireTime($userId)
83+
protected function checkExpireTime()
8684
{
87-
$data = $this->plugin->getDataForId($userId);
85+
$data = $this->plugin->getData();
8886

8987
if ($data === null || empty($data->getExpireDays())) {
9088
return;
@@ -115,6 +113,6 @@ protected function checkExpireTime($userId)
115113
protected function initialize()
116114
{
117115
$this->plugin = $this->dic->get(PluginManager::class)
118-
->getPluginInfo(Plugin::PLUGIN_NAME);
116+
->getPlugin(Plugin::PLUGIN_NAME);
119117
}
120118
}

src/lib/Controllers/PreferencesController.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,12 @@
2727
use Psr\Container\ContainerInterface;
2828
use SP\Core\Context\ContextInterface;
2929
use SP\Core\Events\Event;
30-
use SP\Modules\Web\Plugins\Authenticator\Models\AuthenticatorData;
3130
use SP\Modules\Web\Plugins\Authenticator\Services\AuthenticatorService;
3231
use SP\Modules\Web\Plugins\Authenticator\Util\PluginContext;
3332
use SP\Mvc\Controller\ExtensibleTabControllerInterface;
3433
use SP\Mvc\View\Components\DataTab;
3534
use SP\Mvc\View\Template;
3635
use SP\Plugin\PluginInterface;
37-
use SP\Util\ArrayUtil;
3836
use SP\Util\ErrorUtil;
3937

4038
/**
@@ -111,14 +109,7 @@ protected function getSecurityTab()
111109
// Datos del usuario de la sesión
112110
$userData = $this->context->getUserData();
113111

114-
// Buscar al usuario en los datos del plugin
115-
/** @var AuthenticatorData $authenticatorData */
116-
$authenticatorData = ArrayUtil::searchInObject(
117-
$this->plugin->getData(),
118-
'userId',
119-
$userData->getId(),
120-
new AuthenticatorData()
121-
);
112+
$authenticatorData = $this->plugin->getData();
122113

123114
$qrCode = '';
124115

src/lib/Plugin.php

Lines changed: 81 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,24 @@
2626

2727
use Psr\Container\ContainerInterface;
2828
use SP\Core\Context\ContextInterface;
29+
use SP\Core\Context\SessionContext;
2930
use SP\Core\Events\Event;
3031
use SP\Core\UI\ThemeInterface;
31-
use SP\DataModel\PluginData;
3232
use SP\Modules\Web\Plugins\Authenticator\Controllers\PreferencesController;
3333
use SP\Modules\Web\Plugins\Authenticator\Models\AuthenticatorData;
34+
use SP\Modules\Web\Plugins\Authenticator\Services\UpgradeService;
3435
use SP\Modules\Web\Plugins\Authenticator\Util\PluginContext;
3536
use SP\Mvc\Controller\ExtensibleTabControllerInterface;
3637
use SP\Plugin\PluginBase;
37-
use SP\Util\Util;
38+
use SP\Plugin\PluginOperation;
39+
use SP\Repositories\NoSuchItemException;
3840
use SplSubject;
3941

4042
/**
4143
* Class Plugin
4244
*
4345
* @package SP\Modules\Web\Plugins\Authenticator
46+
* @property AuthenticatorData $data
4447
*/
4548
class Plugin extends PluginBase
4649
{
@@ -51,6 +54,14 @@ class Plugin extends PluginBase
5154
* @var ContainerInterface
5255
*/
5356
private $dic;
57+
/**
58+
* @var PluginOperation
59+
*/
60+
private $pluginOperation;
61+
/**
62+
* @var SessionContext
63+
*/
64+
private $session;
5465

5566
/**
5667
* Receive update from subject
@@ -75,16 +86,14 @@ public function update(SplSubject $subject)
7586
*/
7687
public function init(ContainerInterface $dic)
7788
{
78-
if (!is_array($this->data)) {
79-
$this->data = [];
80-
}
81-
8289
$this->base = dirname(__DIR__);
8390
$this->themeDir = $this->base . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR . $dic->get(ThemeInterface::class)->getThemeName();
8491

8592
$this->setLocales();
8693

8794
$this->dic = $dic;
95+
96+
$this->session = $this->dic->get(ContextInterface::class);
8897
}
8998

9099
/**
@@ -103,15 +112,36 @@ public function updateEvent($eventType, Event $event)
103112
/** @var ExtensibleTabControllerInterface $source */
104113
$source = $event->getSource(ExtensibleTabControllerInterface::class);
105114

115+
$this->loadData();
106116
(new PreferencesController($source, $this, $this->dic))
107117
->setUp();
108118
break;
109119
case 'login.finish':
120+
$this->loadData();
110121
$this->checkLogin($event);
111122
break;
112123
}
113124
}
114125

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+
115145
/**
116146
* Comprobar 2FA en el login
117147
*
@@ -121,48 +151,40 @@ public function updateEvent($eventType, Event $event)
121151
*/
122152
private function checkLogin(Event $event)
123153
{
124-
$session = $this->dic->get(ContextInterface::class);
125154
$pluginContext = $this->dic->get(PluginContext::class);
126155

127-
$data = $this->getDataForId($session->getUserData()->getId());
128-
129-
if ($data !== null && $data->isTwofaEnabled()) {
156+
if ($this->data !== null && $this->data->isTwofaEnabled()) {
130157
$pluginContext->setTwoFApass(false);
131-
$session->setAuthCompleted(false);
158+
$this->session->setAuthCompleted(false);
132159

133160
$eventData = $event->getEventMessage()->getExtra();
134161

135162
if (isset($eventData['redirect'][0])
136163
&& is_callable($eventData['redirect'][0])
137164
) {
138-
$session->setTrasientKey('redirect', $eventData['redirect'][0]('authenticatorLogin/index'));
165+
$this->session->setTrasientKey('redirect', $eventData['redirect'][0]('authenticatorLogin/index'));
139166
} else {
140-
$session->setTrasientKey('redirect', 'index.php?r=authenticatorLogin/index');
167+
$this->session->setTrasientKey('redirect', 'index.php?r=authenticatorLogin/index');
141168
}
142169
} else {
143170
$pluginContext->setTwoFApass(true);
144-
$session->setAuthCompleted(true);
171+
$this->session->setAuthCompleted(true);
145172
}
146173
}
147174

148175
/**
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
162177
*/
163178
public function getData()
164179
{
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();
166188
}
167189

168190
/**
@@ -202,7 +224,7 @@ public function getAuthor()
202224
*/
203225
public function getVersion()
204226
{
205-
return [2, 0, 1];
227+
return [2, 1, 0];
206228
}
207229

208230
/**
@@ -212,7 +234,7 @@ public function getVersion()
212234
*/
213235
public function getCompatibleVersion()
214236
{
215-
return [3, 0];
237+
return [3, 1];
216238
}
217239

218240
/**
@@ -239,13 +261,15 @@ public function getName()
239261
* Establecer los datos de un Id
240262
*
241263
* @param $id
242-
* @param AuthenticatorData $AuthenticatorData
264+
* @param AuthenticatorData $authenticatorData
243265
*
244266
* @return Plugin
267+
* @throws \SP\Core\Exceptions\ConstraintException
268+
* @throws \SP\Core\Exceptions\QueryException
245269
*/
246-
public function setDataForId($id, AuthenticatorData $AuthenticatorData)
270+
public function setDataForId($id, AuthenticatorData $authenticatorData)
247271
{
248-
$this->data[$id] = $AuthenticatorData;
272+
$this->pluginOperation->update($id, $authenticatorData);
249273

250274
return $this;
251275
}
@@ -254,22 +278,37 @@ public function setDataForId($id, AuthenticatorData $AuthenticatorData)
254278
* Eliminar los datos de un Id
255279
*
256280
* @param $id
281+
*
282+
* @throws \SP\Core\Exceptions\ConstraintException
283+
* @throws \SP\Core\Exceptions\QueryException
284+
* @throws \SP\Repositories\NoSuchItemException
257285
*/
258286
public function deleteDataForId($id)
259287
{
260-
if (isset($this->data[$id])) {
261-
unset($this->data[$id]);
262-
}
288+
$this->pluginOperation->delete($id);
263289
}
264290

265291
/**
266-
* @param mixed $pluginData
292+
* @param mixed $pluginOperation
267293
*/
268-
public function onLoadData(PluginData $pluginData)
294+
public function onLoad(PluginOperation $pluginOperation)
269295
{
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+
}
274313
}
275314
}

0 commit comments

Comments
 (0)