Skip to content

Commit

Permalink
[shopsys] admin can limit managed domains (#3289)
Browse files Browse the repository at this point in the history
  • Loading branch information
grossmannmartin authored Nov 6, 2024
2 parents f6bebbc + a92d3ed commit d88270e
Show file tree
Hide file tree
Showing 86 changed files with 911 additions and 220 deletions.
27 changes: 27 additions & 0 deletions assets/js/admin/components/SelectAdminDomains.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import Register from '../../common/utils/Register';

export default class SelectAdminDomains {

constructor ($selectDomainsDropdown) {
this.$selectDomainsDropdown = $selectDomainsDropdown;
this.$selectDomainsDropdown.find('.js-domains-select-all').on('click', () => this.selectAll());
this.$selectDomainsDropdown.find('.js-domains-select-none').on('click', () => this.selectNone());
}

static init () {
$('.js-domains-select').each(function () {
// eslint-disable-next-line no-new
new SelectAdminDomains($(this));
});
}

selectAll () {
this.$selectDomainsDropdown.find('input[type=checkbox]').prop('checked', true);
}

selectNone () {
this.$selectDomainsDropdown.find('input[type=checkbox]').prop('checked', false);
}
}

(new Register()).registerCallback(SelectAdminDomains.init, 'SelectAdminDomains.init');
12 changes: 10 additions & 2 deletions assets/js/admin/components/ToggleMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,25 @@ export default class ToggleMenu {
this.$items = $toggleMenu.filterAllNodes('.js-toggle-menu-item');

this.$items.click(function (event) {
const isOpened = $(this).hasClass('open');

ToggleMenu.hideAllSubmenus(_this);

$(this).filterAllNodes('.js-toggle-menu-submenu').show();
$(this).addClass('open');
if (!isOpened) {
$(this).filterAllNodes('.js-toggle-menu-submenu').show();
$(this).addClass('open');
}

event.stopPropagation();
});

$(document).on('click', function () {
ToggleMenu.hideAllSubmenus(_this);
});

this.$items.find('.js-toggle-menu-submenu').on('click', function (event) {
event.stopPropagation();
});
}

static hideAllSubmenus (toggleMenu) {
Expand Down
1 change: 1 addition & 0 deletions assets/js/admin/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import './PromoCodeGroup';
import './PromoCodeLimits';
import './RoleGroups';
import './initSelect2';
import './SelectAdminDomains';
import './SelectToggle';
import './SideMenu';
import './SortableValues';
Expand Down
4 changes: 2 additions & 2 deletions assets/js/admin/utils/Window.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ export default class Window {
}

moveToCenter () {
let relativeY = $(window).height() / 2 - this.$window.height() / 2;
let minRelativeY = $(window).height() * 0.1;
let relativeY = window.innerHeight / 2 - this.$window.height() / 2;
let minRelativeY = window.innerHeight * 0.1;

if (relativeY < minRelativeY) {
relativeY = minRelativeY;
Expand Down
59 changes: 45 additions & 14 deletions assets/styles/admin/todo.less
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,73 @@
*/

.list-files__item .form-line__side {
margin-left: 0;
margin-left: 0;
}

.table-fixed {
table-layout: fixed;
table-layout: fixed;
}

.edit-uuid {
font-size: 45%;
color: grey;
margin-left: 1em;
font-size: 45%;
color: grey;
margin-left: 1em;
}

.opening-hours-widget {
display: flex;
align-items: baseline;
.form-line {
padding-top: 0 !important;
padding-bottom: 0 !important;
}
display: flex;
align-items: baseline;

.form-line {
padding-top: 0 !important;
padding-bottom: 0 !important;
}
}

.js-promo-code-group-unpack {
float: right;
float: right;
}

.multi-column-list {
.form-choice {
padding: 10px 10px 10px 18px;
}

display: grid;
grid-template-columns: repeat(5, 1fr);
grid-gap: 0;
background-color: white;
padding: 0;
}

.multi-column-list .box-dropdown__options__item {
&:first-child {
border-top: 1px solid @bg-page;
}
box-sizing: border-box;
break-inside: avoid;
}

.box-dropdown__options__item label {
color: @color-base;
}

.table-form-cell-short {
width: 335px;
width: 335px;
}

.administrator_customer_list .table-grid__cell--actions {
width: 130px;
}

.form-bg-color {
background-color: @color-f;
background-color: @color-f;
}

.select-domain-buttons {
background-color: @color-f;
padding-bottom: 1em;
padding-right: 1em;
}

.table-col-200px{
Expand Down
8 changes: 6 additions & 2 deletions src/Component/Domain/AdminDomainFilterTabsFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class AdminDomainFilterTabsFacade
{
protected const SESSION_PREFIX = 'admin_domain_filter_tabs_';
protected const string SESSION_PREFIX = 'admin_domain_filter_tabs_';

/**
* @param \Symfony\Component\HttpFoundation\RequestStack $requestStack
Expand Down Expand Up @@ -54,9 +54,13 @@ public function getSelectedDomainConfig(string $namespace): ?DomainConfig
return null;
}

if (!in_array($domainId, $this->domain->getAdminEnabledDomainIds(), true)) {
throw new InvalidDomainIdException();
}

return $this->domain->getDomainConfigById($domainId);
} catch (InvalidDomainIdException|SessionNotFoundException) {
$this->requestStack->getSession()->set($this->getSessionKey($namespace), null);
$this->setSelectedDomainId($namespace, null);

return null;
}
Expand Down
15 changes: 11 additions & 4 deletions src/Component/Domain/AdminDomainTabsFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class AdminDomainTabsFacade
{
protected const SESSION_SELECTED_DOMAIN = 'selected_domain_id';
protected const string SESSION_SELECTED_DOMAIN = 'selected_domain_id';

/**
* @param \Shopsys\FrameworkBundle\Component\Domain\Domain $domain
Expand Down Expand Up @@ -48,11 +48,18 @@ public function getSelectedDomainConfig(): DomainConfig
try {
$domainId = $this->requestStack->getSession()->get(static::SESSION_SELECTED_DOMAIN);

if (!in_array($domainId, $this->domain->getAdminEnabledDomainIds(), true)) {
throw new InvalidDomainIdException();
}

return $this->domain->getDomainConfigById($domainId);
} catch (InvalidDomainIdException | SessionNotFoundException) {
$allDomains = $this->domain->getAll();
} catch (InvalidDomainIdException|SessionNotFoundException) {
$allowedDomainIds = $this->domain->getAdminEnabledDomainIds();
$firstAllowedDomainId = reset($allowedDomainIds);

$this->setSelectedDomainId($firstAllowedDomainId);

return reset($allDomains);
return $this->domain->getDomainConfigById($firstAllowedDomainId);
}
}
}
41 changes: 39 additions & 2 deletions src/Component/Domain/Domain.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Shopsys\FrameworkBundle\Component\Domain\Exception\UnableToResolveDomainException;
use Shopsys\FrameworkBundle\Component\Setting\Exception\SettingValueNotFoundException;
use Shopsys\FrameworkBundle\Component\Setting\Setting;
use Shopsys\FrameworkBundle\Model\Administrator\AdministratorFacade;
use Symfony\Component\HttpFoundation\Request;

class Domain implements DomainIdsProviderInterface
Expand All @@ -26,10 +27,12 @@ class Domain implements DomainIdsProviderInterface
/**
* @param \Shopsys\FrameworkBundle\Component\Domain\Config\DomainConfig[] $domainConfigs
* @param \Shopsys\FrameworkBundle\Component\Setting\Setting $setting
* @param \Shopsys\FrameworkBundle\Model\Administrator\AdministratorFacade $administratorFacade
*/
public function __construct(
protected array $domainConfigs,
protected Setting $setting,
protected readonly array $domainConfigs,
protected readonly Setting $setting,
protected readonly AdministratorFacade $administratorFacade,
) {
}

Expand Down Expand Up @@ -88,6 +91,8 @@ public function getAll()
{
$domainConfigsWithDataCreated = [];

$this->setting->initAllDomainsSettings();

foreach ($this->domainConfigs as $domainConfig) {
$domainId = $domainConfig->getId();

Expand Down Expand Up @@ -229,4 +234,36 @@ public function findFirstB2bDomain(): ?DomainConfig

return null;
}

/**
* @return int[]
*/
public function getAdminEnabledDomainIds(): array
{
$selectedDomainIds = $this->administratorFacade->getCurrentlyLoggedAdministrator()->getDisplayOnlyDomainIds();

return count($selectedDomainIds) > 0 ? $selectedDomainIds : $this->getAllIds();
}

/**
* @return \Shopsys\FrameworkBundle\Component\Domain\Config\DomainConfig[]
*/
public function getAdminEnabledDomains(): array
{
$domains = [];

foreach ($this->getAdminEnabledDomainIds() as $selectedDomainId) {
$domains[$selectedDomainId] = $this->getDomainConfigById($selectedDomainId);
}

return $domains;
}

/**
* @return bool
*/
public function hasAdminAllDomainsEnabled(): bool
{
return count($this->getAdminEnabledDomainIds()) === count($this->getAllIds());
}
}
5 changes: 4 additions & 1 deletion src/Component/Domain/DomainFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@

use Shopsys\FrameworkBundle\Component\Domain\Config\DomainsConfigLoader;
use Shopsys\FrameworkBundle\Component\Setting\Setting;
use Shopsys\FrameworkBundle\Model\Administrator\AdministratorFacade;

class DomainFactory
{
/**
* @param \Shopsys\FrameworkBundle\Component\Domain\Config\DomainsConfigLoader $domainsConfigLoader
* @param \Shopsys\FrameworkBundle\Component\Setting\Setting $setting
* @param \Shopsys\FrameworkBundle\Model\Administrator\AdministratorFacade $administratorFacade
*/
public function __construct(
protected readonly DomainsConfigLoader $domainsConfigLoader,
protected readonly Setting $setting,
protected readonly AdministratorFacade $administratorFacade,
) {
}

Expand All @@ -30,7 +33,7 @@ public function create($domainsConfigFilepath, $domainsUrlsConfigFilepath)
$domainsConfigFilepath,
$domainsUrlsConfigFilepath,
);
$domain = new Domain($domainConfigs, $this->setting);
$domain = new Domain($domainConfigs, $this->setting, $this->administratorFacade);

$domainId = getenv('DOMAIN');

Expand Down
5 changes: 4 additions & 1 deletion src/Component/Domain/DomainFactoryOverwritingDomainUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Shopsys\FrameworkBundle\Component\Domain\Config\DomainConfig;
use Shopsys\FrameworkBundle\Component\Domain\Config\DomainsConfigLoader;
use Shopsys\FrameworkBundle\Component\Setting\Setting;
use Shopsys\FrameworkBundle\Model\Administrator\AdministratorFacade;

class DomainFactoryOverwritingDomainUrl
{
Expand All @@ -16,11 +17,13 @@ class DomainFactoryOverwritingDomainUrl
* @param string|null $overwriteDomainUrl
* @param \Shopsys\FrameworkBundle\Component\Domain\Config\DomainsConfigLoader $domainsConfigLoader
* @param \Shopsys\FrameworkBundle\Component\Setting\Setting $setting
* @param \Shopsys\FrameworkBundle\Model\Administrator\AdministratorFacade $administratorFacade
*/
public function __construct(
$overwriteDomainUrl,
protected readonly DomainsConfigLoader $domainsConfigLoader,
protected readonly Setting $setting,
protected readonly AdministratorFacade $administratorFacade,
) {
$this->overwriteDomainUrl = $overwriteDomainUrl;
}
Expand All @@ -41,7 +44,7 @@ public function create($domainsConfigFilepath, $domainsUrlsConfigFilepath)
$domainConfigs = $this->overwriteDomainUrl($domainConfigs);
}

$domain = new Domain($domainConfigs, $this->setting);
$domain = new Domain($domainConfigs, $this->setting, $this->administratorFacade);

$domainId = getenv('DOMAIN');

Expand Down
11 changes: 11 additions & 0 deletions src/Component/Router/FriendlyUrl/FriendlyUrlFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,17 @@ public function getAllByRouteNameAndEntityId($routeName, $entityId)
return $this->friendlyUrlRepository->getAllByRouteNameAndEntityId($routeName, $entityId);
}

/**
* @param string $routeName
* @param int $entityId
* @param int[] $domainIds
* @return \Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\FriendlyUrl[]
*/
public function getAllByRouteNameDomainIdsAndEntityIds(string $routeName, int $entityId, array $domainIds): array
{
return $this->friendlyUrlRepository->getAllByRouteNameDomainIdsAndEntityIds($routeName, $entityId, $domainIds);
}

/**
* @param int $domainId
* @param string $routeName
Expand Down
23 changes: 23 additions & 0 deletions src/Component/Router/FriendlyUrl/FriendlyUrlRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,29 @@ public function getAllByRouteNameAndEntityId($routeName, $entityId)
);
}

/**
* @param string $routeName
* @param int $entityId
* @param int[] $domainIds
* @return \Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\FriendlyUrl[]
*/
public function getAllByRouteNameDomainIdsAndEntityIds(string $routeName, int $entityId, array $domainIds): array
{
$criteria = [
'routeName' => $routeName,
'entityId' => $entityId,
'domainId' => $domainIds,
];

return $this->getFriendlyUrlRepository()->findBy(
$criteria,
[
'domainId' => 'ASC',
'slug' => 'ASC',
],
);
}

/**
* @param string $routeName
* @param int $entityId
Expand Down
Loading

0 comments on commit d88270e

Please sign in to comment.