Completed
Push — master ( b2b032...858bee )
by Pavel
09:16 queued 06:10
created

RegistrationController::updateProfileAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 0
cts 4
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 2
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
            $password = $this->get('app.custom.mailer')->sendMailRecovery($email);
114
            $newPassword = $this->get('security.password_encoder')
115
                ->encodePassword($user, $password);
116
            $user->setPassword($newPassword);
117
            $user->setIsActive(true);
118
119
            $em->flush();
120
            $this->addFlash('notice', 'The new password is sent to your email');
121
122
            return $this->redirectToRoute('homepage');
123
        } elseif ($email && !$user) {
124
125
            $this->addFlash('notice', 'Email is incorrectly specified');
126
127
            return $this->redirectToRoute('homepage');
128
        } elseif ($user && $user->isAccountNonLocked() == false) {
129
            $this->addFlash('notice', 'You are blocked');
130
131
            return $this->redirectToRoute('homepage');
132
        } else {
133
134
            return [];
135
        }
136
    }
137
}
138