Skip to content

Commit

Permalink
Merge pull request #222 from PrestaShopCorp/feat/multishop
Browse files Browse the repository at this point in the history
Multishop + responsive of backoffice
  • Loading branch information
hschoenenberger authored Oct 27, 2021
2 parents 1ed4614 + 31731b3 commit 7f5c1e2
Show file tree
Hide file tree
Showing 17 changed files with 185 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/accounts-qc-php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ jobs:
id: neon
run: |
PS_VERSION="${{ matrix.presta-versions }}"
[[ "${PS_VERSION:0:3}" != '1.7' ]] && echo ::set-output name=filename::phpstan-PS-1.6.neon || echo ::set-output name=filename::phpstan-PS-1.7.neon
[[ "${PS_VERSION:0:3}" != '1.6' ]] && echo ::set-output name=filename::phpstan-PS-1.7.neon || echo ::set-output name=filename::phpstan-PS-1.6.neon
- name: PHPStan PrestaShop ${{ matrix.presta-versions }}
run: |
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ endif
--workdir=/web/module \
phpstan/phpstan:${PHPSTAN_VERSION} analyse \
--configuration=/web/module/tests/phpstan/${NEON_FILE}
docker volume rm ps-volume


# target: phpunit - Start phpunit
Expand Down
2 changes: 1 addition & 1 deletion _dev/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"moment": "^2.26.0",
"moment-range": "^4.0.2",
"prestakit": "^1.1.0",
"prestashop_accounts_vue_components": "^2.0.0",
"prestashop_accounts_vue_components": "^3.0.0",
"regenerator-runtime": "^0.13.5",
"tailwindcss": "^1.8.10",
"tween.js": "^16.6.0",
Expand Down
12 changes: 9 additions & 3 deletions _dev/src/core/settings/pages/OnBoardingApp.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* International Registered Trademark & Property of PrestaShop SA
*-->
<template>
<div class="onboarding pt-5">
<div class="pt-5 onboarding">
<section class="onboarding-header">
<ConfigInformation :app="app" />
</section>
Expand Down Expand Up @@ -99,10 +99,16 @@ export default {

<style lang="scss" scoped>
.onboarding {
@apply max-w-6xl mx-auto #{!important};
@apply max-w-screen-lg mx-auto #{!important};

&-header {
@apply mb-4 #{!important};
@apply mb-2 #{!important};
}

@screen md {
&-header {
@apply mb-4 #{!important};
}
}
}
</style>
9 changes: 8 additions & 1 deletion _dev/src/core/settings/pages/SettingsApp.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<div id="settingsApp">
<Tabs :vertical="vertical">
<Tab :label="$t('general.settings')" to="/settings/onboarding" />
<!-- <Tab :label="$t('general.help')" to="/settings/help" />-->
<!-- <Tab :label="$t('general.help')" to="/settings/help" />-->
</Tabs>
</div>
</template>
Expand Down Expand Up @@ -61,8 +61,15 @@ export default {
text-align: left;

.tab-content {
padding: 0;
background: transparent;
}

@media only screen and (max-width: 768px) {
.tab-content {
padding: 1rem;
}
}
}

#settingsApp .card-header,
Expand Down
2 changes: 1 addition & 1 deletion _dev/vue.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module.exports = {
extract: false,
},
runtimeCompiler: true,
productionSourceMap: false,
// productionSourceMap: false,
filenameHashing: false,
outputDir: "../views/",
assetsDir: "",
Expand Down
43 changes: 43 additions & 0 deletions classes/Context/ShopContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

use Context;
use PrestaShop\Module\PsAccounts\Repository\ConfigurationRepository;
use PrestaShop\Module\PsAccounts\Repository\UserTokenRepository;

/**
* Get the shop context
Expand All @@ -33,6 +34,11 @@ class ShopContext
*/
private $configuration;

/**
* @var UserTokenRepository
*/
private $userTokenRepository;

/**
* @var Context
*/
Expand All @@ -42,13 +48,16 @@ class ShopContext
* ShopContext constructor.
*
* @param ConfigurationRepository $configuration
* @param UserTokenRepository $userTokenRepository
* @param Context $context
*/
public function __construct(
ConfigurationRepository $configuration,
UserTokenRepository $userTokenRepository,
Context $context
) {
$this->configuration = $configuration;
$this->userTokenRepository = $userTokenRepository;
$this->context = $context;
}

Expand All @@ -68,6 +77,32 @@ public function isShop173()
return version_compare(_PS_VERSION_, '1.7.3.0', '>=');
}

