1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpDraft\Controllers; |
4
|
|
|
|
5
|
|
|
use \Silex\Application; |
6
|
|
|
use Symfony\Component\HttpFoundation\Request; |
7
|
|
|
use Symfony\Component\HttpFoundation\Response; |
8
|
|
|
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; |
9
|
|
|
use PhpDraft\Domain\Entities\LoginUser; |
10
|
|
|
use PhpDraft\Domain\Models\PhpDraftResponse; |
11
|
|
|
|
12
|
|
|
class AuthenticationController |
13
|
|
|
{ |
14
|
|
|
//See Commish->Index for permissions check |
15
|
|
|
|
16
|
|
|
public function Login(Application $app, Request $request) { |
17
|
|
|
$email = $request->get('_email'); |
18
|
|
|
$password = $request->get('_password'); |
19
|
|
|
|
20
|
|
|
$response = new PhpDraftResponse(); |
21
|
|
|
|
22
|
|
|
try { |
23
|
|
|
$credentialValidity = $app['phpdraft.LoginUserValidator']->areLoginCredentialsValid($email, $password); |
24
|
|
|
|
25
|
|
|
if ($credentialValidity->success == false) { |
26
|
|
|
throw new UsernameNotFoundException(sprintf('Email %s does not exist', $email)); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
$user = $app['users']->loadUserByUsername($email); |
30
|
|
|
|
31
|
|
|
if (!$user->isEnabled() || !$app['security.encoder.digest']->isPasswordValid($user->getPassword(), $password, $user->getSalt())) { |
32
|
|
|
throw new UsernameNotFoundException(sprintf('Email %s does not exist', $email)); |
33
|
|
|
} else { |
34
|
|
|
$response->success = true; |
35
|
|
|
|
36
|
|
|
$response = $app['phpdraft.LoginUserService']->SetAuthenticationObjectValuesOnLogin($response, $user); |
37
|
|
|
|
38
|
|
|
//If user is enabled, provided valid password and has a verification (pwd reset) key, wipe it (no longer needed) |
39
|
|
|
if ($user->hasVerificationKey()) { |
40
|
|
|
$app['phpdraft.LoginUserRepository']->EraseVerificationKey($user->getEmail()); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
} catch (UsernameNotFoundException $e) { |
44
|
|
|
$response->success = false; |
45
|
|
|
$response->errors[] = 'Invalid credentials.'; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return $app->json($response, $response->responseType()); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function Register(Application $app, Request $request) { |
52
|
|
|
$validity = $app['phpdraft.LoginUserValidator']->IsRegistrationUserValid($request); |
53
|
|
|
|
54
|
|
|
if (!$validity->success) { |
55
|
|
|
return $app->json($validity, Response::HTTP_BAD_REQUEST); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
//TODO: Remove. Temporary workaround to disable Recaptcha verifications on localhost |
59
|
|
|
$whitelist = array( |
60
|
|
|
'127.0.0.1', |
61
|
|
|
'::1' |
62
|
|
|
); |
63
|
|
|
|
64
|
|
|
$captcha = $request->get('_recaptcha'); |
65
|
|
|
$userIp = $request->getClientIp(); |
66
|
|
|
|
67
|
|
|
if (!in_array($userIp, $whitelist)) { |
68
|
|
|
|
69
|
|
|
$recaptcha = new \ReCaptcha\ReCaptcha(RECAPTCHA_SECRET); |
|
|
|
|
70
|
|
|
$recaptchaResponse = $recaptcha->verify($captcha, $userIp); |
71
|
|
|
|
72
|
|
|
if (!$recaptchaResponse->isSuccess()) { |
73
|
|
|
$response = new PhpDraftResponse(false, array()); |
74
|
|
|
$response->errors = $recaptchaResponse->getErrorCodes(); |
75
|
|
|
return $app->json($response, $response->responseType()); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$user = new LoginUser(); |
80
|
|
|
|
81
|
|
|
$user->email = $request->get('_email'); |
82
|
|
|
$user->password = $request->get('_password'); |
83
|
|
|
$user->name = $request->get('_name'); |
84
|
|
|
|
85
|
|
|
$response = $app['phpdraft.LoginUserService']->CreateUnverifiedNewUser($user); |
86
|
|
|
|
87
|
|
|
return $app->json($response, $response->responseType()); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function VerifyAccount(Application $app, Request $request) { |
91
|
|
|
$validity = $app['phpdraft.LoginUserValidator']->IsVerificationValid($request); |
92
|
|
|
|
93
|
|
|
if (!$validity->success) { |
94
|
|
|
return $app->json($validity, Response::HTTP_BAD_REQUEST); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$email = $request->get('_email'); |
98
|
|
|
|
99
|
|
|
$user = $app['phpdraft.LoginUserRepository']->Load($email); |
100
|
|
|
|
101
|
|
|
$response = $app['phpdraft.LoginUserService']->VerifyUser($user); |
102
|
|
|
|
103
|
|
|
if ($response->success) { |
104
|
|
|
$loginUser = $app['users']->loadUserByUsername($email); |
105
|
|
|
$response = $app['phpdraft.LoginUserService']->SetAuthenticationObjectValuesOnLogin($response, $loginUser); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $app->json($response, $response->responseType()); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function LostPassword(Application $app, Request $request) { |
112
|
|
|
$validity = $app['phpdraft.LoginUserValidator']->IsForgottenPasswordUserValid($request); |
113
|
|
|
|
114
|
|
|
if (!$validity->success) { |
115
|
|
|
return $app->json($validity, Response::HTTP_BAD_REQUEST); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$email = $request->get('_email'); |
119
|
|
|
|
120
|
|
|
//TODO: Remove. Temporary workaround to disable Recaptcha verifications on localhost |
121
|
|
|
$whitelist = array( |
122
|
|
|
'127.0.0.1', |
123
|
|
|
'::1' |
124
|
|
|
); |
125
|
|
|
|
126
|
|
|
$captcha = $request->get('_recaptcha'); |
127
|
|
|
$userIp = $request->getClientIp(); |
128
|
|
|
|
129
|
|
|
if (!in_array($userIp, $whitelist)) { |
130
|
|
|
|
131
|
|
|
$recaptcha = new \ReCaptcha\ReCaptcha(RECAPTCHA_SECRET); |
|
|
|
|
132
|
|
|
$recaptchaResponse = $recaptcha->verify($captcha, $userIp); |
133
|
|
|
|
134
|
|
|
if (!$recaptchaResponse->isSuccess()) { |
135
|
|
|
$response = new PhpDraftResponse(false, array()); |
136
|
|
|
$response->errors = $recaptchaResponse->getErrorCodes(); |
137
|
|
|
return $app->json($response, $response->responseType()); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$user = $app['phpdraft.LoginUserRepository']->Load($email); |
142
|
|
|
|
143
|
|
|
$response = $app['phpdraft.LoginUserService']->BeginForgottenPasswordProcess($user); |
144
|
|
|
|
145
|
|
|
return $app->json($response, $response->responseType()); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function VerifyResetPasswordToken(Application $app, Request $request) { |
149
|
|
|
$email = $request->get('_email'); |
150
|
|
|
$verificationToken = $request->get('_verificationToken'); |
151
|
|
|
|
152
|
|
|
$validity = $app['phpdraft.LoginUserValidator']->IsResetPasswordTokenValid($email, $verificationToken); |
153
|
|
|
|
154
|
|
|
return $app->json($validity, $validity->responseType()); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function ResetPassword(Application $app, Request $request) { |
158
|
|
|
$validity = $app['phpdraft.LoginUserValidator']->IsResetPasswordRequestValid($request); |
159
|
|
|
|
160
|
|
|
if (!$validity->success) { |
161
|
|
|
return $app->json($validity, Response::HTTP_BAD_REQUEST); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
$email = $request->get('_email'); |
165
|
|
|
$password = $request->get('_password'); |
166
|
|
|
|
167
|
|
|
$user = $app['phpdraft.LoginUserRepository']->Load($email); |
168
|
|
|
|
169
|
|
|
$user->password = $password; |
170
|
|
|
|
171
|
|
|
$response = $app['phpdraft.LoginUserService']->ResetPassword($user); |
172
|
|
|
|
173
|
|
|
if ($response->success) { |
174
|
|
|
$loginUser = $app['users']->loadUserByUsername($email); |
175
|
|
|
$response = $app['phpdraft.LoginUserService']->SetAuthenticationObjectValuesOnLogin($response, $loginUser); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
return $app->json($response, $response->responseType()); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|