Completed
Push — master ( 858bee...ca95b0 )
by Pavel
10s
created

RegistrationController::checkUserHashRecovery()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 23
ccs 0
cts 14
cp 0
rs 9.0856
cc 2
eloc 14
nc 2
nop 1
crap 6
1
<?php
2
3
namespace AppBundle\Controller;
4
5
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
6
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
7
use AppBundle\Entity\User;
8
use Symfony\Component\HttpFoundation\Request;
9
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
10
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
11
use Symfony\Component\HttpFoundation\Response;
12
13
/**
14
 * Class RegitrationController
15
 * @package AppBundle\Controller
16
 */
17
class RegistrationController extends Controller
18
{
19
    /**
20
     * @Route("/registration", name="user_registration")
21
     * @Template("@App/registration/registration.html.twig")
22
     */
23 1
    public function registerAction(Request $request)
24
    {
25 1
        return $this->get('app.registration.user')
26 1
            ->registrationUser($request);
27
    }
28
29
    /**
30
     * @Route("/account/update-profile", name="update_profile")
31
     * @Template("@App/registration/updateRegistration.html.twig")
32
     */
33
    public function updateProfileAction(Request $request)
34
    {
35
        $user = $this->getUser();
36
        return $this->get('app.registration.user')
37
            ->updateRegistrationUser($request, $user);
38
    }
39
40
    /**
41
     * @Route("/account/after-soc-login", name="after_soc_login")
42
     */
43
    public function afterSocLogin()
44
    {
45
        $user = $this->getUser();
46
        if (!$user->getPassword())
47
            return $this->redirectToRoute('update_profile');
48
49
        return $this->redirectToRoute('account');
50
    }
51
52
53
    /**
54
     * @Route("/register/check_useremail", name="register_check_email")
55
     */
56
    public function checkUserEmail(Request $request)
57
    {
58
        $email = $request->request->get('email');
59
60
        $em = $this->getDoctrine()->getManager();
61
62
        $user = $em->getRepository('AppBundle:User')
63
            ->findOneBy(array('email' => $email));
64
65
        if ($user) {
66
67
            return new Response('No', 200);
68
        }
69
70
        return new Response('Yes', 200);
71
    }
72
73
    /**
74
     * @Route("/registration/check_hash/{hash}/{email}", name="register_check_hash")
75
     * @Method("GET")
76
     */
77
    public function checkUserHash($hash, $email)
78
    {
79
        $em = $this->getDoctrine()->getManager();
80
81
        $user = $em->getRepository('AppBundle:User')
82
            ->findOneBy(array('email' => $email, 'hash' => $hash));
83
84
        if ($user) {
85
            $user->setIsActive(true);
86
            $user->setHash(null);
87
            $this->addFlash('notice', 'You have successfully passed registration confirmation');
88
89
            $em->flush();
90
91
            return $this->redirectToRoute('homepage');
92
        }
93
94
        $this->addFlash('notice', 'You haven\'t passed registration confirmation');
95
96
        return $this->redirectToRoute('homepage');
97
    }
98
99
    /**
100
     * @Route("/registration/recovery-password", name="recovery_password")
101
     * @Template("@App/registration/recoveryPassword.html.twig")
102
     */
103
    public function recoveryPassword(Request $request)
104
    {
105
        $em = $this->getDoctrine()->getManager();
106
107
        $email = trim($request->get('email'));
108
109
        $user = $em->getRepository('AppBundle:User')
110
            ->findOneBy(['email' => $email]);
111
112
        if ($user && $user->isAccountNonLocked() == true) {
113
            list($password, $hash) = $this->get('app.custom.mailer')->sendMailCheckWithRecovery($user->getEmail());
114
115
            $user->setHash($hash);
116
            $tmpPassword = $this->get('security.password_encoder')
117
                ->encodePassword($user, $password);
118
            $user->setTmpPassword($tmpPassword);
119
120
            $em->flush();
121
122
            $this->addFlash('notice', 'We send new pass for you, please confirm on e-mail');
123
124
            return $this->redirectToRoute('homepage');
125
        } elseif ($email && !$user) {
126
127
            $this->addFlash('notice', 'Email is incorrectly specified');
128
129
            return $this->redirectToRoute('homepage');
130
        } elseif ($user && $user->isAccountNonLocked() == false) {
131
            $this->addFlash('notice', 'You are blocked');
132
133
            return $this->redirectToRoute('homepage');
134
        } else {
135
136
            return [];
137
        }
138
139
    }
140
141
    /**
142
     * @Route("/registration/recovery/check_hash/{hash}", name="recovery_check_hash")
143
     * @Method("GET")
144
     */
145
    public function checkUserHashRecovery($hash)
146
    {
147
        $em = $this->getDoctrine()->getManager();
148
149
        $user = $em->getRepository('AppBundle:User')
150
            ->findOneBy(['hash' => $hash]);
151
152
        if ($user) {
153
            $user->setPassword($user->getTmpPassword());
154
            $user->setIsActive(true);
155
            $user->setHash(null);
156
            $user->setTmpPassword(null);
157
158
            $em->flush();
159
            $this->addFlash('notice', 'The new password is activate');
160
161
            return $this->redirectToRoute('homepage');
162
        }
163
164
        $this->addFlash('notice', 'You haven\'t passed recovery password confirmation');
165
166
        return $this->redirectToRoute('homepage');
167
    }
168
}
169