diff --git a/app/Http/Controllers/Auth/UserController.php b/app/Http/Controllers/Auth/UserController.php index 88fbd907..5e6b0f37 100644 --- a/app/Http/Controllers/Auth/UserController.php +++ b/app/Http/Controllers/Auth/UserController.php @@ -2,20 +2,36 @@ namespace App\Http\Controllers\Auth; +use App\Models\User; +use App\Services\TwoFAccountService; use App\Http\Requests\UserUpdateRequest; +use App\Http\Requests\UserDeleteRequest; use App\Api\v1\Resources\UserResource; use App\Http\Controllers\Controller; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Hash; +use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\Artisan; use App\Exceptions\UnsupportedWithReverseProxyException; +use Exception; class UserController extends Controller { /** - * Create a new controller instance. + * The TwoFAccount Service instance. */ - public function __construct() + protected $twofaccountService; + + + /** + * Create a new controller instance. + * + * @param \App\Services\TwoFAccountService $twofaccountService + * @return void + */ + public function __construct(TwoFAccountService $twofaccountService) { + $this->twofaccountService = $twofaccountService; $authGuard = config('auth.defaults.guard'); if ($authGuard === 'reverse-proxy-guard') { @@ -27,7 +43,7 @@ public function __construct() /** * Update the user's profile information. * - * @param \App\Api\v1\Requests\UserUpdateRequest $request + * @param \App\Http\Requests\UserUpdateRequest $request * @return \App\Api\v1\Resources\UserResource */ public function update(UserUpdateRequest $request) @@ -48,4 +64,45 @@ public function update(UserUpdateRequest $request) return new UserResource($user); } + + + /** + * Delete the user's account. + * + * @param \App\Http\Requests\UserDeleteRequest $request + * @return \Illuminate\Http\JsonResponse + */ + public function delete(UserDeleteRequest $request) + { + $validated = $request->validated(); + + 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('web_authn_credentials')->delete(); + DB::table('web_authn_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'); + } + catch (\Throwable $e) { + return response()->json(['message' => __('errors.user_deletion_failed')], 400); + } + + return response()->json(null, 204); + } } \ No newline at end of file diff --git a/app/Http/Requests/UserDeleteRequest.php b/app/Http/Requests/UserDeleteRequest.php new file mode 100644 index 00000000..d6326bbe --- /dev/null +++ b/app/Http/Requests/UserDeleteRequest.php @@ -0,0 +1,32 @@ + 'required|string', + ]; + } +} diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index ef5239da..c303a8eb 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -6,6 +6,9 @@ use Illuminate\Support\Facades\Schema; use Illuminate\Support\ServiceProvider; use Illuminate\Http\Resources\Json\JsonResource; +use Laravel\Passport\Console\ClientCommand; +use Laravel\Passport\Console\InstallCommand; +use Laravel\Passport\Console\KeysCommand; class AppServiceProvider extends ServiceProvider @@ -30,5 +33,11 @@ public function boot() Blade::withoutComponentTags(); Schema::defaultStringLength(191); JsonResource::withoutWrapping(); + + $this->commands([ + InstallCommand::class, + ClientCommand::class, + KeysCommand::class, + ]); } } diff --git a/resources/js/components/FormButtons.vue b/resources/js/components/FormButtons.vue index 472caa50..2568f44a 100644 --- a/resources/js/components/FormButtons.vue +++ b/resources/js/components/FormButtons.vue @@ -1,7 +1,7 @@