2021-10-21 10:35:37 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
|
|
use App\Http\Requests\CustomerRegisterRequest;
|
|
|
|
|
use App\Http\Requests\CustomerUpdatePassword;
|
|
|
|
|
use App\Http\Requests\LoginRequest;
|
|
|
|
|
use App\Http\Requests\ResetCustomerPassword;
|
2021-12-03 10:55:30 +01:00
|
|
|
use App\Http\Requests\UpdateInfosRequest;
|
2021-10-21 10:35:37 +02:00
|
|
|
use App\Lib\Utils;
|
|
|
|
|
use App\Mail\PasswordResetMail;
|
|
|
|
|
use App\Mail\RegisterConfirmMail;
|
|
|
|
|
use App\Models\Customers;
|
|
|
|
|
use App\Models\PasswordReset;
|
|
|
|
|
use Illuminate\Http\RedirectResponse;
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
|
use Illuminate\Support\Facades\Mail;
|
|
|
|
|
use Illuminate\Support\Facades\Session;
|
2021-12-03 10:55:30 +01:00
|
|
|
use Illuminate\Support\Facades\Validator;
|
2021-10-21 10:35:37 +02:00
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
use LDC\LDCCRM\Enums\ApiError;
|
|
|
|
|
use LDC\LDCCRM\Exceptions\ApiErrorException;
|
|
|
|
|
use LDC\LDCCRM\Exceptions\CrmException;
|
|
|
|
|
use LDC\LDCCRM\Exceptions\ValidationException;
|
|
|
|
|
use LDC\LDCCRM\Models\Customer as CrmCustomer;
|
|
|
|
|
|
|
|
|
|
class AuthController extends Controller
|
|
|
|
|
{
|
2021-10-21 14:55:30 +02:00
|
|
|
/**
|
|
|
|
|
* Affiche le formulaire utilise view share vars
|
2023-01-10 11:09:36 +01:00
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Contracts\View\View|\Illuminate\Contracts\View\Factory
|
2021-10-21 14:55:30 +02:00
|
|
|
*/
|
|
|
|
|
public function register()
|
|
|
|
|
{
|
|
|
|
|
return view('front.auth.register');
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-21 10:35:37 +02:00
|
|
|
/**
|
|
|
|
|
* Enregistre le customer dans la base de données
|
2023-01-11 17:45:12 +01:00
|
|
|
* @param \App\Http\Requests\CustomerRegisterRequest $request
|
|
|
|
|
* @return \Illuminate\Contracts\View\View|\Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse
|
2021-10-21 10:35:37 +02:00
|
|
|
*/
|
|
|
|
|
public function registerSubmit(CustomerRegisterRequest $request)
|
|
|
|
|
{
|
2021-12-02 10:32:42 +01:00
|
|
|
$crmCustomer = null;
|
2021-10-21 10:35:37 +02:00
|
|
|
try {
|
2022-02-10 16:14:58 +01:00
|
|
|
$crmCustomer = new CrmCustomer(\array_intersect_key(
|
2021-10-21 10:35:37 +02:00
|
|
|
$request->all(),
|
|
|
|
|
[
|
|
|
|
|
'firstname' => '',
|
|
|
|
|
'lastname' => '',
|
|
|
|
|
'birth_date' => '',
|
|
|
|
|
'address' => '',
|
|
|
|
|
'postcode' => '',
|
|
|
|
|
'country' => '',
|
|
|
|
|
'city' => '',
|
|
|
|
|
'phone' => '',
|
|
|
|
|
'email' => '',
|
|
|
|
|
'password' => '',
|
|
|
|
|
'opt_in' => '',
|
|
|
|
|
]
|
|
|
|
|
));
|
|
|
|
|
$crmCustomer->password = $request->password;
|
|
|
|
|
$crmCustomer->save();
|
2021-12-02 10:32:42 +01:00
|
|
|
|
2022-02-10 16:14:58 +01:00
|
|
|
$customer = new Customers($request->all());
|
2021-12-02 10:32:42 +01:00
|
|
|
$customer->hashcode = $crmCustomer->hashcode;
|
|
|
|
|
$customer->save();
|
|
|
|
|
|
|
|
|
|
Mail::to($request->email)
|
|
|
|
|
->send(new RegisterConfirmMail($customer, $crmCustomer->firstname));
|
|
|
|
|
|
|
|
|
|
return view('front.auth.confirm-register');
|
2021-10-21 10:35:37 +02:00
|
|
|
} catch (ApiErrorException $e) {
|
|
|
|
|
if ($e->getApiError()->equals(ApiError::customerExists())) {
|
|
|
|
|
return back()->withErrors('Un compte est déjà associé à cette adresse email.');
|
|
|
|
|
}
|
2021-12-08 12:22:52 +01:00
|
|
|
if ($e->getApiError()->equals(ApiError::validationFailed())) {
|
|
|
|
|
return back()->withErrors($e->getMessage());
|
|
|
|
|
}
|
2023-01-10 11:09:36 +01:00
|
|
|
if (app()->bound('sentry')) {
|
|
|
|
|
app('sentry')->captureException($e);
|
|
|
|
|
}
|
2021-10-21 10:35:37 +02:00
|
|
|
return back()->withErrors('Une erreur est survenue, merci de réessayer ultérieurement.');
|
|
|
|
|
} catch (ValidationException $e) {
|
|
|
|
|
return back()->withErrors($e->getMessage());
|
2021-12-02 10:32:42 +01:00
|
|
|
} catch (\Exception | \TypeError | CrmException $e) {
|
2023-01-10 11:09:36 +01:00
|
|
|
if (app()->bound('sentry')) {
|
|
|
|
|
app('sentry')->captureException($e);
|
|
|
|
|
}
|
|
|
|
|
// Une erreur critique est survenue avec la CRM..
|
2021-12-02 10:32:42 +01:00
|
|
|
return back()->withErrors('Une erreur est survenue, merci de réessayer ultérieurement.');
|
2022-03-10 13:31:10 +00:00
|
|
|
} //end try
|
2021-12-02 10:32:42 +01:00
|
|
|
return back()->withErrors('Une erreur est survenue, merci de réessayer ultérieurement.');
|
2021-10-21 10:35:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Active le compte d'un customer
|
2023-01-10 11:09:36 +01:00
|
|
|
*
|
|
|
|
|
* @param string $hashcode
|
|
|
|
|
* @return \Illuminate\Contracts\View\View|\Illuminate\Contracts\View\Factory
|
2021-10-21 10:35:37 +02:00
|
|
|
*/
|
|
|
|
|
public function activate(string $hashcode)
|
|
|
|
|
{
|
|
|
|
|
$hashcode = filter_var($hashcode, FILTER_SANITIZE_STRING);
|
|
|
|
|
if (!($customer = Customers::select('hashcode')->where('hashcode', $hashcode)->first())) {
|
|
|
|
|
return view('front.auth.activate')->with('message', "Ce lien d'activation n'est pas valide.");
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
/** @var \LDC\LDCCRM\Models\Customer */
|
|
|
|
|
$crmCustomer = $customer->apiData;
|
|
|
|
|
if ($crmCustomer->activate()) {
|
|
|
|
|
return view('front.auth.activate')
|
|
|
|
|
->with('message', 'Votre compte a bien été activé, vous pouvez désormais vous connecter.');
|
|
|
|
|
}
|
2021-12-01 08:11:18 +01:00
|
|
|
} catch (ApiErrorException $e) {
|
|
|
|
|
if ($e->getApiError()->equals(ApiError::customerAlreadyActive())) {
|
|
|
|
|
return view('front.auth.activate')
|
2023-01-10 11:09:36 +01:00
|
|
|
->with(
|
|
|
|
|
'message',
|
|
|
|
|
'Votre compte est déjà activé, vous pouvez dès à présent vous connecter avec celui-ci.'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
if (app()->bound('sentry')) {
|
|
|
|
|
app('sentry')->captureException($e);
|
2021-12-01 08:11:18 +01:00
|
|
|
}
|
2023-01-10 11:09:36 +01:00
|
|
|
// Une erreur critique est survenue avec la CRM.
|
2021-12-01 08:11:18 +01:00
|
|
|
return view('front.auth.activate')
|
2022-03-10 13:31:10 +00:00
|
|
|
->with('message', 'Une erreur est survenue, merci de réessayer ultérieurement.');
|
2021-12-01 08:11:18 +01:00
|
|
|
} catch (CrmException $e) {
|
2023-01-10 11:09:36 +01:00
|
|
|
if (app()->bound('sentry')) {
|
|
|
|
|
app('sentry')->captureException($e);
|
|
|
|
|
}
|
|
|
|
|
// Une erreur critique est survenue avec la CRM.
|
2021-10-21 10:35:37 +02:00
|
|
|
return view('front.auth.activate')
|
|
|
|
|
->with('message', 'Une erreur est survenue, merci de réessayer ultérieurement.');
|
2022-03-10 13:31:10 +00:00
|
|
|
} //end try
|
2021-10-21 10:35:37 +02:00
|
|
|
return view('front.auth.activate')->with('message', "Ce lien d'activation n'est pas valide.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Retourne la page de connexion
|
2023-01-10 11:09:36 +01:00
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
|
* @return \Illuminate\Contracts\View\View|\Illuminate\Contracts\View\Factory
|
2021-10-21 10:35:37 +02:00
|
|
|
*/
|
|
|
|
|
public function authenticate(Request $request)
|
|
|
|
|
{
|
|
|
|
|
if ($request->get('url')) {
|
|
|
|
|
Session::put('url.intended', $request->get('url'));
|
|
|
|
|
Session::save();
|
|
|
|
|
}
|
|
|
|
|
return view('front.auth.login');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gère l'authentification des customers
|
2023-01-10 11:09:36 +01:00
|
|
|
*
|
|
|
|
|
* @param \App\Http\Requests\LoginRequest $request
|
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
|
* @throws ValidationException ValidationException.
|
|
|
|
|
* @throws ApiErrorException ApiErrorException.
|
2021-10-21 10:35:37 +02:00
|
|
|
*/
|
|
|
|
|
public function authenticateSubmit(LoginRequest $request)
|
|
|
|
|
{
|
|
|
|
|
/** @var Customers|null */
|
|
|
|
|
$customer = Customers::where('email', $request->email)->first();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
/** @var \LDC\LDCCRM\CrmApiV2 */
|
|
|
|
|
$crm = app('crmAPI');
|
2021-12-01 16:00:05 +01:00
|
|
|
|
2023-01-10 11:09:36 +01:00
|
|
|
// If login debug mode.
|
2021-12-01 16:00:05 +01:00
|
|
|
if (config('app.debug') && config('crm.NOCRM')) {
|
|
|
|
|
$loginIsValid = true;
|
|
|
|
|
} else {
|
|
|
|
|
$loginIsValid = $crm->login($request->email, $request->password);
|
|
|
|
|
}
|
2021-10-21 10:35:37 +02:00
|
|
|
|
2023-01-10 11:09:36 +01:00
|
|
|
// Ne devrait pas survenir, $crm->login throws ApiError::authFailed().
|
2021-10-21 10:35:37 +02:00
|
|
|
if (!$loginIsValid) {
|
2023-01-10 11:09:36 +01:00
|
|
|
// Mauvais identifiants.
|
2021-10-21 10:35:37 +02:00
|
|
|
return back()->withErrors("Email ou mot de passe incorrect.");
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-10 11:09:36 +01:00
|
|
|
// Dans tout les cas : récupérer le crm user pour vérifier si il doit être complété.
|
2021-12-03 10:55:30 +01:00
|
|
|
$crmCustomer = $crm->getCustomer($request->email);
|
|
|
|
|
|
|
|
|
|
// Test is user data valid ?
|
|
|
|
|
try {
|
2023-01-10 11:09:36 +01:00
|
|
|
// On va tester de mettre a jour uniquement les champs.
|
|
|
|
|
// Requis pour mes br afin de detecter si ils doivent etre mis à jour.
|
|
|
|
|
// La validation va se faire par la crm (ou le client).
|
2021-12-03 10:55:30 +01:00
|
|
|
$crmCustomer->fill([
|
|
|
|
|
'birth_date' => $crmCustomer->birth_date,
|
|
|
|
|
'address' => $crmCustomer->address,
|
|
|
|
|
'postcode' => $crmCustomer->postcode,
|
|
|
|
|
'country' => $crmCustomer->country,
|
|
|
|
|
'city' => $crmCustomer->city
|
|
|
|
|
]);
|
|
|
|
|
|
2023-01-10 11:09:36 +01:00
|
|
|
// On valide les données selon les critères du site LG.
|
2022-02-10 16:14:58 +01:00
|
|
|
$rules = collect((new UpdateInfosRequest())->rules());
|
2021-12-03 10:55:30 +01:00
|
|
|
$validator = Validator::make(
|
|
|
|
|
[
|
|
|
|
|
'birth_date' => $crmCustomer->birth_date,
|
|
|
|
|
'address' => $crmCustomer->address,
|
|
|
|
|
'postcode' => $crmCustomer->postcode,
|
2021-12-03 11:11:31 +01:00
|
|
|
'country' => $crmCustomer->country->value,
|
2021-12-03 10:55:30 +01:00
|
|
|
'city' => $crmCustomer->city
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'birth_date' => $rules->get('birth_date'),
|
|
|
|
|
'address' => $rules->get('address'),
|
|
|
|
|
'postcode' => $rules->get('postcode'),
|
|
|
|
|
'country' => $rules->get('country'),
|
|
|
|
|
'city' => $rules->get('city')
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
if ($validator->fails()) {
|
|
|
|
|
throw new ValidationException();
|
2021-12-03 11:11:31 +01:00
|
|
|
} else {
|
|
|
|
|
Session::remove('needCrmInfoUpdate', true);
|
2021-12-03 10:55:30 +01:00
|
|
|
}
|
|
|
|
|
} catch (ValidationException $e) {
|
|
|
|
|
Session::push('needCrmInfoUpdate', true);
|
|
|
|
|
} catch (ApiErrorException $e) {
|
|
|
|
|
if (!$e->getApiError()->equals(ApiError::validationFailed())) {
|
|
|
|
|
throw $e;
|
|
|
|
|
}
|
|
|
|
|
Session::push('needCrmInfoUpdate', true);
|
2022-03-10 13:31:10 +00:00
|
|
|
} //end try
|
2021-12-03 10:55:30 +01:00
|
|
|
|
2023-01-10 11:09:36 +01:00
|
|
|
// Si pas de compte local mais existe en CRM.
|
2021-12-03 14:29:14 +01:00
|
|
|
if (!$customer) {
|
|
|
|
|
$customer = $this->createLocalCustomer($crmCustomer);
|
|
|
|
|
}
|
2021-10-21 10:35:37 +02:00
|
|
|
|
|
|
|
|
return $this->processLogin($customer, $request->remember);
|
|
|
|
|
} catch (ApiErrorException $e) {
|
|
|
|
|
return $this->handleLoginApiErrorException($e, $request);
|
2021-12-01 16:00:05 +01:00
|
|
|
} catch (CrmException | \Exception $e) {
|
2023-01-10 11:09:36 +01:00
|
|
|
if (app()->bound('sentry')) {
|
|
|
|
|
app('sentry')->captureException($e);
|
|
|
|
|
}
|
|
|
|
|
// Une erreur critique est survenue avec la CRM.
|
2021-10-21 10:35:37 +02:00
|
|
|
return back()->withErrors('Une erreur est survenue, merci de réessayer ultérieurement.');
|
2022-03-10 13:31:10 +00:00
|
|
|
} //end try
|
2021-10-21 10:35:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Handle Api error exception
|
2023-01-11 17:45:12 +01:00
|
|
|
* @param \LDC\LDCCRM\Exceptions\ApiErrorException $e
|
|
|
|
|
* @param \App\Http\Requests\LoginRequest $request
|
2023-01-10 11:09:36 +01:00
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
2023-01-11 17:45:12 +01:00
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException BindingResolutionException.
|
|
|
|
|
* @throws \Exception Exception.
|
2021-10-21 10:35:37 +02:00
|
|
|
*/
|
|
|
|
|
private function handleLoginApiErrorException(ApiErrorException $e, LoginRequest $request)
|
|
|
|
|
{
|
|
|
|
|
/** @var Customers|null */
|
|
|
|
|
$customer = Customers::where('email', $request->email)->first();
|
|
|
|
|
/** @var \LDC\LDCCRM\CrmApiV2 */
|
|
|
|
|
$crm = app('crmAPI');
|
|
|
|
|
switch ($e->getApiError()) {
|
|
|
|
|
case ApiError::authFailed():
|
2023-01-10 11:09:36 +01:00
|
|
|
// Mauvais identifiants.
|
2021-10-21 10:35:37 +02:00
|
|
|
return back()->withErrors("Email ou mot de passe incorrect.");
|
|
|
|
|
case ApiError::customerNotActivated():
|
2023-01-10 11:09:36 +01:00
|
|
|
// Le compte n'est pas activé.
|
2021-10-21 10:35:37 +02:00
|
|
|
if (!$customer) {
|
|
|
|
|
try {
|
|
|
|
|
if (!($crmCustomer = $crm->getCustomer($request->email))) {
|
|
|
|
|
Log::critical(sprintf('Customer %s coulnd not be retrieved from crm', $request->email));
|
|
|
|
|
return back()->withErrors(
|
|
|
|
|
__('Une erreur est survenue, merci de réessayer ultérieurement.')
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
$customer = $this->createLocalCustomer($crmCustomer);
|
|
|
|
|
} catch (CrmException | ApiErrorException $e) {
|
2023-01-10 11:09:36 +01:00
|
|
|
if (app()->bound('sentry')) {
|
|
|
|
|
app('sentry')->captureException($e);
|
|
|
|
|
}
|
|
|
|
|
// Une erreur critique est survenue avec la CRM.
|
2021-10-21 10:35:37 +02:00
|
|
|
return back()->withErrors('Une erreur est survenue, merci de réessayer ultérieurement.');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Mail::to($request->email)->send(new RegisterConfirmMail($customer));
|
|
|
|
|
return back()
|
|
|
|
|
->withErrors(
|
|
|
|
|
__('Votre compte n\'est pas activé, vérifiez dans vos emails pour y trouver
|
|
|
|
|
le mail de confirmation. Un lien d\'activation vous a été renvoyé.')
|
|
|
|
|
);
|
|
|
|
|
default:
|
2023-01-10 11:09:36 +01:00
|
|
|
if (app()->bound('sentry')) {
|
|
|
|
|
app('sentry')->captureException($e);
|
|
|
|
|
}
|
|
|
|
|
// Une erreur non gérée.
|
2021-10-21 10:35:37 +02:00
|
|
|
return back()->withErrors('Une erreur est survenue, merci de réessayer ultérieurement.');
|
2022-03-10 13:31:10 +00:00
|
|
|
} //end switch
|
2021-10-21 10:35:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a local customer
|
|
|
|
|
*
|
2023-01-10 11:09:36 +01:00
|
|
|
* @param \LDC\LDCCRM\Models\Customer as CrmCustomer $crmCustomer
|
2021-10-21 10:35:37 +02:00
|
|
|
* @return \app\Models\Customers
|
|
|
|
|
*/
|
2021-12-01 13:25:05 +01:00
|
|
|
private function createLocalCustomer(CrmCustomer $crmCustomer): Customers
|
2021-10-21 10:35:37 +02:00
|
|
|
{
|
|
|
|
|
$pseudo = Utils::getUniquePseudo(
|
2021-12-01 13:25:05 +01:00
|
|
|
$crmCustomer->firstname,
|
|
|
|
|
$crmCustomer->lastname
|
2021-10-21 10:35:37 +02:00
|
|
|
);
|
2021-12-03 14:29:14 +01:00
|
|
|
|
2022-02-10 16:14:58 +01:00
|
|
|
$customer = Customers::firstOrNew([
|
2021-12-01 13:25:05 +01:00
|
|
|
'email' => $crmCustomer->email
|
2021-10-21 10:35:37 +02:00
|
|
|
]);
|
2021-12-01 13:25:05 +01:00
|
|
|
$customer->hashcode = $crmCustomer->hashcode;
|
2021-10-21 10:35:37 +02:00
|
|
|
$customer->username = $pseudo;
|
|
|
|
|
$customer->saveOrFail();
|
|
|
|
|
|
|
|
|
|
return $customer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Login a customer
|
2023-01-11 17:45:12 +01:00
|
|
|
* @param \App\Models\Customers $customer
|
|
|
|
|
* @param boolean $remember
|
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException BindingResolutionException.
|
|
|
|
|
* @throws \Exception Exception.
|
|
|
|
|
* @throws \RuntimeException RuntimeException.
|
2021-10-21 10:35:37 +02:00
|
|
|
*/
|
|
|
|
|
private function processLogin(Customers $customer, bool $remember)
|
|
|
|
|
{
|
|
|
|
|
/** @var \Illuminate\Auth\SessionGuard */
|
|
|
|
|
$guard = auth('customer');
|
|
|
|
|
$guard->login($customer, $remember);
|
|
|
|
|
$url = Session::pull('url.intended');
|
|
|
|
|
Session::forget('url.intended');
|
|
|
|
|
Session::save();
|
|
|
|
|
return $url ? redirect()->to($url) : redirect()->to(Session::pull('previousurl'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Deconnexion
|
|
|
|
|
*
|
2023-01-10 11:09:36 +01:00
|
|
|
* @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse
|
2021-10-21 10:35:37 +02:00
|
|
|
*/
|
|
|
|
|
public function logout()
|
|
|
|
|
{
|
|
|
|
|
/** @var \Illuminate\Auth\SessionGuard */
|
|
|
|
|
$guard = auth('customer');
|
|
|
|
|
if ($guard->check()) {
|
|
|
|
|
$guard->logout();
|
|
|
|
|
return redirect()->route('home');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-01-11 17:45:12 +01:00
|
|
|
* Fomulaire reset mot de passe sur la page login
|
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
|
* @return \Illuminate\Http\RedirectResponse|void
|
|
|
|
|
* @throws \Exception Exception.
|
|
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException BindingResolutionException.
|
2021-10-21 10:35:37 +02:00
|
|
|
*/
|
|
|
|
|
public function passwordReset(Request $request)
|
|
|
|
|
{
|
|
|
|
|
$request->validate([
|
|
|
|
|
'email' => 'required|email:rfc,filter|max:255',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
if (Customers::where('email', $request->email)->exists()) {
|
|
|
|
|
return $this->sendResetPasswordLink($request->email);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-10 11:09:36 +01:00
|
|
|
// Le customer n'existe pas en Local, donc il faut le créer d'abord.
|
2021-10-21 10:35:37 +02:00
|
|
|
/** @var \LDC\LDCCRM\CrmApiV2 */
|
|
|
|
|
$crm = app('crmAPI');
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
if (!($crmCustomer = $crm->getCustomer($request->email))) {
|
|
|
|
|
return back()->withErrors(__('Cette adresse email n\'est pas enregistrée dans nos services.'));
|
|
|
|
|
}
|
2023-01-10 11:09:36 +01:00
|
|
|
// TODO remove this test.
|
2021-11-30 17:24:33 +01:00
|
|
|
if (!$crmCustomer->hashcode) {
|
2023-01-10 11:09:36 +01:00
|
|
|
if (app()->bound('sentry')) {
|
|
|
|
|
app('sentry')->captureException(new \Exception(sprintf(
|
|
|
|
|
'Reset pwd user hascode was null for %s,%s,%s,%s',
|
|
|
|
|
$crmCustomer->hashcode,
|
|
|
|
|
$crmCustomer->email,
|
|
|
|
|
$crmCustomer->city,
|
|
|
|
|
$crmCustomer->lastname
|
|
|
|
|
)));
|
|
|
|
|
}
|
2021-11-30 17:24:33 +01:00
|
|
|
}
|
2021-10-21 10:35:37 +02:00
|
|
|
$customer = $this->createLocalCustomer($crmCustomer);
|
|
|
|
|
return $this->sendResetPasswordLink($customer->email);
|
|
|
|
|
} catch (CrmException | ApiErrorException $e) {
|
2023-01-10 11:09:36 +01:00
|
|
|
if (app()->bound('sentry')) {
|
|
|
|
|
app('sentry')->captureException($e);
|
|
|
|
|
}
|
|
|
|
|
// Une erreur critique est survenue avec la CRM.
|
2021-10-21 10:35:37 +02:00
|
|
|
return back()->withErrors('Une erreur est survenue, merci de réessayer ultérieurement.');
|
2022-03-10 13:31:10 +00:00
|
|
|
} //end try
|
2021-10-21 10:35:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Send password reset email
|
|
|
|
|
*
|
2023-01-10 11:09:36 +01:00
|
|
|
* @param string $email
|
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
2021-10-21 10:35:37 +02:00
|
|
|
*/
|
|
|
|
|
private function sendResetPasswordLink(string $email): RedirectResponse
|
|
|
|
|
{
|
2023-01-10 11:09:36 +01:00
|
|
|
// Suppression des anciens tokens.
|
2021-10-21 10:35:37 +02:00
|
|
|
PasswordReset::where('email', $email)->delete();
|
|
|
|
|
|
|
|
|
|
$token = Str::random(80);
|
|
|
|
|
|
2022-02-10 16:14:58 +01:00
|
|
|
$passwordReset = new PasswordReset();
|
|
|
|
|
$passwordReset->email = $email;
|
|
|
|
|
$passwordReset->token = $token;
|
2021-10-21 10:35:37 +02:00
|
|
|
$passwordReset->created_at = now();
|
|
|
|
|
$passwordReset->save();
|
|
|
|
|
|
|
|
|
|
Mail::to($email)->send(new PasswordResetMail($token));
|
|
|
|
|
return back()->with(
|
|
|
|
|
'success',
|
|
|
|
|
__("La procédure pour changer votre mot de passe a été envoyé sur votre email.")
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Print the view used to change the password of a user when he required it
|
2023-01-10 11:09:36 +01:00
|
|
|
*
|
|
|
|
|
* @param string $token Token used to identificate the user to change the password of.
|
2021-10-21 10:35:37 +02:00
|
|
|
* @return View The view to print
|
|
|
|
|
*/
|
2023-01-10 11:09:36 +01:00
|
|
|
public function viewChangePassword(string $token)
|
2021-10-21 10:35:37 +02:00
|
|
|
{
|
|
|
|
|
$pwdReset = PasswordReset::where('token', $token)->first();
|
|
|
|
|
if (!$pwdReset) {
|
|
|
|
|
abort(419);
|
|
|
|
|
}
|
|
|
|
|
$customer = Customers::where('email', $pwdReset->email)->first();
|
|
|
|
|
if (!$customer) {
|
|
|
|
|
abort(404);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return view('front.auth.password-reset', compact('customer'))->with('token', $token);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-01-11 17:45:12 +01:00
|
|
|
* Modification du mot de passe par lien via le formulaire reset mot de passe
|
|
|
|
|
* @param \App\Http\Requests\ResetCustomerPassword $request
|
|
|
|
|
* @param string $token
|
2023-01-10 11:09:36 +01:00
|
|
|
* @return \Illuminate\Contracts\View\View|\Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse
|
2023-01-11 17:45:12 +01:00
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException BindingResolutionException.
|
|
|
|
|
* @throws \LDC\LDCCRM\Exceptions\CrmException CrmException.
|
2021-10-21 10:35:37 +02:00
|
|
|
*/
|
|
|
|
|
public function changePassword(ResetCustomerPassword $request, string $token)
|
|
|
|
|
{
|
|
|
|
|
$passwordReset = PasswordReset::where('token', $token)->first();
|
2022-02-10 16:14:58 +01:00
|
|
|
$customer = Customers::where('email', $passwordReset->email)->first();
|
2021-10-21 10:35:37 +02:00
|
|
|
|
|
|
|
|
/** @var \LDC\LDCCRM\CrmApiV2 */
|
|
|
|
|
$crm = app('crmAPI');
|
|
|
|
|
|
|
|
|
|
try {
|
2023-01-10 11:09:36 +01:00
|
|
|
// Ne devrait pas survenir, $crm->login throws ApiError::authFailed().
|
2021-10-21 10:35:37 +02:00
|
|
|
if ($crm->login($customer->email, $request->password)) {
|
2023-01-10 11:09:36 +01:00
|
|
|
// Mauvais identifiants.
|
2021-10-21 10:35:37 +02:00
|
|
|
return back()->withErrors('Vous ne pouvez pas utiliser un de vos anciens mot de passe.');
|
|
|
|
|
}
|
|
|
|
|
// phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch
|
|
|
|
|
} catch (ApiErrorException $e) {
|
2023-01-10 11:09:36 +01:00
|
|
|
// Continue.
|
2021-10-21 10:35:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
if (!($crmCustomer = $crm->getCustomer($customer->hashcode))) {
|
|
|
|
|
return back()
|
|
|
|
|
->withErrors('Changement impossible, compte inexistant.');
|
|
|
|
|
}
|
|
|
|
|
$crmCustomer->password = $request->password;
|
|
|
|
|
$crmCustomer->save();
|
|
|
|
|
} catch (\TypeError | CrmException | ApiErrorException $e) {
|
2023-01-10 11:09:36 +01:00
|
|
|
if (app()->bound('sentry')) {
|
|
|
|
|
app('sentry')->captureException($e);
|
|
|
|
|
}
|
|
|
|
|
// Une erreur critique est survenue avec la CRM.
|
2021-10-21 10:35:37 +02:00
|
|
|
return back()->withErrors('Une erreur est survenue, merci de réessayer ultérieurement.');
|
|
|
|
|
} catch (ValidationException $e) {
|
|
|
|
|
return back()->withErrors(sprintf('Nouveau mot de passe : %s', $e->getMessage()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$passwordReset->delete();
|
|
|
|
|
|
|
|
|
|
return view('front.auth.password-reset-ok', compact('customer'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Mettre à jour le mot de passe lorsque l'on est connecté
|
2023-01-11 17:45:12 +01:00
|
|
|
* @param \App\Http\Requests\CustomerUpdatePassword $request
|
2023-01-10 11:09:36 +01:00
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
2023-01-11 17:45:12 +01:00
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException BindingResolutionException.
|
2021-10-21 10:35:37 +02:00
|
|
|
*/
|
|
|
|
|
public function changePasswordWhenAuthenticated(CustomerUpdatePassword $request)
|
|
|
|
|
{
|
|
|
|
|
/** @var \App\Models\Customers */
|
|
|
|
|
$customer = auth('customer')->user();
|
|
|
|
|
|
|
|
|
|
/** @var \LDC\LDCCRM\CrmApiV2 */
|
|
|
|
|
$crm = app('crmAPI');
|
|
|
|
|
|
2023-01-10 11:09:36 +01:00
|
|
|
// Test old password.
|
2021-10-21 10:35:37 +02:00
|
|
|
try {
|
2023-01-10 11:09:36 +01:00
|
|
|
// Ne devrait pas survenir, $crm->login throws ApiError::authFailed().
|
2021-10-21 10:35:37 +02:00
|
|
|
if (!$crm->login($customer->email, $request->password_old)) {
|
2023-01-10 11:09:36 +01:00
|
|
|
// Mauvais identifiants.
|
2021-10-21 10:35:37 +02:00
|
|
|
return back()->withErrors('L\'ancien mot de passe ne correspond pas.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$crmCustomer = $crm->getCustomer($customer->hashcode);
|
|
|
|
|
|
|
|
|
|
$crmCustomer->password = $request->password;
|
|
|
|
|
$crmCustomer->save();
|
|
|
|
|
} catch (ApiErrorException $e) {
|
2022-03-31 09:05:08 +02:00
|
|
|
if ($e->getApiError()->equals(ApiError::authFailed())) {
|
2021-10-21 10:35:37 +02:00
|
|
|
return back()->withErrors('L\'ancien mot de passe ne correspond pas.');
|
|
|
|
|
}
|
2023-01-10 11:09:36 +01:00
|
|
|
if (app()->bound('sentry')) {
|
|
|
|
|
app('sentry')->captureException($e);
|
|
|
|
|
}
|
|
|
|
|
// Une erreur critique est survenue avec la CRM.
|
2021-10-21 10:35:37 +02:00
|
|
|
return back()->withErrors('Une erreur est survenue, merci de réessayer ultérieurement.');
|
|
|
|
|
} catch (\TypeError | CrmException $e) {
|
2023-01-10 11:09:36 +01:00
|
|
|
if (app()->bound('sentry')) {
|
|
|
|
|
app('sentry')->captureException($e);
|
|
|
|
|
}
|
|
|
|
|
// Une erreur critique est survenue avec la CRM.
|
2021-10-21 10:35:37 +02:00
|
|
|
return back()->withErrors('Une erreur est survenue, merci de réessayer ultérieurement.');
|
|
|
|
|
} catch (ValidationException $e) {
|
|
|
|
|
return back()->withErrors(sprintf('Nouveau mot de passe : %s', $e->getMessage()));
|
2022-03-10 13:31:10 +00:00
|
|
|
} //end try
|
2021-10-21 10:35:37 +02:00
|
|
|
return back()->with('success', __("Modifications enregistrés !"));
|
|
|
|
|
}
|
|
|
|
|
}
|