1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Smart\AuthenticationBundle\Controller; |
4
|
|
|
|
5
|
|
|
use Smart\AuthenticationBundle\Security\Form\Type\UserProfileType; |
6
|
|
|
use Smart\AuthenticationBundle\Form\Type\Security\ForgotPasswordType; |
7
|
|
|
use Smart\AuthenticationBundle\Security\Token; |
8
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
9
|
|
|
use Symfony\Component\HttpFoundation\Request; |
10
|
|
|
use Symfony\Component\HttpFoundation\Response; |
11
|
|
|
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils; |
12
|
|
|
use Yokai\MessengerBundle\Sender\SenderInterface; |
13
|
|
|
use Yokai\SecurityTokenBundle\Manager\TokenManagerInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @author Nicolas Bastien <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class AbstractSecurityController extends Controller |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Define application context, override this in your controller |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $context; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @return Response |
28
|
|
|
*/ |
29
|
|
|
public function loginAction() |
30
|
|
|
{ |
31
|
|
|
$helper = $this->getAuthenticationUtils(); |
32
|
|
|
|
33
|
|
|
return $this->render($this->context . '/security/login.html.twig', [ |
34
|
|
|
'last_username' => $helper->getLastUsername(), |
35
|
|
|
'error' => $helper->getLastAuthenticationError(), |
36
|
|
|
'layout_template' => $this->context . '/empty_layout.html.twig', |
37
|
|
|
'security_login_check_url' => $this->generateUrl($this->context . '_security_login_check'), |
38
|
|
|
'security_forgot_password_url' => $this->generateUrl($this->context . '_security_forgot_password'), |
39
|
|
|
]); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param Request $request |
44
|
|
|
* |
45
|
|
|
* @return Response |
46
|
|
|
*/ |
47
|
|
|
public function forgotPasswordAction(Request $request) |
48
|
|
|
{ |
49
|
|
|
$form = $this->createForm(ForgotPasswordType::class); |
50
|
|
|
$form->handleRequest($request); |
51
|
|
|
|
52
|
|
|
if (!$form->isSubmitted() || !$form->isValid()) { |
53
|
|
|
return $this->render( |
54
|
|
|
$this->context . '/security/forgot_password.html.twig', |
55
|
|
|
[ |
56
|
|
|
'form' => $form->createView(), |
57
|
|
|
'security_login_form_url' => $this->generateUrl($this->context . '_security_login_form'), |
58
|
|
|
'security_forgot_password_url' => $this->generateUrl($this->context . '_security_forgot_password'), |
59
|
|
|
] |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$user = $this->get($this->context . '_user_provider')->loadUserByUsername($form->get('email')->getData()); |
64
|
|
|
|
65
|
|
|
$this->addFlash('success', 'flash.forgot_password.success'); |
66
|
|
|
|
67
|
|
|
if ($user) { |
68
|
|
|
$token = $this->getTokenManager()->create(Token::RESET_PASSWORD, $user); |
69
|
|
|
|
70
|
|
|
$this->getMessenger()->send( |
71
|
|
|
'security.forgot_password', |
72
|
|
|
$user, |
73
|
|
|
[ |
74
|
|
|
'{context}' => $this->context, |
75
|
|
|
'token' => $token->getValue(), |
76
|
|
|
'domain' => $this->container->getParameter('domain'), |
77
|
|
|
'security_reset_password_route' => $this->context . '_security_reset_password' |
78
|
|
|
] |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $this->redirectToRoute($this->context . '_security_login_form'); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param Request $request |
87
|
|
|
* |
88
|
|
|
* @return Response |
89
|
|
|
*/ |
90
|
|
|
public function profileAction(Request $request) |
91
|
|
|
{ |
92
|
|
|
$user = $this->getUser(); |
93
|
|
|
|
94
|
|
|
$form = $this->createForm(UserProfileType::class, $user, []); |
95
|
|
|
|
96
|
|
|
$form->handleRequest($request); |
97
|
|
|
|
98
|
|
|
if (!$form->isSubmitted() || !$form->isValid()) { |
99
|
|
|
return $this->render($this->context . '/security/profile.html.twig', [ |
100
|
|
|
'base_template' => $this->get('sonata.admin.pool')->getTemplate('layout'), |
101
|
|
|
'admin_pool' => $this->get('sonata.admin.pool'), |
102
|
|
|
'form' => $form->createView(), |
103
|
|
|
'security_profile_url' => $this->generateUrl('admin_security_profile'), |
104
|
|
|
]); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if (null !== $user->getPlainPassword()) { |
108
|
|
|
$encoder = $this->get('security.password_encoder'); |
109
|
|
|
$user->setPassword( |
110
|
|
|
$encoder->encodePassword($user, $user->getPlainPassword()) |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$manager = $this->getDoctrine()->getManager(); |
115
|
|
|
$manager->persist($user); |
|
|
|
|
116
|
|
|
$manager->flush(); |
117
|
|
|
|
118
|
|
|
$this->addFlash('success', $this->translate('profile_edit.processed', [], 'security')); |
119
|
|
|
|
120
|
|
|
return $this->redirectToRoute('sonata_admin_dashboard'); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return AuthenticationUtils |
125
|
|
|
*/ |
126
|
|
|
private function getAuthenticationUtils() |
127
|
|
|
{ |
128
|
|
|
return $this->get('security.authentication_utils'); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param string $id The message id (may also be an object that can be cast to string) |
133
|
|
|
* @param array $parameters An array of parameters for the message |
134
|
|
|
* @param string|null $domain The domain for the message or null to use the default |
135
|
|
|
* |
136
|
|
|
* @return string |
137
|
|
|
*/ |
138
|
|
|
protected function translate($id, array $parameters = array(), $domain = null) |
139
|
|
|
{ |
140
|
|
|
return $this->get('translator')->trans($id, $parameters, $domain); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @return TokenManagerInterface |
145
|
|
|
*/ |
146
|
|
|
private function getTokenManager() |
147
|
|
|
{ |
148
|
|
|
return $this->get('yokai_security_token.token_manager'); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @return SenderInterface |
153
|
|
|
*/ |
154
|
|
|
protected function getMessenger() |
155
|
|
|
{ |
156
|
|
|
return $this->get('yokai_messenger.sender'); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.