Completed
Pull Request — master (#5)
by Nicolas
28:56
created

AbstractSecurityController::getTokenManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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);
0 ignored issues
show
Bug introduced by
It seems like $user defined by $this->getUser() on line 92 can also be of type null; however, Doctrine\Common\Persiste...bjectManager::persist() does only seem to accept object, maybe add an additional type check?

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:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
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