1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the core-bundle package. |
5
|
|
|
* |
6
|
|
|
* (c) 2018 WEBEWEB |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace WBW\Bundle\CoreBundle\Controller; |
13
|
|
|
|
14
|
|
|
use Psr\Log\LoggerInterface; |
15
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
16
|
|
|
use Symfony\Component\DependencyInjection\Container; |
17
|
|
|
use Symfony\Component\EventDispatcher\Event; |
18
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
19
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface; |
20
|
|
|
use Symfony\Component\Routing\RouterInterface; |
21
|
|
|
use Symfony\Component\Security\Core\User\User; |
22
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
23
|
|
|
use WBW\Bundle\CoreBundle\Event\NotificationEvent; |
24
|
|
|
use WBW\Bundle\CoreBundle\EventListener\KernelEventListener; |
25
|
|
|
use WBW\Bundle\CoreBundle\Exception\BadUserRoleException; |
26
|
|
|
use WBW\Bundle\CoreBundle\Helper\UserHelper; |
27
|
|
|
use WBW\Bundle\CoreBundle\Notification\NotificationInterface; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Abstract controller. |
31
|
|
|
* |
32
|
|
|
* @author webeweb <https://github.com/webeweb/> |
33
|
|
|
* @package WBW\Bundle\CoreBundle\Controller |
34
|
|
|
* @abstract |
35
|
|
|
*/ |
36
|
|
|
abstract class AbstractController extends Controller { |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Get the container. |
40
|
|
|
* |
41
|
|
|
* @return Container Returns the container. |
42
|
|
|
*/ |
43
|
|
|
protected function getContainer() { |
44
|
|
|
return $this->get("service_container"); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Get the event dispatcher. |
49
|
|
|
* |
50
|
|
|
* @return EventDispatcherInterface Returns the event dispatcher. |
51
|
|
|
*/ |
52
|
|
|
protected function getEventDispatcher() { |
53
|
|
|
return $this->get("event_dispatcher"); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get the kernel event listener. |
58
|
|
|
* |
59
|
|
|
* @return KernelEventListener Returns the kernel event listener. |
60
|
|
|
*/ |
61
|
|
|
protected function getKernelEventListener() { |
62
|
|
|
return $this->get(KernelEventListener::SERVICE_NAME); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get the logger. |
67
|
|
|
* |
68
|
|
|
* @return LoggerInterface Returns the logger. |
69
|
|
|
*/ |
70
|
|
|
protected function getLogger() { |
71
|
|
|
return $this->get("logger"); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get the router. |
76
|
|
|
* |
77
|
|
|
* @return RouterInterface Returns the router. |
78
|
|
|
*/ |
79
|
|
|
protected function getRouter() { |
80
|
|
|
return $this->get("router"); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get the session. |
85
|
|
|
* |
86
|
|
|
* @return SessionInterface Returns the session. |
87
|
|
|
*/ |
88
|
|
|
protected function getSession() { |
89
|
|
|
return $this->get("session"); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Get the translator. |
94
|
|
|
* |
95
|
|
|
* @return TranslatorInterface Returns the translator. |
96
|
|
|
*/ |
97
|
|
|
protected function getTranslator() { |
98
|
|
|
return $this->get("translator"); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Determines if the connected user have roles or redirect. |
103
|
|
|
* |
104
|
|
|
* @param array $roles The roles. |
105
|
|
|
* @param bool $or OR ? |
106
|
|
|
* @param string $redirectUrl The redirect URL. |
107
|
|
|
* @param string $originUrl The origin URL. |
108
|
|
|
* @return bool Returns true. |
109
|
|
|
* @throws BadUserRoleException Throws a bad user role exception. |
110
|
|
|
*/ |
111
|
|
|
protected function hasRolesOrRedirect(array $roles, $or, $redirectUrl, $originUrl = "") { |
112
|
|
|
|
113
|
|
|
// Get the user. |
114
|
|
|
$user = $this->getKernelEventListener()->getUser(); |
115
|
|
|
|
116
|
|
|
// User have roles ? |
117
|
|
|
if (false === UserHelper::hasRoles($user, $roles, $or)) { |
118
|
|
|
|
119
|
|
|
// Throw a bad user role exception with an anonymous user if user is null. |
120
|
|
|
$user = null !== $user ? $user : new User("anonymous", ""); |
121
|
|
|
throw new BadUserRoleException($user, $roles, $redirectUrl, $originUrl); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
// Return |
125
|
|
|
return true; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Notify. |
130
|
|
|
* |
131
|
|
|
* @param string $eventName The event name. |
132
|
|
|
* @param NotificationInterface $notification The notification. |
133
|
|
|
* @return Event Returns the event. |
134
|
|
|
*/ |
135
|
|
|
protected function notify($eventName, NotificationInterface $notification) { |
136
|
|
|
|
137
|
|
|
// Get and check the event dispatcher. |
138
|
|
|
$eventDispatcher = $this->getEventDispatcher(); |
139
|
|
|
if (null === $eventDispatcher || false === $eventDispatcher->hasListeners($eventName)) { |
140
|
|
|
return null; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
// Log a debug trace. |
144
|
|
|
$this->getLogger()->debug(sprintf("Core controller dispatch a notification event with name \"%s\"", $eventName)); |
145
|
|
|
|
146
|
|
|
// Dispatch the event. |
147
|
|
|
return $eventDispatcher->dispatch($eventName, new NotificationEvent($eventName, $notification)); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
} |
151
|
|
|
|