1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Smart\AuthenticationBundle\Controller; |
4
|
|
|
|
5
|
|
|
use Smart\AuthenticationBundle\Security\Form\Type\UserProfileType; |
6
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
7
|
|
|
use Symfony\Component\HttpFoundation\Request; |
8
|
|
|
use Symfony\Component\HttpFoundation\Response; |
9
|
|
|
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @author Nicolas Bastien <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class AbstractSecurityController extends Controller |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Define application context, override this in your controller |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $context; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param string $id The message id (may also be an object that can be cast to string) |
24
|
|
|
* @param array $parameters An array of parameters for the message |
25
|
|
|
* @param string|null $domain The domain for the message or null to use the default |
26
|
|
|
* |
27
|
|
|
* @return string |
28
|
|
|
*/ |
29
|
|
|
protected function translate($id, array $parameters = array(), $domain = null) |
30
|
|
|
{ |
31
|
|
|
return $this->get('translator')->trans($id, $parameters, $domain); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @return Response |
36
|
|
|
*/ |
37
|
|
|
public function loginAction() |
38
|
|
|
{ |
39
|
|
|
$helper = $this->getAuthenticationUtils(); |
40
|
|
|
|
41
|
|
|
return $this->render($this->context . '/security/login.html.twig', [ |
42
|
|
|
'last_username' => $helper->getLastUsername(), |
43
|
|
|
'error' => $helper->getLastAuthenticationError(), |
44
|
|
|
'layout_template' => $this->context . '/empty_layout.html.twig', |
45
|
|
|
'security_login_check_url' => $this->generateUrl($this->context . '_security_login_check'), |
46
|
|
|
// 'security_forgot_password_url' => $this->generateUrl($this->context . '_security_forgot_password'), |
|
|
|
|
47
|
|
|
]); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return AuthenticationUtils |
52
|
|
|
*/ |
53
|
|
|
private function getAuthenticationUtils() |
54
|
|
|
{ |
55
|
|
|
return $this->get('security.authentication_utils'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param Request $request |
60
|
|
|
* |
61
|
|
|
* @return Response |
62
|
|
|
*/ |
63
|
|
|
public function profileAction(Request $request) |
64
|
|
|
{ |
65
|
|
|
$user = $this->getUser(); |
66
|
|
|
|
67
|
|
|
$form = $this->createForm(UserProfileType::class, $user, []); |
68
|
|
|
|
69
|
|
|
$form->handleRequest($request); |
70
|
|
|
|
71
|
|
|
if (!$form->isSubmitted() || !$form->isValid()) { |
72
|
|
|
return $this->render($this->context . '/security/profile.html.twig', [ |
73
|
|
|
'base_template' => $this->get('sonata.admin.pool')->getTemplate('layout'), |
74
|
|
|
'admin_pool' => $this->get('sonata.admin.pool'), |
75
|
|
|
'form' => $form->createView(), |
76
|
|
|
'security_profile_url' => $this->generateUrl('admin_security_profile'), |
77
|
|
|
]); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if (null !== $user->getPlainPassword()) { |
81
|
|
|
$encoder = $this->get('security.password_encoder'); |
82
|
|
|
$user->setPassword( |
83
|
|
|
$encoder->encodePassword($user, $user->getPlainPassword()) |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$manager = $this->getDoctrine()->getManager(); |
88
|
|
|
$manager->persist($user); |
|
|
|
|
89
|
|
|
$manager->flush(); |
90
|
|
|
|
91
|
|
|
$this->addFlash('success', $this->translate('profile_edit.processed', [], 'security')); |
92
|
|
|
|
93
|
|
|
return $this->redirectToRoute('sonata_admin_dashboard'); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.