-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathps_accounts.php
481 lines (425 loc) · 14.4 KB
/
ps_accounts.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <[email protected]>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/
if (!defined('_PS_VERSION_')) {
exit;
}
require_once __DIR__ . '/vendor/autoload.php';
if (!class_exists('\PrestaShop\Module\PsAccounts\Hook\HookableTrait')) {
ps_accounts_fix_upgrade();
}
class Ps_accounts extends Module
{
use \PrestaShop\Module\PsAccounts\Hook\HookableTrait;
const DEFAULT_ENV = '';
// Needed in order to retrieve the module version easier (in api call headers) than instanciate
// the module each time to get the version
const VERSION = '7.1.0';
/**
* Admin tabs
*
* @var array class names
*/
private $adminControllers = [
'AdminAjaxPsAccountsController',
'AdminDebugPsAccountsController',
'AdminOAuth2PsAccountsController',
'AdminLoginPsAccountsController',
];
/**
* Hooks exposed by the module
*
* @var array
*/
private $customHooks = [
[
'name' => 'displayAccountUpdateWarning',
'title' => 'Display account update warning',
'description' => 'Show a warning message when the user wants to'
. ' update his shop configuration',
'position' => 1,
],
[
'name' => 'actionShopAccountLinkAfter',
'title' => 'Shop linked event',
'description' => 'Shop linked with PrestaShop Account',
'position' => 1,
],
[
'name' => 'actionShopAccountUnlinkAfter',
'title' => 'Shop unlinked event',
'description' => 'Shop unlinked with PrestaShop Account',
'position' => 1,
],
[
'name' => 'actionShopAccessTokenRefreshAfter',
'title' => 'Shop access token refreshed event',
'description' => 'Shop access token refreshed event',
'position' => 1,
],
];
/**
* Hooks to register
*
* @var array hook or class names
*/
private $hooks = [
//\PrestaShop\Module\PsAccounts\Hook\ActionAdminLoginControllerLoginAfter::class,
'actionAdminLoginControllerLoginAfter',
'actionObjectEmployeeDeleteAfter',
'actionObjectShopAddAfter',
'actionObjectShopDeleteAfter',
'actionObjectShopDeleteBefore',
'actionObjectShopUpdateAfter',
'actionObjectShopUrlUpdateAfter',
'actionShopAccountLinkAfter',
'actionShopAccountUnlinkAfter',
'displayAccountUpdateWarning',
'displayBackOfficeEmployeeMenu',
'displayDashboardTop',
// toggle single/multi-shop
//'actionObjectShopAddAfter',
//'actionObjectShopDeleteAfter',
// Login/Logout OAuth
// PS 1.6 - 1.7
'displayBackOfficeHeader',
'actionAdminLoginControllerSetMedia',
// PS >= 8
//'actionAdminControllerInitBefore',
];
/**
* @var \PrestaShop\Module\PsAccounts\ServiceContainer\PsAccountsContainer
*/
private $moduleContainer;
/**
* Ps_accounts constructor.
*/
public function __construct()
{
$this->name = 'ps_accounts';
$this->tab = 'administration';
$this->author = 'PrestaShop';
$this->need_instance = 0;
$this->bootstrap = false;
// We cannot use the const VERSION because the const is not computed by addons marketplace
// when the zip is uploaded
$this->version = '7.1.0';
$this->module_key = 'abf2cd758b4d629b2944d3922ef9db73';
parent::__construct();
$this->displayName = $this->l(
'PrestaShop Account'
);
$this->description = $this->l(
'Link your store to your PrestaShop account to activate and manage your subscriptions in your ' .
'back office. Do not uninstall this module if you have a current subscription.'
);
$this->description_full = $this->l(
'Link your store to your PrestaShop account to activate and manage your subscriptions in your ' .
'back office. Do not uninstall this module if you have a current subscription.'
);
$this->confirmUninstall = $this->l(
'This action will prevent immediately your PrestaShop services and Community services from ' .
'working as they are using PrestaShop Accounts module for authentication.'
);
$this->ps_versions_compliancy = ['min' => '1.6.1', 'max' => _PS_VERSION_];
}
/**
* @return \PrestaShop\Module\PsAccounts\Vendor\Monolog\Logger
*/
public function getLogger()
{
return $this->getService('ps_accounts.logger');
}
/**
* @return \Context
*/
public function getContext()
{
return $this->context;
}
/**
* @return bool
*
* @throws PrestaShopDatabaseException
* @throws PrestaShopException
* @throws Exception
*/
public function install()
{
$installer = new PrestaShop\Module\PsAccounts\Module\Install($this, Db::getInstance());
$status = $installer->installInMenu()
&& $installer->installDatabaseTables()
&& parent::install()
&& $this->addCustomHooks($this->customHooks)
&& $this->registerHook($this->getHooksToRegister());
$this->onModuleReset();
return $status;
}
/**
* @return bool
*
* @throws PrestaShopDatabaseException
* @throws PrestaShopException
*/
public function uninstall()
{
$uninstaller = new PrestaShop\Module\PsAccounts\Module\Uninstall($this, Db::getInstance());
return $uninstaller->uninstallMenu()
&& $uninstaller->uninstallDatabaseTables()
&& parent::uninstall();
}
/**
* @phpstan-ignore-next-line
*
* @return \PrestaShop\PrestaShop\Adapter\SymfonyContainer|\Symfony\Component\DependencyInjection\ContainerInterface|null
*/
public function getCoreServiceContainer()
{
/* @phpstan-ignore-next-line */
if (method_exists($this, 'getContainer')) {
return $this->getContainer();
}
if (class_exists('\PrestaShop\PrestaShop\Adapter\SymfonyContainer')) {
return \PrestaShop\PrestaShop\Adapter\SymfonyContainer::getInstance();
}
return null;
}
/**
* @return \PrestaShop\Module\PsAccounts\ServiceContainer\PsAccountsContainer
*
* @throws Exception
*/
public function getServiceContainer()
{
if (null === $this->moduleContainer) {
$this->moduleContainer = (new \PrestaShop\Module\PsAccounts\ServiceContainer\PsAccountsContainer(
__DIR__ . '/config.php'
))->init();
}
return $this->moduleContainer;
}
/**
* @param string $serviceName
*
* @return mixed
*/
public function getService($serviceName)
{
return $this->getServiceContainer()->getService($serviceName);
}
/**
* @param string $serviceName
*
* @return bool
*/
public function hasService($serviceName)
{
return $this->getServiceContainer()->has($serviceName);
}
/**
* @param string $name
*
* @return mixed
*/
public function getParameter($name)
{
return $this->getServiceContainer()->getParameter($name);
}
/**
* @param string $name
*
* @return bool
*/
public function hasParameter($name)
{
return $this->getServiceContainer()->hasParameter($name);
}
/**
* @return array
*/
public function getAdminControllers()
{
return array_map(function ($className) {
return preg_replace('/^.*?(\w+)Controller$/', '\1', $className);
//return preg_replace('/^(.*?)Controller$/', '\1', $className);
}, $this->adminControllers);
}
/**
* @return array
*/
public function getHooksToRegister()
{
return array_map(function ($className) {
return is_a($className, '\PrestaShop\Module\PsAccounts\Hook\Hook', true) ?
$className::getName() : $className;
}, $this->hooks);
}
/**
* @return array
*/
public function getCustomHooks()
{
return $this->customHooks;
}
/**
* @param array $customHooks
*
* @return bool
*/
public function addCustomHooks($customHooks)
{
$ret = true;
foreach ($customHooks as $customHook) {
try {
$verify = true;
if ((bool) Hook::getIdByName($customHook['name']) === false) {
$hook = new Hook();
$hook->name = $customHook['name'];
$hook->title = $customHook['title'];
$hook->description = $customHook['description'];
$hook->position = $customHook['position'];
$verify = $hook->add(); // return true on success
}
$ret = $ret && $verify;
} catch (\Throwable $e) {
/* @phpstan-ignore-next-line */
} catch (\Exception $e) {
}
}
return $ret;
}
/**
* Render the configuration form.
*
* @return string
*
* @throws PrestaShopException
*/
public function getContent()
{
//$this->context->smarty->assign('pathVendor', $this->_path . 'views/js/chunk-vendors.' . $this->version . '.js');
$this->context->smarty->assign('pathApp', $this->_path . 'views/js/app.' . $this->version . '.js');
$this->context->smarty->assign('pathAppAssets', $this->_path . 'views/css/app.' . $this->version . '.css');
$this->context->smarty->assign('urlAccountsCdn', $this->getParameter('ps_accounts.accounts_cdn_url'));
$storePresenter = new PrestaShop\Module\PsAccounts\Presenter\Store\StorePresenter($this, $this->context);
Media::addJsDef([
'storePsAccounts' => $storePresenter->present(),
]);
/** @var \PrestaShop\Module\PsAccounts\Presenter\PsAccountsPresenter $psAccountsPresenter */
$psAccountsPresenter = $this->getService(\PrestaShop\Module\PsAccounts\Presenter\PsAccountsPresenter::class);
Media::addJsDef([
'contextPsAccounts' => $psAccountsPresenter->present((string) $this->name),
]);
return $this->display(__FILE__, 'views/templates/admin/app.tpl');
}
/**
* @return string
*/
public function getSsoAccountUrl()
{
$url = $this->getParameter('ps_accounts.sso_account_url');
$langIsoCode = $this->getContext()->language->iso_code;
return $url . '?lang=' . substr($langIsoCode, 0, 2);
}
/**
* @return \PrestaShop\Module\PsAccounts\Context\ShopContext
*/
public function getShopContext()
{
return $this->getService(\PrestaShop\Module\PsAccounts\Context\ShopContext::class);
}
/**
* @return \PrestaShop\Module\PsAccounts\Middleware\Oauth2Middleware
*/
public function getOauth2Middleware()
{
return $this->getService(\PrestaShop\Module\PsAccounts\Middleware\Oauth2Middleware::class);
}
/**
* @return bool
*/
public function isShopEdition()
{
return Module::isEnabled('smb_edition');
}
/**
* @return \Symfony\Component\HttpFoundation\Session\SessionInterface
*
* @throws Exception
*/
public function getSession()
{
$container = $this->getCoreServiceContainer();
if ($container) {
try {
/**
* @var \Symfony\Component\HttpFoundation\Session\SessionInterface $session
* @phpstan-ignore-next-line
*/
$session = $container->get('session');
/* @phpstan-ignore-next-line */
} catch (\Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException $e) {
try {
// FIXME: fix for 1.7.7.x
global $kernel;
$session = $kernel->getContainer()->get('session');
/* @phpstan-ignore-next-line */
} catch (\Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException $e) {
// FIXME: fix for 9.x
global $request;
$session = $request->getSession();
}
}
return $session;
}
throw new \Exception('Feature not available');
}
/**
* @return void
*
* @throws Exception
*/
public function onModuleReset()
{
/** @var \PrestaShop\Module\PsAccounts\Factory\CircuitBreakerFactory $circuitBreakerFactory */
$circuitBreakerFactory = $this->getService(\PrestaShop\Module\PsAccounts\Factory\CircuitBreakerFactory::class);
$circuitBreakerFactory->resetAll();
/** @var \PrestaShop\Module\PsAccounts\Repository\ConfigurationRepository $configurationRepository */
$configurationRepository = $this->getService(\PrestaShop\Module\PsAccounts\Repository\ConfigurationRepository::class);
$configurationRepository->fixMultiShopConfig();
// FIXME: this wont prevent from re-implanting override on reset of module
$uninstaller = new PrestaShop\Module\PsAccounts\Module\Uninstall($this, Db::getInstance());
$uninstaller->deleteAdminTab('AdminLogin');
}
}
/**
* @return void
*/
function ps_accounts_fix_upgrade()
{
$root = __DIR__;
$requires = array_merge([
$root . '/src/Module/Install.php',
// $root . '/src/Hook/Hook.php',
$root . '/src/Hook/HookableTrait.php',
], []/*, glob($root . '/src/Hook/*.php')*/);
foreach ($requires as $filename) {
require_once $filename;
}
}