<?php
namespace Aqarmap\Bundle\UserBundle\EventListener;
use Aqarmap\Bundle\MainBundle\Event\ConfigureMenuEvent;
use Aqarmap\Bundle\PaymentBundle\Constant\GatewayTypes;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
class ConfigureMenuEventSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
{
protected TranslatorInterface $translator;
/**
* @var TokenStorageInterface
*/
private $tokenStorage;
/**
* @var AuthorizationCheckerInterface
*/
private $authorizationChecker;
public function __construct(
TranslatorInterface $translator,
TokenStorageInterface $tokenStorage,
AuthorizationCheckerInterface $authorizationChecker
) {
$this->translator = $translator;
$this->tokenStorage = $tokenStorage;
$this->authorizationChecker = $authorizationChecker;
}
public function onAdminSidebarMenuConfigure(ConfigureMenuEvent $event): void
{
$menu = $event->getMenu();
$userRole = $this->authorizationChecker;
if ($userRole->isGranted('ROLE_SUPPORT')
|| $userRole->isGranted('ROLE_USER_MANAGEMENT')
|| $userRole->isGranted('ROLE_MARKETING')
|| $userRole->isGranted('ROLE_IMPORT_USERS')
|| $userRole->isGranted('ROLE_IMPORT_USERS_UGC')
|| $userRole->isGranted('ROLE_IMPORT_DEVELOPER_USERS')
|| $userRole->isGranted('ROLE_CREDIT_PACKAGE')) {
$menu
->addChild('users_group', ['uri' => '#'])
->setLabel('Users Management')
->setExtra('position', 50)
->setExtra('icon', 'users');
}
if ($userRole->isGranted('ROLE_SUPPORT')) {
$menu['users_group']
->addChild('Users', ['route' => 'aqarmap_admin_user'])
->setLabel($this->translator->trans('layout.admin.users'))
->setExtra('position', 51)
->setExtra('icon', 'user');
}
if ($userRole->isGranted('ROLE_IMPORT_USERS')) {
$menu['users_group']
->addChild('import_users', ['route' => 'aqarmap_admin_import_users'])
->setLabel($this->translator->trans('layout.admin.import_users'))
->setExtra('position', 52)
->setExtra('icon', 'upload');
}
if ($userRole->isGranted('ROLE_IMPORT_USERS_UGC')) {
$menu['users_group']
->addChild('import_users_UGC', ['route' => 'aqarmap_admin_import_users_ugc'])
->setLabel($this->translator->trans('layout.admin.import_users_ugc.label'))
->setExtra('position', 53)
->setExtra('icon', 'upload');
}
if ($userRole->isGranted('ROLE_IMPORT_DEVELOPER_USERS')) {
$menu['users_group']
->addChild('import_developer_users', ['route' => 'aqarmap_admin_import_developer_users'])
->setLabel($this->translator->trans('layout.admin.import_developer_users'))
->setExtra('position', 61)
->setExtra('icon', 'upload');
}
if ($userRole->isGranted('ROLE_USER_MANAGEMENT')) {
// Packages
$menu['users_group']
->addChild('user_team', ['route' => 'aqarmap_admin_team_index'])
->setLabel($this->translator->trans('team.admin_label'))
->setExtra('position', 58)
->setExtra('icon', 'users');
// Transactions
$menu['users_group']
->addChild('credit_transactions', ['route' => 'aqarmap_admin_credit_transaction'])
->setLabel($this->translator->trans('layout.admin.credit_transactions.title'))
->setExtra('position', 55)
->setExtra('icon', 'bitcoin')
->setExtra('brand_icon', true);
// Payment
$menu['users_group']
->addChild('payment_transactions', [
'route' => 'aqarmap_admin_payment_transactions',
'routeParameters' => [
'status' => 'approved',
'order' => 'approval',
],
])
->setLabel($this->translator->trans('layout.admin.payment_transactions'))
->setExtra('position', 56)
->setExtra('icon', 'cc-visa')
->setExtra('brand_icon', true);
$menu['users_group']
->addChild('manual_fawry', [
'route' => 'aqarmap_admin_payment_transactions',
'routeParameters' => [
'gateway' => GatewayTypes::MANUAL_FAWRY,
'order' => 'approval',
],
])
->setLabel($this->translator->trans('layout.admin.manual_fawry'))
->setExtra('position', 62)
->setExtra('icon', 'cc-visa')
->setExtra('brand_icon', true);
}
if ($userRole->isGranted('ROLE_CREDIT_PACKAGE')) {
$menu['users_group']
->addChild('credit_package', ['route' => 'aqarmap_admin_package'])
->setLabel($this->translator->trans('layout.admin.credit_package'))
->setExtra('position', 54)
->setExtra('icon', 'bullhorn');
}
}
public function onMainNavbarMenuConfigure(ConfigureMenuEvent $event): void
{
$menu = $event->getMenu();
if ($this->getAuthorizationChecker()->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
// Logout
$username = $this->getTokenStorage()->getToken()->getUser()->getUsername();
$menu
->addChild('logout', ['route' => 'app_logout'])
->setLabel($this->translator->trans('layout.logout_username', ['%username%' => $username]))
->setExtra('position', 1000);
} else {
// Register
$menu
->addChild('register', ['route' => 'app_register'])
->setLabel($this->translator->trans('layout.register'))
->setExtra('position', 900);
// Login
$menu
->addChild('login', ['route' => 'app_login'])
->setLabel($this->translator->trans('layout.signin'))
->setExtra('position', 800);
}
}
/**
* @return TokenStorageInterface
*/
private function getTokenStorage()
{
return $this->tokenStorage;
}
/**
* @return AuthorizationCheckerInterface
*/
private function getAuthorizationChecker()
{
return $this->authorizationChecker;
}
/**
* @return array<string, mixed>
*/
public static function getSubscribedEvents(): array
{
return ['aqarmap.main.admin_sidebar_menu_configure' => 'onAdminSidebarMenuConfigure'];
}
}