/**
* @return int
*/
public function getShopContext()
{
return \Shop::getContext();
}

/**
* ID of shop or group
*
* @return int|null
*/
public function getShopContextId()
{
if (\Shop::getContext() == \Shop::CONTEXT_SHOP) {
return \Shop::getContextShopID();
}

if (\Shop::getContext() == \Shop::CONTEXT_GROUP) {
return \Shop::getContextShopGroupID();
}

return null;
}

/**
* @return bool
*/
Expand Down Expand Up @@ -123,4 +158,12 @@ public function getConfiguration()
{
return $this->configuration;
}

/**
* @return UserTokenRepository
*/
public function getUserToken()
{
return $this->userTokenRepository;
}
}
1 change: 1 addition & 0 deletions classes/Module/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public function installInMenu()
$tab->class_name = $controllerName;
$tab->name = [];

/** @var array $lang */
foreach (\Language::getLanguages(true) as $lang) {
$tab->name[$lang['id_lang']] = $this->module->displayName . ' (' . $k . ')';
}
Expand Down
19 changes: 13 additions & 6 deletions classes/Presenter/PsAccountsPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,20 +98,27 @@ public function __construct(
*/
public function present($psxName = 'ps_accounts')
{
$this->shopLinkAccountService->prepareLinkAccount();

$shopContext = $this->shopProvider->getShopContext();

$moduleName = $this->module->name;

$currentShop = $this->shopProvider->getCurrentShop($psxName);
$shopBase64 = base64_encode((string) json_encode($currentShop));
$unlinkedShops = $this->shopProvider->getUnlinkedShops(
$psxName,
$shopContext->getContext()->employee->id
);
$shopBase64 = base64_encode(
(string) json_encode(array_values($unlinkedShops))
);
$onboardingLink = $this->module->getParameter('ps_accounts.accounts_ui_url')
. '?shopPayload=' . $shopBase64;
. '?shops=' . $shopBase64;

try {
return array_merge(
[
'currentContext' => [
'type' => $shopContext->getShopContext(),
'id' => $shopContext->getShopContextId(),
],
'psxName' => $psxName,
'psIs17' => $shopContext->isShop17(),

Expand Down Expand Up @@ -142,7 +149,7 @@ public function present($psxName = 'ps_accounts')
'employeeId' => $shopContext->getContext()->employee->id,
'isSuperAdmin' => $shopContext->getContext()->employee->isSuperAdmin(),
],
'currentShop' => $currentShop,
'currentShop' => $this->shopProvider->getCurrentShop($psxName),
'isShopContext' => $shopContext->isShopContext(),
'superAdminEmail' => $this->psAccountsService->getSuperAdminEmail(),

Expand Down
25 changes: 20 additions & 5 deletions classes/Provider/RsaKeysProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,25 @@ public function generateKeys($refresh = false)
}
}

/**
* @return string|bool|null
*/
public function getOrGenerateAccountsRsaPublicKey()
{
$publicKey = $this->getPublicKey();
if ($publicKey) {
return $publicKey;
}

try {
$this->regenerateKeys();

return $this->getPublicKey();
} catch (\Exception $e) {
return null;
}
}

/**
* @return void
*
Expand All @@ -154,11 +173,7 @@ public function regenerateKeys()
*/
public function hasKeys()
{
return false === (
empty($this->configuration->getAccountsRsaPublicKey())
|| empty($this->configuration->getAccountsRsaPrivateKey())
|| empty($this->configuration->getAccountsRsaSignData())
);
return false === empty($this->configuration->getAccountsRsaPublicKey());
}

/**
Expand Down
67 changes: 63 additions & 4 deletions classes/Provider/ShopProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public function __construct(
public function formatShopData($shopData, $psxName = '')
{
$configuration = $this->shopContext->getConfiguration();
$userToken = $this->shopContext->getUserToken();

$shopId = $configuration->getShopId();

Expand All @@ -72,6 +73,9 @@ public function formatShopData($shopData, $psxName = '')
/** @var ShopLinkAccountService $shopLinkAccountService */
$shopLinkAccountService = $module->getService(ShopLinkAccountService::class);

/** @var RsaKeysProvider $rsaKeyProvider */
$rsaKeyProvider = $module->getService(RsaKeysProvider::class);

$data = [
'id' => (string) $shopData['id_shop'],
'name' => $shopData['name'],
Expand All @@ -81,8 +85,13 @@ public function formatShopData($shopData, $psxName = '')

// LinkAccount
'uuid' => $configuration->getShopUuid() ?: null,
'publicKey' => $configuration->getAccountsRsaPublicKey() ?: null,
'publicKey' => $rsaKeyProvider->getOrGenerateAccountsRsaPublicKey() ?: null,
'employeeId' => (int) $configuration->getEmployeeId() ?: null,
'user' => [
'email' => $userToken->getTokenEmail() ?: null,
'uuid' => $userToken->getTokenUuid() ?: null,
'emailIsValidated' => $userToken->getTokenEmailVerified(),
],

'url' => $this->link->getAdminLink(
'AdminModules',
Expand Down Expand Up @@ -112,7 +121,7 @@ public function formatShopData($shopData, $psxName = '')
*/
public function getCurrentShop($psxName = '')
{
$data = $this->formatShopData(\Shop::getShop($this->shopContext->getContext()->shop->id), $psxName);
$data = $this->formatShopData((array) \Shop::getShop($this->shopContext->getContext()->shop->id), $psxName);

return array_merge($data, [
'multishop' => $this->shopContext->isMultishopActive(),
Expand All @@ -135,7 +144,13 @@ public function getShopsTree($psxName)
foreach (\Shop::getTree() as $groupId => $groupData) {
$shops = [];
foreach ($groupData['shops'] as $shopId => $shopData) {
$shops[] = $this->formatShopData($shopData, $psxName);
$data = $this->formatShopData((array) $shopData, $psxName);

$shops[] = array_merge($data, [
'multishop' => $this->shopContext->isMultishopActive(),
'moduleName' => $psxName,
'psVersion' => _PS_VERSION_,
]);
}

$shopList[] = [
Expand All @@ -151,6 +166,50 @@ public function getShopsTree($psxName)
return $shopList;
}

/**
* @param string $psxName
* @param int $employeeId
*
* @return array
*
* @throws \PrestaShopException
*/
public function getUnlinkedShops($psxName, $employeeId)
{
$shopTree = $this->getShopsTree($psxName);
$shops = [];

switch ($this->getShopContext()->getShopContext()) {
case \Shop::CONTEXT_ALL:
$shops = array_reduce($shopTree, function ($carry, $shopGroup) {
return array_merge($carry, $shopGroup['shops']);
}, []);
break;
case \Shop::CONTEXT_GROUP:
$shops = array_reduce($shopTree, function ($carry, $shopGroup) {
if ($shopGroup['id'] != $this->getShopContext()->getShopContextId()) {
return $carry;
}

return array_merge($carry, $shopGroup['shops']);
}, []);
break;
case \Shop::CONTEXT_SHOP:
$shops = [$this->getCurrentShop($psxName)];
break;
}

$unlinkedShops = array_filter($shops, function ($shop) {
return $shop['uuid'] === null || ($shop['uuid'] && $shop['isLinkedV4']);
});

return array_map(function ($shop) use ($employeeId) {
$shop['employeeId'] = (string) $employeeId;

return $shop;
}, $unlinkedShops);
}

/**
* @return ShopContext
*/
Expand All @@ -162,7 +221,7 @@ public function getShopContext()
/**
* @param int $shopId
*
* @return false|string|null
* @return false|string
*/
private function getShopPhysicalUri($shopId)
{
Expand Down
4 changes: 4 additions & 0 deletions classes/Service/ShopLinkAccountService.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ public function resetLinkAccount()
$this->shopTokenRepository->cleanupCredentials();
$this->userTokenRepository->cleanupCredentials();
$this->configuration->updateEmployeeId('');
try {
$this->rsaKeysProvider->generateKeys();
} catch (\Exception $e) {
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<module>
<name>ps_accounts</name>
<displayName><![CDATA[PrestaShop Account]]></displayName>
<version><![CDATA[5.0.4]]></version>
<version><![CDATA[5.1.0]]></version>
<description><![CDATA[Link your PrestaShop account to your online shop to activate &amp; manage services on your back-office. Don&#039;t uninstall this module if you are already using a service, as it will prevent it from working.]]></description>
<author><![CDATA[PrestaShop]]></author>
<tab><![CDATA[administration]]></tab>
Expand Down
Loading

0 comments on commit 7f5c1e2

Please sign in to comment.