ldc-legaulois/app/Http/Controllers/Club/Auth/ResetPasswordController.php

113 lines
3.3 KiB
PHP
Raw Permalink Normal View History

2024-07-05 16:34:57 +02:00
<?php
namespace App\Http\Controllers\Club\Auth;
use App\Events\CustomerPasswordReset;
2024-07-05 16:34:57 +02:00
use App\Http\Controllers\Controller;
use App\Models\Customer;
2026-09-25 16:25:31 +02:00
use Illuminate\Contracts\Auth\PasswordBroker;
use Illuminate\Contracts\Auth\StatefulGuard;
use Illuminate\Contracts\View\View;
2024-07-05 16:34:57 +02:00
use Illuminate\Foundation\Auth\ResetsPasswords;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Password;
use Illuminate\Support\Facades\Session;
2024-07-05 16:34:57 +02:00
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;
use LDC\LDCCRM\Exceptions\ApiErrorException;
use LDC\LDCCRM\Exceptions\CrmException;
use LDC\LDCCRM\Exceptions\ValidationException as ExceptionsValidationException;
2024-07-05 16:34:57 +02:00
class ResetPasswordController extends Controller
{
use ResetsPasswords;
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest:club');
}
/**
* Get the guard to be used during authentication.
*/
2026-09-25 16:25:31 +02:00
protected function guard(): StatefulGuard
2024-07-05 16:34:57 +02:00
{
return Auth::guard('club');
}
/**
* Display the password reset view for the given token.
*
* If no token is present, display the link request form.
*/
2026-09-25 16:25:31 +02:00
public function showResetForm(Request $request): View
2024-07-05 16:34:57 +02:00
{
$token = $request->route()->parameter('token');
return view('club.auth.passwords.reset')->with([
'token' => $token,
2026-09-25 16:25:31 +02:00
'email' => $request->email,
]);
2024-07-05 16:34:57 +02:00
}
/**
* Reset the given user's password.
*
* @throws \Illuminate\Validation\ValidationException If something goes wrong.
*/
protected function resetPassword(Customer $customer, string $password): void
2024-07-05 16:34:57 +02:00
{
/** @var \LDC\LDCCRM\CrmApiV4 */
$crm = app('crmAPI');
2024-07-05 16:34:57 +02:00
try {
2026-09-25 16:25:31 +02:00
if (! ($crmCustomer = $crm->getCustomer($customer->hashcode))) {
throw ValidationException::withMessages([
2026-09-25 16:25:31 +02:00
'error' => trans('Changement impossible, compte inexistant.'),
]);
}
$crmCustomer->password = $password;
$crmCustomer->save();
} catch (\TypeError | CrmException | ApiErrorException $e) {
report($e);
throw ValidationException::withMessages([
2026-09-25 16:25:31 +02:00
'error' => trans('Une erreur est survenue, merci de réessayer ultérieurement.'),
]);
} catch (ExceptionsValidationException $e) {
throw ValidationException::withMessages([
2026-09-25 16:25:31 +02:00
'password' => $e->getMessage(),
]);
}
2024-07-05 16:34:57 +02:00
$customer->setRememberToken(Str::random(60));
$customer->saveOrFail();
event(new CustomerPasswordReset($customer, $customer->api_data->firstname));
2024-07-05 16:34:57 +02:00
$this->guard()->login($customer);
Session::flash('success', trans('Votre mot de passe à bien été réinitialisé, vous êtes à présent connecté.'));
2024-07-05 16:34:57 +02:00
}
/**
* Get the post register / login redirect path.
*/
public function redirectPath(): string
{
return $this->authLoggedRedirectPath();
}
/**
* Get the broker to be used during password reset.
*/
2026-09-25 16:25:31 +02:00
public function broker(): PasswordBroker
2024-07-05 16:34:57 +02:00
{
return Password::broker('customers');
}
}