1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use App\Constants\TranslationCode; |
6
|
|
|
use App\Models\User; |
7
|
|
|
use App\Services\LogService; |
8
|
|
|
use App\Services\UserService; |
9
|
|
|
use Carbon\Carbon; |
10
|
|
|
use Exception; |
11
|
|
|
use Illuminate\Http\JsonResponse; |
12
|
|
|
use Illuminate\Http\Request; |
13
|
|
|
use Illuminate\Support\Facades\Auth; |
14
|
|
|
use Illuminate\Support\Facades\DB; |
15
|
|
|
use Illuminate\Support\Facades\Hash; |
16
|
|
|
use Illuminate\Support\Facades\Log; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class UserController |
20
|
|
|
* |
21
|
|
|
* @package App\Http\Controllers |
22
|
|
|
*/ |
23
|
|
|
class UserController extends Controller |
24
|
|
|
{ |
25
|
|
|
/** @var UserService */ |
26
|
|
|
private $userService; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* UserController constructor. |
30
|
|
|
*/ |
31
|
|
|
public function __construct() |
32
|
|
|
{ |
33
|
|
|
parent::__construct(); |
34
|
|
|
|
35
|
|
|
$this->userService = new UserService(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Register the user, send activation code on email |
40
|
|
|
* |
41
|
|
|
* @param Request $request |
42
|
|
|
* |
43
|
|
|
* @return JsonResponse |
44
|
|
|
*/ |
45
|
|
|
public function register(Request $request) |
46
|
|
|
{ |
47
|
|
|
try { |
48
|
|
|
$validator = $this->userService->validateRegisterRequest($request); |
49
|
|
|
|
50
|
|
|
if (!$validator->passes()) { |
51
|
|
|
return $this->userErrorResponse($validator->messages()); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$request->merge(['password' => Hash::make($request->get('password'))]); |
55
|
|
|
|
56
|
|
|
DB::beginTransaction(); |
57
|
|
|
|
58
|
|
|
$this->userService->registerUser($request, $this->baseService->getLanguage($request)); |
59
|
|
|
|
60
|
|
|
DB::commit(); |
61
|
|
|
|
62
|
|
|
return $this->successResponse(); |
63
|
|
|
} catch (Exception $e) { |
64
|
|
|
Log::error(LogService::getExceptionTraceAsString($e)); |
65
|
|
|
|
66
|
|
|
return $this->errorResponse(); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Generate and send a forgot code on email |
72
|
|
|
* |
73
|
|
|
* @param Request $request |
74
|
|
|
* |
75
|
|
|
* @return JsonResponse |
76
|
|
|
*/ |
77
|
|
|
public function forgotPassword(Request $request) |
78
|
|
|
{ |
79
|
|
|
try { |
80
|
|
|
$validator = $this->userService->validateForgotPasswordRequest($request); |
81
|
|
|
|
82
|
|
|
if (!$validator->passes()) { |
83
|
|
|
return $this->userErrorResponse($validator->messages()); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$user = User::whereEncrypted('email', $request->get('email'))->first(); |
87
|
|
|
|
88
|
|
|
if ($user->status === User::STATUS_UNCONFIRMED) { |
89
|
|
|
return $this->userErrorResponse(['account' => TranslationCode::ERROR_ACCOUNT_UNACTIVATED]); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if ($user->updated_at->addMinute() > Carbon::now()) { |
93
|
|
|
return $this->userErrorResponse(['forgot' => TranslationCode::ERROR_FORGOT_CODE_SEND_COOLDOWN]); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
DB::beginTransaction(); |
97
|
|
|
|
98
|
|
|
$this->userService->sendForgotPasswordCode($user, $this->baseService->getLanguage($request)); |
99
|
|
|
|
100
|
|
|
DB::commit(); |
101
|
|
|
|
102
|
|
|
return $this->successResponse(); |
103
|
|
|
} catch (Exception $e) { |
104
|
|
|
Log::error(LogService::getExceptionTraceAsString($e)); |
105
|
|
|
|
106
|
|
|
return $this->errorResponse(); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Change password with generated code |
112
|
|
|
* |
113
|
|
|
* @param Request $request |
114
|
|
|
* |
115
|
|
|
* @return JsonResponse |
116
|
|
|
*/ |
117
|
|
|
public function changePassword(Request $request) |
118
|
|
|
{ |
119
|
|
|
try { |
120
|
|
|
$validator = $this->userService->validateChangePasswordRequest($request); |
121
|
|
|
|
122
|
|
|
if (!$validator->passes()) { |
123
|
|
|
return $this->userErrorResponse($validator->messages()); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** @var User|null $user */ |
127
|
|
|
$user = User::whereEncrypted('email', $request->get('email')) |
128
|
|
|
->where('forgot_code', $request->get('code')) |
129
|
|
|
->first(); |
130
|
|
|
|
131
|
|
|
if (!$user) { |
132
|
|
|
return $this->userErrorResponse(['forgot' => TranslationCode::ERROR_FORGOT_CODE_INVALID]); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
if (Carbon::parse($user->forgot_time)->addHour() < Carbon::now()) { |
136
|
|
|
return $this->userErrorResponse(['forgot' => TranslationCode::ERROR_FORGOT_PASSED_1H]); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
DB::beginTransaction(); |
140
|
|
|
|
141
|
|
|
$this->userService->updatePassword($user, $request->get('password')); |
142
|
|
|
|
143
|
|
|
DB::commit(); |
144
|
|
|
|
145
|
|
|
return $this->successResponse(); |
146
|
|
|
} catch (Exception $e) { |
147
|
|
|
Log::error(LogService::getExceptionTraceAsString($e)); |
148
|
|
|
|
149
|
|
|
return $this->errorResponse(); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Activate account |
155
|
|
|
* |
156
|
|
|
* @param Request $request |
157
|
|
|
* |
158
|
|
|
* @return JsonResponse |
159
|
|
|
*/ |
160
|
|
|
public function activateAccount(Request $request) |
161
|
|
|
{ |
162
|
|
|
try { |
163
|
|
|
$validator = $this->userService->validateActivateAccountOrChangeEmailRequest($request); |
164
|
|
|
|
165
|
|
|
if (!$validator->passes()) { |
166
|
|
|
return $this->userErrorResponse($validator->messages()); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
DB::beginTransaction(); |
170
|
|
|
|
171
|
|
|
$activated = $this->userService->activateUserAccount($request->get('email'), $request->get('code')); |
172
|
|
|
|
173
|
|
|
if (!$activated) { |
174
|
|
|
return $this->userErrorResponse(['code' => TranslationCode::ERROR_CODE_INVALID]); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
DB::commit(); |
178
|
|
|
|
179
|
|
|
return $this->successResponse(); |
180
|
|
|
} catch (Exception $e) { |
181
|
|
|
Log::error(LogService::getExceptionTraceAsString($e)); |
182
|
|
|
|
183
|
|
|
return $this->errorResponse(); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Resend activation code |
189
|
|
|
* |
190
|
|
|
* @param Request $request |
191
|
|
|
* |
192
|
|
|
* @return JsonResponse |
193
|
|
|
*/ |
194
|
|
|
public function resendActivationCode(Request $request) |
195
|
|
|
{ |
196
|
|
|
try { |
197
|
|
|
$validator = $this->userService->validateResendActivationCodeRequest($request); |
198
|
|
|
|
199
|
|
|
if (!$validator->passes()) { |
200
|
|
|
return $this->userErrorResponse($validator->messages()); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
DB::beginTransaction(); |
204
|
|
|
|
205
|
|
|
$error = $this->userService->resendRegisterMail($request, $this->baseService->getLanguage($request)); |
206
|
|
|
|
207
|
|
|
DB::commit(); |
208
|
|
|
|
209
|
|
|
if (!$error) { |
210
|
|
|
return $this->successResponse(); |
211
|
|
|
} else { |
212
|
|
|
return $this->userErrorResponse($error); |
213
|
|
|
} |
214
|
|
|
} catch (Exception $e) { |
215
|
|
|
Log::error(LogService::getExceptionTraceAsString($e)); |
216
|
|
|
|
217
|
|
|
return $this->errorResponse(); |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Get logged user |
223
|
|
|
* |
224
|
|
|
* @return JsonResponse |
225
|
|
|
*/ |
226
|
|
|
public function getUser() |
227
|
|
|
{ |
228
|
|
|
try { |
229
|
|
|
$user = Auth::user(); |
230
|
|
|
|
231
|
|
|
return $this->successResponse($user); |
232
|
|
|
} catch (Exception $e) { |
233
|
|
|
Log::error(LogService::getExceptionTraceAsString($e)); |
234
|
|
|
|
235
|
|
|
return $this->errorResponse(); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Update profile |
242
|
|
|
* |
243
|
|
|
* @param Request $request |
244
|
|
|
* |
245
|
|
|
* @return JsonResponse |
246
|
|
|
*/ |
247
|
|
|
public function updateUser(Request $request) |
248
|
|
|
{ |
249
|
|
|
try { |
250
|
|
|
/** @var User $user */ |
251
|
|
|
$user = Auth::user(); |
252
|
|
|
|
253
|
|
|
$validator = $this->userService->validateUpdateUserRequest($request); |
254
|
|
|
|
255
|
|
|
if (!$validator->passes()) { |
256
|
|
|
return $this->userErrorResponse($validator->messages()); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
$email = $request->get('email'); |
260
|
|
|
|
261
|
|
|
if ($user->email !== $email) { |
262
|
|
|
/** @var User|null $userExists */ |
263
|
|
|
$userExists = User::whereEncrypted('email', $email)->first(); |
264
|
|
|
|
265
|
|
|
if ($userExists) { |
266
|
|
|
return $this->userErrorResponse(['email' => TranslationCode::ERROR_EMAIL_REGISTERED]); |
267
|
|
|
} |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
if ($request->has('newPassword') && !app('hash')->check($request->get('oldPassword'), $user->password)) { |
271
|
|
|
return $this->userErrorResponse(['oldPassword' => TranslationCode::ERROR_OLD_PASSWORD_WRONG]); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
DB::beginTransaction(); |
275
|
|
|
|
276
|
|
|
$this->userService->updateLoggedUser($user, $request); |
277
|
|
|
|
278
|
|
|
DB::commit(); |
279
|
|
|
|
280
|
|
|
return $this->successResponse($user); |
281
|
|
|
} catch (Exception $e) { |
282
|
|
|
Log::error(LogService::getExceptionTraceAsString($e)); |
283
|
|
|
|
284
|
|
|
return $this->errorResponse(); |
285
|
|
|
} |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* Change picture |
290
|
|
|
* |
291
|
|
|
* @param Request $request |
292
|
|
|
* |
293
|
|
|
* @return JsonResponse |
294
|
|
|
*/ |
295
|
|
|
public function changeUserPicture(Request $request) |
296
|
|
|
{ |
297
|
|
|
try { |
298
|
|
|
$validator = $this->userService->validateUpdateUserPictureRequest($request); |
299
|
|
|
|
300
|
|
|
if (!$validator->passes()) { |
301
|
|
|
return $this->userErrorResponse($validator->messages()); |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
DB::beginTransaction(); |
305
|
|
|
|
306
|
|
|
$this->userService->updateLoggedUserPicture($request->file('picture')); |
307
|
|
|
|
308
|
|
|
DB::commit(); |
309
|
|
|
|
310
|
|
|
return $this->successResponse(); |
311
|
|
|
} catch (Exception $e) { |
312
|
|
|
Log::error(LogService::getExceptionTraceAsString($e)); |
313
|
|
|
|
314
|
|
|
return $this->errorResponse(); |
315
|
|
|
} |
316
|
|
|
} |
317
|
|
|
} |
318
|
|
|
|