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

120 lines
3.4 KiB
PHP
Raw Normal View History

2024-07-05 16:34:57 +02:00
<?php
namespace App\Http\Controllers\Club\Auth;
use App\Http\Controllers\Controller;
use App\Models\Customer;
2024-07-05 16:34:57 +02:00
use Illuminate\Auth\Events\PasswordReset;
use Illuminate\Foundation\Auth\ResetsPasswords;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Password;
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.
*
* @return \Illuminate\Contracts\Auth\StatefulGuard
*/
protected function guard(): \Illuminate\Contracts\Auth\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.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Contracts\View\View
*/
public function showResetForm(Request $request): \Illuminate\Contracts\View\View
2024-07-05 16:34:57 +02:00
{
$token = $request->route()->parameter('token');
return view('club.auth.passwords.reset')->with([
'token' => $token,
'email' => $request->email
]);
2024-07-05 16:34:57 +02:00
}
/**
* Reset the given user's password.
*
* @param \App\Models\Customer $customer
* @param string $password
2024-07-05 16:34:57 +02:00
* @return void
* @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 {
if (!($crmCustomer = $crm->getCustomer($customer->hashcode))) {
throw ValidationException::withMessages([
'error' => trans('Changement impossible, compte inexistant.')
]);
}
$crmCustomer->password = $password;
$crmCustomer->save();
} catch (\TypeError | CrmException | ApiErrorException $e) {
report($e);
throw ValidationException::withMessages([
'error' => trans('Une erreur est survenue, merci de réessayer ultérieurement.')
]);
} catch (ExceptionsValidationException $e) {
throw ValidationException::withMessages([
'password' => $e->getMessage()
]);
}
2024-07-05 16:34:57 +02:00
$customer->setRememberToken(Str::random(60));
$customer->saveOrFail();
event(new PasswordReset($customer));
$this->guard()->login($customer);
}
/**
* Get the post register / login redirect path.
*
* @return string
*/
public function redirectPath(): string
{
return $this->authLoggedRedirectPath();
}
/**
* Get the broker to be used during password reset.
*
* @return \Illuminate\Contracts\Auth\PasswordBroker
*/
public function broker(): \Illuminate\Contracts\Auth\PasswordBroker
2024-07-05 16:34:57 +02:00
{
return Password::broker('customers');
}
}