2FAuth/app/Http/Controllers/Auth/UserController.php

91 lines
3.0 KiB
PHP
Raw Normal View History

<?php
2022-03-15 14:47:07 +01:00
namespace App\Http\Controllers\Auth;
use App\Api\v1\Resources\UserResource;
use App\Http\Controllers\Controller;
2022-11-22 15:15:52 +01:00
use App\Http\Requests\UserDeleteRequest;
use App\Http\Requests\UserUpdateRequest;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Auth;
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;
class UserController extends Controller
2022-11-22 15:15:52 +01:00
{
/**
* Update the user's profile information.
*
2022-11-22 15:15:52 +01:00
* @param \App\Http\Requests\UserUpdateRequest $request
* @return \App\Api\v1\Resources\UserResource|\Illuminate\Http\JsonResponse
*/
public function update(UserUpdateRequest $request)
{
2022-11-22 15:15:52 +01:00
$user = $request->user();
$validated = $request->validated();
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
return response()->json(['message' => __('errors.wrong_current_password')], 400);
}
2022-11-22 15:15:52 +01:00
if (! config('2fauth.config.isDemoApp')) {
$user->update([
2022-11-22 15:15:52 +01:00
'name' => $validated['name'],
'email' => $validated['email'],
]);
2022-10-18 17:34:56 +02:00
}
Log::info('User account updated');
return new UserResource($user);
}
/**
* Delete the user's account.
*
2022-11-22 15:15:52 +01:00
* @param \App\Http\Requests\UserDeleteRequest $request
* @return \Illuminate\Http\JsonResponse
*/
public function delete(UserDeleteRequest $request)
{
2022-10-18 17:34:56 +02:00
Log::info('User deletion requested');
$validated = $request->validated();
2022-11-22 15:15:52 +01:00
if (! Hash::check($validated['password'], Auth::user()->password)) {
return response()->json(['message' => __('errors.wrong_current_password')], 400);
}
try {
DB::transaction(function () {
DB::table('twofaccounts')->delete();
DB::table('groups')->delete();
DB::table('options')->delete();
DB::table('webauthn_credentials')->delete();
DB::table('webauthn_recoveries')->delete();
DB::table('oauth_access_tokens')->delete();
DB::table('oauth_auth_codes')->delete();
DB::table('oauth_clients')->delete();
DB::table('oauth_personal_access_clients')->delete();
DB::table('oauth_refresh_tokens')->delete();
DB::table('password_resets')->delete();
DB::table('users')->delete();
});
Artisan::call('passport:install --force');
Artisan::call('config:clear');
}
2022-03-31 08:38:35 +02:00
// @codeCoverageIgnoreStart
catch (\Throwable $e) {
2022-10-18 17:34:56 +02:00
Log::error('User deletion failed');
2022-11-22 15:15:52 +01:00
return response()->json(['message' => __('errors.user_deletion_failed')], 400);
}
2022-03-31 08:38:35 +02:00
// @codeCoverageIgnoreEnd
2022-10-18 17:34:56 +02:00
Log::info('User deleted');
return response()->json(null, 204);
}
2022-11-22 15:15:52 +01:00
}