1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* (c) FSi sp. z o.o. <[email protected]> |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace FSi\Bundle\AdminSecurityBundle\EventListener; |
11
|
|
|
|
12
|
|
|
use FSi\Bundle\AdminBundle\Event\MenuEvent; |
13
|
|
|
use FSi\Bundle\AdminBundle\Menu\Item\Item; |
14
|
|
|
use FSi\Bundle\AdminBundle\Menu\Item\RoutableItem; |
15
|
|
|
use FSi\Bundle\AdminSecurityBundle\Security\User\ChangeablePasswordInterface; |
16
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
17
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
18
|
|
|
|
19
|
|
|
class BuildAccountMenuListener |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var TranslatorInterface |
23
|
|
|
*/ |
24
|
|
|
private $translator; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var TokenStorageInterface |
28
|
|
|
*/ |
29
|
|
|
private $tokenStorage; |
30
|
|
|
|
31
|
|
|
public function __construct( |
32
|
|
|
TranslatorInterface $translator, |
33
|
|
|
TokenStorageInterface $tokenStorage |
34
|
|
|
) { |
35
|
|
|
$this->translator = $translator; |
36
|
|
|
$this->tokenStorage = $tokenStorage; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function createAccountMenu(MenuEvent $event) |
40
|
|
|
{ |
41
|
|
|
if (!$this->hasUserLoggedIn()) { |
42
|
|
|
return null; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$rootItem = $this->createRootItem(); |
46
|
|
|
|
47
|
|
|
if ($this->canChangeUserPassword()) { |
48
|
|
|
$changePasswordItem = new RoutableItem('account.change-password', 'fsi_admin_change_password'); |
49
|
|
|
$changePasswordItem->setLabel($this->translator->trans('admin.change_password', [], 'FSiAdminSecurity')); |
50
|
|
|
$rootItem->addChild($changePasswordItem); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$logoutItem = new RoutableItem('account.logout', 'fsi_admin_security_user_logout'); |
54
|
|
|
$logoutItem->setLabel($this->translator->trans('admin.logout', [], 'FSiAdminSecurity')); |
55
|
|
|
$rootItem->addChild($logoutItem); |
56
|
|
|
|
57
|
|
|
$event->getMenu()->addChild($rootItem); |
58
|
|
|
|
59
|
|
|
return $event->getMenu(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return Item |
64
|
|
|
*/ |
65
|
|
|
private function createRootItem() |
66
|
|
|
{ |
67
|
|
|
$rootItem = new Item('account'); |
68
|
|
|
|
69
|
|
|
$rootItem->setLabel( |
70
|
|
|
$this->translator->trans( |
71
|
|
|
'admin.welcome', |
72
|
|
|
['%username%' => $this->tokenStorage->getToken()->getUsername()], |
73
|
|
|
'FSiAdminSecurity' |
74
|
|
|
) |
75
|
|
|
); |
76
|
|
|
|
77
|
|
|
$rootItem->setOptions([ |
78
|
|
|
'attr' => [ |
79
|
|
|
'id' => 'account', |
80
|
|
|
] |
81
|
|
|
]); |
82
|
|
|
|
83
|
|
|
return $rootItem; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return bool |
88
|
|
|
*/ |
89
|
|
|
private function hasUserLoggedIn() |
90
|
|
|
{ |
91
|
|
|
return $this->tokenStorage->getToken() !== null; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return bool |
96
|
|
|
*/ |
97
|
|
|
private function canChangeUserPassword() |
98
|
|
|
{ |
99
|
|
|
return $this->tokenStorage->getToken()->getUser() instanceof ChangeablePasswordInterface; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|