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()->all()); |
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, $request)); |
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()->all()); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** @var User $user */ |
87
|
|
|
$user = User::whereEncrypted('email', $request->get('email'))->first(); |
88
|
|
|
|
89
|
|
|
if ($user->status === User::STATUS_UNCONFIRMED) { |
90
|
|
|
return $this->userErrorResponse(['account' => TranslationCode::ERROR_FORGOT_ACCOUNT_UNACTIVATED]); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
if ($user->updated_at->addMinute() > Carbon::now()) { |
94
|
|
|
return $this->userErrorResponse(['forgot' => TranslationCode::ERROR_FORGOT_CODE_SEND_COOLDOWN]); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
DB::beginTransaction(); |
98
|
|
|
|
99
|
|
|
$this->userService->sendForgotPasswordCode($user, $user->language); |
100
|
|
|
|
101
|
|
|
DB::commit(); |
102
|
|
|
|
103
|
|
|
return $this->successResponse(); |
104
|
|
|
} catch (Exception $e) { |
105
|
|
|
Log::error(LogService::getExceptionTraceAsString($e, $request)); |
106
|
|
|
|
107
|
|
|
return $this->errorResponse(); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Change password with generated code |
113
|
|
|
* |
114
|
|
|
* @param Request $request |
115
|
|
|
* |
116
|
|
|
* @return JsonResponse |
117
|
|
|
*/ |
118
|
|
|
public function changePassword(Request $request) |
119
|
|
|
{ |
120
|
|
|
try { |
121
|
|
|
$validator = $this->userService->validateChangePasswordRequest($request); |
122
|
|
|
|
123
|
|
|
if (!$validator->passes()) { |
124
|
|
|
return $this->userErrorResponse($validator->messages()->all()); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** @var User|null $user */ |
128
|
|
|
$user = User::whereEncrypted('email', $request->get('email')) |
129
|
|
|
->where('forgot_code', $request->get('code')) |
130
|
|
|
->first(); |
131
|
|
|
|
132
|
|
|
if (!$user) { |
133
|
|
|
return $this->userErrorResponse(['forgot' => TranslationCode::ERROR_FORGOT_CODE_INVALID]); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
if ($user->forgot_time->addHour() < Carbon::now()) { |
137
|
|
|
return $this->userErrorResponse(['forgot' => TranslationCode::ERROR_FORGOT_PASSED_1H]); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
DB::beginTransaction(); |
141
|
|
|
|
142
|
|
|
$this->userService->updatePassword($user, $request->get('password')); |
143
|
|
|
|
144
|
|
|
DB::commit(); |
145
|
|
|
|
146
|
|
|
return $this->successResponse(); |
147
|
|
|
} catch (Exception $e) { |
148
|
|
|
Log::error(LogService::getExceptionTraceAsString($e, $request)); |
149
|
|
|
|
150
|
|
|
return $this->errorResponse(); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Activate account |
156
|
|
|
* |
157
|
|
|
* @param Request $request |
158
|
|
|
* |
159
|
|
|
* @return JsonResponse |
160
|
|
|
*/ |
161
|
|
|
public function activateAccount(Request $request) |
162
|
|
|
{ |
163
|
|
|
try { |
164
|
|
|
$validator = $this->userService->validateActivateAccountOrChangeEmailRequest($request); |
165
|
|
|
|
166
|
|
|
if (!$validator->passes()) { |
167
|
|
|
return $this->userErrorResponse($validator->messages()->all()); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
DB::beginTransaction(); |
171
|
|
|
|
172
|
|
|
$activated = $this->userService->activateUserAccount($request->get('email'), $request->get('code')); |
173
|
|
|
|
174
|
|
|
if (!$activated) { |
175
|
|
|
return $this->userErrorResponse(['code' => TranslationCode::ERROR_ACTIVATE_CODE_WRONG]); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
DB::commit(); |
179
|
|
|
|
180
|
|
|
return $this->successResponse(); |
181
|
|
|
} catch (Exception $e) { |
182
|
|
|
Log::error(LogService::getExceptionTraceAsString($e, $request)); |
183
|
|
|
|
184
|
|
|
return $this->errorResponse(); |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Resend activation code |
190
|
|
|
* |
191
|
|
|
* @param Request $request |
192
|
|
|
* |
193
|
|
|
* @return JsonResponse |
194
|
|
|
*/ |
195
|
|
|
public function resendActivationCode(Request $request) |
196
|
|
|
{ |
197
|
|
|
try { |
198
|
|
|
$validator = $this->userService->validateResendActivationCodeRequest($request); |
199
|
|
|
|
200
|
|
|
if (!$validator->passes()) { |
201
|
|
|
return $this->userErrorResponse($validator->messages()->all()); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
DB::beginTransaction(); |
205
|
|
|
|
206
|
|
|
$error = $this->userService->resendRegisterMail($request); |
207
|
|
|
|
208
|
|
|
DB::commit(); |
209
|
|
|
|
210
|
|
|
if (!$error) { |
211
|
|
|
return $this->successResponse(); |
212
|
|
|
} else { |
213
|
|
|
return $this->userErrorResponse($error); |
214
|
|
|
} |
215
|
|
|
} catch (Exception $e) { |
216
|
|
|
Log::error(LogService::getExceptionTraceAsString($e, $request)); |
217
|
|
|
|
218
|
|
|
return $this->errorResponse(); |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Get logged user |
224
|
|
|
* |
225
|
|
|
* @return JsonResponse |
226
|
|
|
*/ |
227
|
|
|
public function getLoggedUser() |
228
|
|
|
{ |
229
|
|
|
try { |
230
|
|
|
return $this->successResponse(Auth::user()); |
|
|
|
|
231
|
|
|
} catch (Exception $e) { |
232
|
|
|
Log::error(LogService::getExceptionTraceAsString($e)); |
233
|
|
|
|
234
|
|
|
return $this->errorResponse(); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Update profile |
241
|
|
|
* |
242
|
|
|
* @param Request $request |
243
|
|
|
* |
244
|
|
|
* @return JsonResponse |
245
|
|
|
*/ |
246
|
|
|
public function updateLoggedUser(Request $request) |
247
|
|
|
{ |
248
|
|
|
try { |
249
|
|
|
/** @var User $user */ |
250
|
|
|
$user = Auth::user(); |
251
|
|
|
|
252
|
|
|
$validator = $this->userService->validateUpdateUserRequest($request); |
253
|
|
|
|
254
|
|
|
if (!$validator->passes()) { |
255
|
|
|
return $this->userErrorResponse($validator->messages()->all()); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
$email = $request->get('email'); |
259
|
|
|
|
260
|
|
|
if ($user->email !== $email) { |
261
|
|
|
/** @var User|null $userExists */ |
262
|
|
|
$userExists = User::whereEncrypted('email', $email)->first(); |
263
|
|
|
|
264
|
|
|
if ($userExists) { |
265
|
|
|
return $this->userErrorResponse(['email' => TranslationCode::ERROR_UPDATE_EMAIL_REGISTERED]); |
266
|
|
|
} |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
if ($request->has('newPassword') && !app('hash')->check($request->get('oldPassword'), $user->password)) { |
270
|
|
|
return $this->userErrorResponse(['oldPassword' => TranslationCode::ERROR_UPDATE_OLD_PASSWORD_WRONG]); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
DB::beginTransaction(); |
274
|
|
|
|
275
|
|
|
$this->userService->updateLoggedUser($user, $request, $this->baseService->getLanguage($request)); |
276
|
|
|
|
277
|
|
|
DB::commit(); |
278
|
|
|
|
279
|
|
|
return $this->successResponse($user); |
280
|
|
|
} catch (Exception $e) { |
281
|
|
|
Log::error(LogService::getExceptionTraceAsString($e, $request)); |
282
|
|
|
|
283
|
|
|
return $this->errorResponse(); |
284
|
|
|
} |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Change picture |
289
|
|
|
* |
290
|
|
|
* @param Request $request |
291
|
|
|
* |
292
|
|
|
* @return JsonResponse |
293
|
|
|
*/ |
294
|
|
|
public function changeLoggedUserPicture(Request $request) |
295
|
|
|
{ |
296
|
|
|
try { |
297
|
|
|
/** @var User $user */ |
298
|
|
|
$user = Auth::user(); |
299
|
|
|
|
300
|
|
|
$validator = $this->userService->validateUpdateUserPictureRequest($request); |
301
|
|
|
|
302
|
|
|
if (!$validator->passes()) { |
303
|
|
|
return $this->userErrorResponse($validator->messages()->all()); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
DB::beginTransaction(); |
307
|
|
|
|
308
|
|
|
$this->userService->updateLoggedUserPicture($user, $request->file('picture')); |
309
|
|
|
|
310
|
|
|
DB::commit(); |
311
|
|
|
|
312
|
|
|
return $this->successResponse($user); |
313
|
|
|
} catch (Exception $e) { |
314
|
|
|
Log::error(LogService::getExceptionTraceAsString($e, $request)); |
315
|
|
|
|
316
|
|
|
return $this->errorResponse(); |
317
|
|
|
} |
318
|
|
|
} |
319
|
|
|
} |
320
|
|
|
|