2024-07-01 10:50:41 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Club\Auth;
|
|
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
2024-10-07 17:45:33 +02:00
|
|
|
use App\Lib\Utils;
|
2024-07-01 10:50:41 +02:00
|
|
|
use App\Models\Customer;
|
2024-08-02 09:56:54 +02:00
|
|
|
use Exception;
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2024-07-01 10:50:41 +02:00
|
|
|
use Illuminate\Foundation\Auth\AuthenticatesUsers;
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Illuminate\Support\Arr;
|
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
2024-10-07 17:45:33 +02:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2024-08-02 09:56:54 +02:00
|
|
|
use Illuminate\Support\Facades\Session;
|
|
|
|
|
use LDC\LDCCRM\Models\Customer as CrmCustomer;
|
2024-07-01 10:50:41 +02:00
|
|
|
|
|
|
|
|
class LoginController extends Controller
|
|
|
|
|
{
|
|
|
|
|
use AuthenticatesUsers;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a new controller instance.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function __construct()
|
|
|
|
|
{
|
|
|
|
|
$this->middleware('guest:club')->except('logout');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the guard to be used during authentication.
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Contracts\Auth\StatefulGuard
|
|
|
|
|
*/
|
2024-08-02 09:56:54 +02:00
|
|
|
protected function guard(): \Illuminate\Contracts\Auth\StatefulGuard
|
2024-07-01 10:50:41 +02:00
|
|
|
{
|
|
|
|
|
return Auth::guard('club');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Show the application's login form.
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Contracts\View\View
|
|
|
|
|
*/
|
2024-08-02 09:56:54 +02:00
|
|
|
public function showLoginForm(): \Illuminate\Contracts\View\View
|
2024-07-01 10:50:41 +02:00
|
|
|
{
|
|
|
|
|
return view('club.auth.login');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Where to redirect users after login.
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
protected function redirectTo(): string
|
|
|
|
|
{
|
2024-07-05 16:34:57 +02:00
|
|
|
return $this->authLoggedRedirectPath();
|
2024-07-01 10:50:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Attempt to log the user into the application.
|
|
|
|
|
*
|
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
|
* @return boolean
|
|
|
|
|
*/
|
2024-08-02 09:56:54 +02:00
|
|
|
protected function attemptLogin(Request $request): bool
|
2024-07-01 10:50:41 +02:00
|
|
|
{
|
2024-10-07 17:45:33 +02:00
|
|
|
$credentials = $this->credentials($request);
|
2024-08-02 09:56:54 +02:00
|
|
|
$customerModel = Customer::query()->where('email', Arr::get($credentials, 'email'))->first();
|
2024-10-07 17:45:33 +02:00
|
|
|
$crmCustomer = CrmCustomer::findWithEmail(Arr::get($credentials, 'email'));
|
2024-07-01 10:50:41 +02:00
|
|
|
|
2024-08-02 09:56:54 +02:00
|
|
|
// Check database customer.
|
|
|
|
|
if (!$customerModel) {
|
2024-10-07 17:45:33 +02:00
|
|
|
// Check crm customer.
|
|
|
|
|
if (!$crmCustomer) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-26 17:39:57 +02:00
|
|
|
/** @var \App\Models\Customer $customerModel */
|
2024-10-07 17:45:33 +02:00
|
|
|
$customerModel = new Customer(Arr::only($crmCustomer->jsonSerialize(), ['email']));
|
|
|
|
|
$customerModel->username = Utils::getUniquePseudo(
|
|
|
|
|
$crmCustomer->firstname,
|
|
|
|
|
$crmCustomer->lastname
|
|
|
|
|
);
|
|
|
|
|
$customerModel->hashcode = $crmCustomer->hashcode;
|
|
|
|
|
|
|
|
|
|
if (!$customerModel->saveOrFail()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// Check crm customer.
|
|
|
|
|
if (!$crmCustomer) {
|
|
|
|
|
$customerModel->delete();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2024-09-26 17:39:57 +02:00
|
|
|
} //end if
|
2024-08-02 09:56:54 +02:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
CrmCustomer::login(Arr::get($credentials, 'email'), Arr::get($credentials, 'password'));
|
|
|
|
|
} catch (Exception $ex) {
|
2024-10-07 17:45:33 +02:00
|
|
|
Log::error($ex);
|
2024-08-02 09:56:54 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
2024-10-07 17:45:33 +02:00
|
|
|
|
2024-08-02 09:56:54 +02:00
|
|
|
$this->guard()->loginUsingId($customerModel->getKey());
|
2024-07-01 10:50:41 +02:00
|
|
|
return true;
|
|
|
|
|
}
|
2024-08-02 09:56:54 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The user has been authenticated.
|
|
|
|
|
*
|
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
|
* @param \Illuminate\Database\Eloquent\Model $customerModel
|
|
|
|
|
* @return void
|
|
|
|
|
* @phpcs:disable Squiz.Commenting.FunctionComment.TypeHintMissing, Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
|
|
|
|
|
*/
|
|
|
|
|
protected function authenticated(Request $request, Model $customerModel): void
|
|
|
|
|
{
|
|
|
|
|
// @phpstan-ignore-next-line
|
|
|
|
|
$customerModel->last_connection = \now();
|
|
|
|
|
$customerModel->saveOrFail();
|
|
|
|
|
Session::flash('success', trans('Vous êtes à présent connecté'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The user has logged out of the application.
|
|
|
|
|
*
|
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
|
* @return void
|
|
|
|
|
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClass
|
|
|
|
|
*/
|
|
|
|
|
protected function loggedOut(Request $request): void
|
|
|
|
|
{
|
|
|
|
|
Session::flash('info', trans('Vous êtes à présent déconnecté'));
|
|
|
|
|
}
|
2024-07-01 10:50:41 +02:00
|
|
|
}
|