UserController::resendActivationCode()   A
last analyzed

Complexity

Conditions 4
Paths 11

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 14
c 1
b 0
f 0
nc 11
nop 1
dl 0
loc 24
rs 9.7998
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 Illuminate\Http\JsonResponse;
11
use Illuminate\Http\Request;
12
use Illuminate\Support\Facades\Auth;
13
use Illuminate\Support\Facades\DB;
14
use Illuminate\Support\Facades\Hash;
15
use Illuminate\Support\Facades\Log;
16
use Throwable;
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()->toArray());
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 (Throwable $t) {
64
            Log::error(LogService::getThrowableTraceAsString($t, $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()->toArray());
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 (Throwable $t) {
105
            Log::error(LogService::getThrowableTraceAsString($t, $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()->toArray());
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) {
0 ignored issues
show
introduced by
$user is of type App\Models\User, thus it always evaluated to true.
Loading history...
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 (Throwable $t) {
148
            Log::error(LogService::getThrowableTraceAsString($t, $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()->toArray());
168
            }
169
170
            DB::beginTransaction();
171
172
            $user = User::whereEncrypted('email', $request->get('email'))->first();
173
174
            if ($user->status === User::STATUS_CONFIRMED) {
0 ignored issues
show
Bug introduced by
The property status does not seem to exist on IonGhitun\MysqlEncryption\Models\BaseModel. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
175
                return $this->userErrorResponse(['account' => TranslationCode::ERROR_ACTIVATE_ACCOUNT_ACTIVATED]);
176
            }
177
178
            $activated = $this->userService->activateUserAccount($request->get('email'), $request->get('code'));
179
180
            if (!$activated) {
0 ignored issues
show
introduced by
The condition $activated is always true.
Loading history...
181
                return $this->userErrorResponse(['code' => TranslationCode::ERROR_ACTIVATE_CODE_WRONG]);
182
            }
183
184
            DB::commit();
185
186
            return $this->successResponse();
187
        } catch (Throwable $t) {
188
            Log::error(LogService::getThrowableTraceAsString($t, $request));
189
190
            return $this->errorResponse();
191
        }
192
    }
193
194
    /**
195
     * Resend activation code
196
     *
197
     * @param  Request  $request
198
     *
199
     * @return JsonResponse
200
     */
201
    public function resendActivationCode(Request $request)
202
    {
203
        try {
204
            $validator = $this->userService->validateResendActivationCodeRequest($request);
205
206
            if (!$validator->passes()) {
207
                return $this->userErrorResponse($validator->messages()->toArray());
208
            }
209
210
            DB::beginTransaction();
211
212
            $error = $this->userService->resendRegisterMail($request);
213
214
            DB::commit();
215
216
            if (!$error) {
217
                return $this->successResponse();
218
            } else {
219
                return $this->userErrorResponse($error);
220
            }
221
        } catch (Throwable $t) {
222
            Log::error(LogService::getThrowableTraceAsString($t, $request));
223
224
            return $this->errorResponse();
225
        }
226
    }
227
228
    /**
229
     * Get logged user
230
     *
231
     * @return JsonResponse
232
     */
233
    public function getLoggedUser()
234
    {
235
        try {
236
            /** @var User $user */
237
            $user = Auth::user();
238
239
            $userData = $this->userService->generateLoginData($user);
240
241
            return $this->successResponse($userData);
242
        } catch (Throwable $t) {
243
            Log::error(LogService::getThrowableTraceAsString($t));
244
245
            return $this->errorResponse();
246
        }
247
    }
248
249
    /**
250
     * Update profile
251
     *
252
     * @param  Request  $request
253
     *
254
     * @return JsonResponse
255
     */
256
    public function updateLoggedUser(Request $request)
257
    {
258
        try {
259
            /** @var User $user */
260
            $user = Auth::user();
261
262
            $validator = $this->userService->validateUpdateUserRequest($request);
263
264
            if (!$validator->passes()) {
265
                return $this->userErrorResponse($validator->messages()->toArray());
266
            }
267
268
            $email = $request->get('email');
269
270
            if ($user->email !== $email) {
271
                $userExists = User::whereEncrypted('email', $email)->first();
272
273
                if ($userExists) {
274
                    return $this->userErrorResponse(['email' => TranslationCode::ERROR_UPDATE_EMAIL_REGISTERED]);
275
                }
276
            }
277
278
            if ($request->has('newPassword') && !app('hash')->check($request->get('oldPassword'), $user->password)) {
279
                return $this->userErrorResponse(['oldPassword' => TranslationCode::ERROR_UPDATE_OLD_PASSWORD_WRONG]);
280
            }
281
282
            DB::beginTransaction();
283
284
            $this->userService->updateLoggedUser($user, $request, $this->baseService->getLanguage($request));
285
286
            DB::commit();
287
288
            return $this->successResponse($user);
289
        } catch (Throwable $t) {
290
            Log::error(LogService::getThrowableTraceAsString($t, $request));
291
292
            return $this->errorResponse();
293
        }
294
    }
295
296
    /**
297
     * Change picture
298
     *
299
     * @param  Request  $request
300
     *
301
     * @return JsonResponse
302
     */
303
    public function changeLoggedUserPicture(Request $request)
304
    {
305
        try {
306
            /** @var User $user */
307
            $user = Auth::user();
308
309
            $validator = $this->userService->validateUpdateUserPictureRequest($request);
310
311
            if (!$validator->passes()) {
312
                return $this->userErrorResponse($validator->messages()->toArray());
313
            }
314
315
            DB::beginTransaction();
316
317
            $this->userService->updateLoggedUserPicture($user, $request->file('picture'));
318
319
            DB::commit();
320
321
            return $this->successResponse($user);
322
        } catch (Throwable $t) {
323
            Log::error(LogService::getThrowableTraceAsString($t, $request));
324
325
            return $this->errorResponse();
326
        }
327
    }
328
}
329