2021-10-01 13:40:37 +02:00
|
|
|
<?php
|
|
|
|
|
2022-03-15 14:47:07 +01:00
|
|
|
namespace App\Http\Controllers\Auth;
|
2021-10-01 13:40:37 +02:00
|
|
|
|
2021-11-07 21:57:22 +01:00
|
|
|
use App\Api\v1\Resources\UserResource;
|
2021-10-01 13:40:37 +02:00
|
|
|
use App\Http\Controllers\Controller;
|
2022-11-22 15:15:52 +01:00
|
|
|
use App\Http\Requests\UserDeleteRequest;
|
|
|
|
use App\Http\Requests\UserUpdateRequest;
|
2023-03-10 16:02:56 +01:00
|
|
|
use App\Models\User;
|
2021-10-01 13:40:37 +02:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
2022-03-28 13:45:19 +02:00
|
|
|
use Illuminate\Support\Facades\DB;
|
2022-11-22 15:15:52 +01:00
|
|
|
use Illuminate\Support\Facades\Hash;
|
2022-10-18 17:34:56 +02:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2021-10-01 13:40:37 +02:00
|
|
|
|
|
|
|
class UserController extends Controller
|
2022-11-22 15:15:52 +01:00
|
|
|
{
|
2021-10-01 13:40:37 +02:00
|
|
|
/**
|
|
|
|
* Update the user's profile information.
|
|
|
|
*
|
2022-08-26 15:57:18 +02:00
|
|
|
* @return \App\Api\v1\Resources\UserResource|\Illuminate\Http\JsonResponse
|
2021-10-01 13:40:37 +02:00
|
|
|
*/
|
|
|
|
public function update(UserUpdateRequest $request)
|
|
|
|
{
|
2022-11-22 15:15:52 +01:00
|
|
|
$user = $request->user();
|
2021-10-01 13:40:37 +02:00
|
|
|
$validated = $request->validated();
|
|
|
|
|
2023-12-09 17:22:24 +01:00
|
|
|
if (config('auth.defaults.guard') === 'reverse-proxy-guard' || $user->oauth_provider) {
|
|
|
|
Log::notice('Account update rejected: reverse-proxy-guard enabled or account from external sso provider');
|
|
|
|
|
|
|
|
return response()->json(['message' => __('errors.account_managed_by_external_provider')], 400);
|
|
|
|
}
|
|
|
|
|
2022-11-22 15:15:52 +01:00
|
|
|
if (! Hash::check($request->password, Auth::user()->password)) {
|
2022-10-18 17:34:56 +02:00
|
|
|
Log::notice('Account update failed: wrong password provided');
|
2022-11-22 15:15:52 +01:00
|
|
|
|
2021-10-01 13:40:37 +02:00
|
|
|
return response()->json(['message' => __('errors.wrong_current_password')], 400);
|
|
|
|
}
|
|
|
|
|
2022-11-22 15:15:52 +01:00
|
|
|
if (! config('2fauth.config.isDemoApp')) {
|
2022-09-07 17:54:27 +02:00
|
|
|
$user->update([
|
2022-11-22 15:15:52 +01:00
|
|
|
'name' => $validated['name'],
|
2021-10-01 13:40:37 +02:00
|
|
|
'email' => $validated['email'],
|
|
|
|
]);
|
2022-10-18 17:34:56 +02:00
|
|
|
}
|
2023-02-27 00:33:42 +01:00
|
|
|
Log::info(sprintf('Account of user ID #%s updated', $user->id));
|
2021-10-01 13:40:37 +02:00
|
|
|
|
|
|
|
return new UserResource($user);
|
|
|
|
}
|
2022-03-28 13:45:19 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete the user's account.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
*/
|
|
|
|
public function delete(UserDeleteRequest $request)
|
|
|
|
{
|
|
|
|
$validated = $request->validated();
|
2023-03-10 22:59:46 +01:00
|
|
|
$user = Auth::user();
|
2022-03-28 13:45:19 +02:00
|
|
|
|
2023-03-10 16:02:56 +01:00
|
|
|
Log::info(sprintf('Deletion of user ID #%s requested', $user->id));
|
|
|
|
|
|
|
|
if ($user->is_admin && User::admins()->count() == 1) {
|
|
|
|
return response()->json(['message' => __('errors.cannot_delete_the_only_admin')], 400);
|
|
|
|
}
|
2023-02-21 14:09:36 +01:00
|
|
|
|
2022-11-22 15:15:52 +01:00
|
|
|
if (! Hash::check($validated['password'], Auth::user()->password)) {
|
2022-03-28 13:45:19 +02:00
|
|
|
return response()->json(['message' => __('errors.wrong_current_password')], 400);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2023-03-10 16:02:56 +01:00
|
|
|
DB::transaction(function () use ($user) {
|
|
|
|
DB::table('twofaccounts')->where('user_id', $user->id)->delete();
|
|
|
|
DB::table('groups')->where('user_id', $user->id)->delete();
|
|
|
|
DB::table('webauthn_credentials')->where('authenticatable_id', $user->id)->delete();
|
|
|
|
DB::table('webauthn_recoveries')->where('email', $user->email)->delete();
|
|
|
|
DB::table('oauth_access_tokens')->where('user_id', $user->id)->delete();
|
|
|
|
DB::table('password_resets')->where('email', $user->email)->delete();
|
|
|
|
DB::table('users')->where('id', $user->id)->delete();
|
2022-03-28 13:45:19 +02:00
|
|
|
});
|
|
|
|
}
|
2022-03-31 08:38:35 +02:00
|
|
|
// @codeCoverageIgnoreStart
|
2022-03-28 13:45:19 +02:00
|
|
|
catch (\Throwable $e) {
|
2023-03-10 16:02:56 +01:00
|
|
|
Log::error(sprintf('Deletion of user ID #%s failed, transaction has been rolled-back', $user->id));
|
2022-11-22 15:15:52 +01:00
|
|
|
|
2022-03-28 13:45:19 +02:00
|
|
|
return response()->json(['message' => __('errors.user_deletion_failed')], 400);
|
|
|
|
}
|
2022-03-31 08:38:35 +02:00
|
|
|
// @codeCoverageIgnoreEnd
|
2023-03-10 22:59:46 +01:00
|
|
|
|
2023-03-10 16:02:56 +01:00
|
|
|
Log::info(sprintf('User ID #%s deleted', $user->id));
|
2022-03-28 13:45:19 +02:00
|
|
|
|
|
|
|
return response()->json(null, 204);
|
|
|
|
}
|
2022-11-22 15:15:52 +01:00
|
|
|
}
|