Issues (3627)

bundles/UserBundle/Controller/PublicController.php (1 issue)

1
<?php
2
3
/*
4
 * @copyright   2014 Mautic Contributors. All rights reserved
5
 * @author      Mautic
6
 *
7
 * @link        http://mautic.org
8
 *
9
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
10
 */
11
12
namespace Mautic\UserBundle\Controller;
13
14
use Mautic\CoreBundle\Controller\FormController;
15
use Mautic\UserBundle\Form\Type\PasswordResetConfirmType;
16
use Mautic\UserBundle\Form\Type\PasswordResetType;
17
use Symfony\Component\Form\FormError;
18
19
class PublicController extends FormController
20
{
21
    /**
22
     * Generates a new password for the user and emails it to them.
23
     */
24
    public function passwordResetAction()
25
    {
26
        /** @var \Mautic\UserBundle\Model\UserModel $model */
27
        $model = $this->getModel('user');
28
29
        $data   = ['identifier' => ''];
30
        $action = $this->generateUrl('mautic_user_passwordreset');
31
        $form   = $this->get('form.factory')->create(PasswordResetType::class, $data, ['action' => $action]);
32
33
        ///Check for a submitted form and process it
34
        if ('POST' == $this->request->getMethod()) {
35
            if ($isValid = $this->isFormValid($form)) {
36
                //find the user
37
                $data = $form->getData();
38
                $user = $model->getRepository()->findByIdentifier($data['identifier']);
39
40
                if (null == $user) {
41
                    $form['identifier']->addError(new FormError($this->translator->trans('mautic.user.user.passwordreset.nouserfound', [], 'validators')));
42
                } else {
43
                    try {
44
                        $model->sendResetEmail($user);
45
                        $this->addFlash('mautic.user.user.notice.passwordreset');
46
                    } catch (\Exception $exception) {
47
                        $this->addFlash('mautic.user.user.notice.passwordreset.error', [], 'error');
0 ignored issues
show
Deprecated Code introduced by
The function Mautic\CoreBundle\Contro...nController::addFlash() has been deprecated: Will be removed in Mautic 3.0. Use CommonController::flashBag->addFlash() instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

47
                        /** @scrutinizer ignore-deprecated */ $this->addFlash('mautic.user.user.notice.passwordreset.error', [], 'error');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
48
                    }
49
50
                    return $this->redirect($this->generateUrl('login'));
51
                }
52
            }
53
        }
54
55
        return $this->delegateView([
56
            'viewParameters' => [
57
                'form' => $form->createView(),
58
            ],
59
            'contentTemplate' => 'MauticUserBundle:Security:reset.html.php',
60
            'passthroughVars' => [
61
                'route' => $action,
62
            ],
63
        ]);
64
    }
65
66
    public function passwordResetConfirmAction()
67
    {
68
        /** @var \Mautic\UserBundle\Model\UserModel $model */
69
        $model = $this->getModel('user');
70
71
        $data   = ['identifier' => '', 'password' => '', 'password_confirm' => ''];
72
        $action = $this->generateUrl('mautic_user_passwordresetconfirm');
73
        $form   = $this->get('form.factory')->create(PasswordResetConfirmType::class, [], ['action' => $action]);
74
        $token  = $this->request->query->get('token');
75
76
        if ($token) {
77
            $this->request->getSession()->set('resetToken', $token);
78
        }
79
80
        ///Check for a submitted form and process it
81
        if ('POST' == $this->request->getMethod()) {
82
            if ($isValid = $this->isFormValid($form)) {
83
                //find the user
84
                $data = $form->getData();
85
                /** @var \Mautic\UserBundle\Entity\User $user */
86
                $user = $model->getRepository()->findByIdentifier($data['identifier']);
87
88
                if (null == $user) {
89
                    $form['identifier']->addError(new FormError($this->translator->trans('mautic.user.user.passwordreset.nouserfound', [], 'validators')));
90
                } else {
91
                    if ($this->request->getSession()->has('resetToken')) {
92
                        $resetToken = $this->request->getSession()->get('resetToken');
93
                        $encoder    = $this->get('security.encoder_factory')->getEncoder($user);
94
95
                        if ($model->confirmResetToken($user, $resetToken)) {
96
                            $encodedPassword = $model->checkNewPassword($user, $encoder, $data['plainPassword']);
97
                            $user->setPassword($encodedPassword);
98
                            $model->saveEntity($user);
99
100
                            $this->addFlash('mautic.user.user.notice.passwordreset.success');
101
102
                            $this->request->getSession()->remove('resetToken');
103
104
                            return $this->redirect($this->generateUrl('login'));
105
                        }
106
107
                        return $this->delegateView([
108
                            'viewParameters' => [
109
                                'form' => $form->createView(),
110
                            ],
111
                            'contentTemplate' => 'MauticUserBundle:Security:resetconfirm.html.php',
112
                            'passthroughVars' => [
113
                                'route' => $action,
114
                            ],
115
                        ]);
116
                    } else {
117
                        $this->addFlash('mautic.user.user.notice.passwordreset.missingtoken');
118
119
                        return $this->redirect($this->generateUrl('mautic_user_passwordresetconfirm'));
120
                    }
121
                }
122
            }
123
        }
124
125
        return $this->delegateView([
126
            'viewParameters' => [
127
                'form' => $form->createView(),
128
            ],
129
            'contentTemplate' => 'MauticUserBundle:Security:resetconfirm.html.php',
130
            'passthroughVars' => [
131
                'route' => $action,
132
            ],
133
        ]);
134
    }
135
}
136