ldc-gen-game-web/app/Http/Controllers/LoginController.php

294 lines
12 KiB
PHP
Raw Normal View History

2020-10-29 15:40:23 +01:00
<?php
namespace App\Http\Controllers;
use App\Mail\PasswordReset;
use App\Mail\VerifyEmail;
2020-11-12 17:22:54 +01:00
use App\Models\Settings;
2020-10-29 15:40:23 +01:00
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;
use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Mail;
2020-10-29 15:40:23 +01:00
class LoginController extends Controller
{
2021-02-23 18:27:03 +01:00
private $apiCrm;
2021-08-18 17:08:02 +02:00
public function __construct()
{
2020-10-29 15:40:23 +01:00
$this->middleware('guest')->except('logout');
2021-02-23 18:27:03 +01:00
$this->apiCrm = new \LDCCRM\ApiV1(config('crm.endpoint'), config('crm.token'));
2020-10-29 15:40:23 +01:00
}
2022-08-11 10:26:29 +02:00
/**
* index
* @return \Illuminate\View\View
*/
2021-08-18 17:08:02 +02:00
public function index()
{
2020-10-29 15:40:23 +01:00
return view('front.basic.login');
}
public function authenticate(Request $request)
{
$request->validate([
'email' => 'required|email',
'password' => 'required',
]);
2021-08-18 17:08:02 +02:00
$email = filter_var($request->email, FILTER_SANITIZE_EMAIL);
2020-10-29 15:40:23 +01:00
$password = $request->password;
$remember = $request->remember !== null ? true : false;
$userLocal = User::where('email', $email)->first();
if ($userLocal) {
if ($userLocal->email_verified === 1) {
if ($userLocal->role > 2) {
2021-02-23 18:27:03 +01:00
$connect = $this->apiCrm->loginCustomer($email, $password);
2021-08-18 17:08:02 +02:00
if ($connect->status === 'KO') {
2021-02-23 18:27:03 +01:00
if ($connect->code === 500) {
return back()->withInput($request->except("password"))
2023-05-09 10:06:31 +00:00
->withErrors(trans('The connection is not possible at the moment, please try again later.'));
2021-08-18 17:08:02 +02:00
} else {
return back()->withInput($request->except("password"))
2023-05-09 10:06:31 +00:00
->withErrors("Falsche E-Mail oder falsches Passwort.");
}
2020-10-29 15:40:23 +01:00
}
}
} else {
$userLocal->email_verify_token = Str::uuid();
$userLocal->save();
Mail::to($email)->send(new VerifyEmail($userLocal));
return back()->withInput($request->except("password"))
2023-05-09 10:06:31 +00:00
->withErrors(
trans('Your account exists, but your email has not been confirmed. Please check your emails.')
);
}//end if
2020-11-20 17:47:05 +01:00
} else {
2021-02-23 18:27:03 +01:00
$connect = $this->apiCrm->loginCustomer($email, $password);
2021-08-18 17:08:02 +02:00
if ($connect->status === 'OK') {
2021-02-23 18:27:03 +01:00
$userLocal = User::create([
'crm_id' => $connect->user->crm_id,
2023-05-10 17:01:16 +02:00
'firstname' => $connect->user->firstname,
'lastname' => $connect->user->lastname,
2021-02-23 18:27:03 +01:00
'birth_date' => $connect->user->birth_date,
'email' => $email,
// TODO : MOTDEPASSE ???.
'password' => Hash::make("MOTDEPASSE"),
2021-02-23 18:27:03 +01:00
'address' => $connect->user->address,
'address2' => $connect->user->address2,
2023-05-10 17:01:16 +02:00
'town' => $connect->user->town,
2021-02-23 18:27:03 +01:00
'postal_code' => $connect->user->postcode,
'country' => $connect->user->country,
'email_verified' => 1
]);
2021-08-18 17:08:02 +02:00
} else {
// Compte existant mais non activé.
if ($connect->code === 116) {
2020-11-20 17:47:05 +01:00
$userLocal = User::create([
2021-02-23 18:27:03 +01:00
'crm_id' => $connect->user->crm_id,
2023-05-10 17:01:16 +02:00
'firstname' => $connect->user->firstname,
'lastname' => $connect->user->lastname,
2021-02-23 18:27:03 +01:00
'birth_date' => $connect->user->birth_date,
'email' => $email,
// TODO : MOTDEPASSE ???.
'password' => Hash::make("MOTDEPASSE"),
2021-02-23 18:27:03 +01:00
'address' => $connect->user->address,
'address2' => $connect->user->address2,
2023-05-10 17:01:16 +02:00
'town' => $connect->user->town,
2021-02-23 18:27:03 +01:00
'postal_code' => $connect->user->postcode,
'country' => $connect->user->country
2020-11-20 17:47:05 +01:00
]);
2021-02-23 18:27:03 +01:00
$userLocal->email_verify_token = Str::uuid();
$userLocal->save();
Mail::to($email)->send(new VerifyEmail($userLocal));
return back()->withInput($request->except("password"))
2023-05-09 10:06:31 +00:00
->withErrors(
trans('Your account exists, but your email has not been confirmed. Please check your emails.')
);
2021-08-18 17:08:02 +02:00
} elseif ($connect->code === 500) {
return back()->withInput($request->except("password"))
2023-05-09 10:06:31 +00:00
->withErrors("Die Verbindung ist momentan nicht möglich, bitte versuchen Sie es später noch einmal.");
2021-08-18 17:08:02 +02:00
} else {
return back()->withInput($request->except("password"))
2023-05-09 10:06:31 +00:00
->withErrors("Falsche E-Mail oder falsches Passwort.");
} //end if
} //end if
}//end if
2020-11-20 17:47:05 +01:00
2022-08-11 10:26:29 +02:00
// phpcs:ignore
// TODO : Modifier avec separation guard.
2020-11-20 17:47:05 +01:00
$tmpPwd = "MOTDEPASSE";
2021-08-18 17:08:02 +02:00
if ($userLocal->role <= 2) {
2020-11-20 17:47:05 +01:00
$tmpPwd = $password;
2021-08-18 17:08:02 +02:00
}
2020-11-20 17:47:05 +01:00
if (Auth::attempt(["email" => $email, "password" => $tmpPwd], $remember)) {
2021-08-18 17:08:02 +02:00
if ($userLocal->role <= 2) {
2020-11-20 17:47:05 +01:00
return redirect()->route('bo.dashboard');
2021-08-18 17:08:02 +02:00
}
2020-11-20 17:47:05 +01:00
return redirect()->route('saisir-code');
2020-10-29 15:40:23 +01:00
} else {
2023-05-09 10:06:31 +00:00
return back()->withInput($request->except('password'))->withErrors('Die eingegebenen IDs sind falsch.');
2020-10-29 15:40:23 +01:00
}
}
2022-08-11 10:26:29 +02:00
/**
* logout
* @return Illuminate\Http\RedirectResponse
*/
2021-08-18 17:08:02 +02:00
public function logout()
{
2020-10-29 15:40:23 +01:00
Auth::logout();
return redirect()->route('home');
}
2022-08-11 10:26:29 +02:00
/**
* confirm email
* @param string $verifyToken
* @return \Illuminate\View\View|Illuminate\Http\RedirectResponse
*/
public function confirmEmail(string $verifyToken)
2021-08-18 17:08:02 +02:00
{
$result = false;
2021-08-18 17:08:02 +02:00
$user = User::where('email_verify_token', $verifyToken)->first();
if (!$user) {
return view('front.basic.confirm-email', compact('result'));
2021-08-18 17:08:02 +02:00
}
2020-11-13 17:42:59 +01:00
2021-02-23 18:27:03 +01:00
$activation = $this->apiCrm->activateCustomer($user->crm_id);
2021-08-18 17:08:02 +02:00
if ($activation->status === 'OK') {
$user->email_verified = 1;
$user->email_verify_token = null;
2021-02-23 18:27:03 +01:00
$user->save();
$result = true;
2021-08-18 17:08:02 +02:00
} else {
// User already active.
if ($activation->code === 110) {
2021-08-18 17:08:02 +02:00
$user->email_verified = 1;
$user->email_verify_token = null;
2020-11-13 17:42:59 +01:00
$user->save();
$result = true;
}
}
return view('front.basic.confirm-email', compact('result'));
}
2021-08-18 17:08:02 +02:00
public function forgotPassword(Request $request)
{
if ($request->tokenValid && $request->token) {
$tokenValid = $request->tokenValid;
2021-08-18 17:08:02 +02:00
$token = $request->token;
return view('front.basic.forgot-password', compact('tokenValid', 'token'));
}
return view('front.basic.forgot-password');
}
2021-08-18 17:08:02 +02:00
public function forgotPasswordReset(Request $request, $token)
{
if (!$request->hasValidSignature(false)) {
return redirect()->route('login')
2023-05-09 10:06:31 +00:00
->withErrors("Der Link zum Ändern Ihres Passworts ist nicht mehr gültig, bitte versuchen Sie es erneut.");
2021-08-18 17:08:02 +02:00
}
$tokenValid = false;
2021-08-18 17:08:02 +02:00
$user = User::where('password_token', $token)->first();
if (!$user) {
return redirect()->route('forgot-password', compact('tokenValid'))
2023-05-09 10:06:31 +00:00
->withErrors('Der Link zum Ändern des Passworts ist falsch.');
2021-08-18 17:08:02 +02:00
}
$tokenValid = true;
return redirect()->route('forgot-password', compact('tokenValid', 'token'));
}
2021-08-18 17:08:02 +02:00
public function mailPassword(Request $request)
{
$email = filter_var($request->email, FILTER_SANITIZE_STRING);
2021-08-18 17:08:02 +02:00
$user = User::where('email', $email)->first();
2021-02-23 18:27:03 +01:00
2020-12-09 17:36:54 +01:00
if (!$user) {
2021-02-23 18:27:03 +01:00
$userCrm = $this->apiCrm->getCustomerByEmail($email);
2021-08-18 17:08:02 +02:00
if ($userCrm->status === 'OK') {
2021-02-23 18:27:03 +01:00
$user = User::create([
'crm_id' => $userCrm->user->crm_id,
2023-05-10 17:01:16 +02:00
'firstname' => $userCrm->user->firstname,
'lastname' => $userCrm->user->lastname,
2021-02-23 18:27:03 +01:00
'birth_date' => $userCrm->user->birth_date,
'email' => $email,
// TODO : MOTDEPASSE ???
'password' => Hash::make("MOTDEPASSE"),
2021-02-23 18:27:03 +01:00
'address' => $userCrm->user->address,
'address2' => $userCrm->user->address2,
2023-05-10 17:01:16 +02:00
'town' => $userCrm->user->town,
2021-02-23 18:27:03 +01:00
'postal_code' => $userCrm->user->postcode,
'country' => $userCrm->user->country,
'email_verified' => 1
]);
2021-08-18 17:08:02 +02:00
if ($userCrm->user->status === 0) {
$user->email_verified = 0;
2021-02-23 18:27:03 +01:00
$user->email_verify_token = Str::uuid();
$user->save();
Mail::to($email)->send(new VerifyEmail($user));
return back()->withInput($request->except("password"))
2023-05-09 10:06:31 +00:00
->withErrors(
trans('Your account exists, but your email has not been confirmed. Please check your emails.')
);
2021-02-23 18:27:03 +01:00
}
2021-08-18 17:08:02 +02:00
} else {
2021-02-23 18:27:03 +01:00
if ($userCrm->code === 101) {
return back()->withInput($request->only('email'))
2023-05-09 10:06:31 +00:00
->withErrors("Die von Ihnen eingegebene E-Mail ist bei uns nicht gespeichert.");
2021-08-18 17:08:02 +02:00
} else {
2023-05-09 10:06:31 +00:00
return back()->withErrors("Es ist ein Fehler aufgetreten. Bitte versuchen Sie es später noch einmal.");
2020-12-09 17:36:54 +01:00
}
} //end if
}//end if
2020-12-09 17:36:54 +01:00
$user->password_token = Str::uuid();
$user->save();
2022-01-11 10:25:23 +01:00
/** @var \App\Models\Settings $settings */
$settings = app(Settings::class);
2020-12-09 17:36:54 +01:00
Mail::to($email)->send(new PasswordReset($user, $settings));
return redirect()->route('login')
2023-05-09 10:06:31 +00:00
->with('success', 'Eine E-Mail zum Zurücksetzen Ihres Passworts wurde Ihnen zugeschickt.');
}
2021-08-18 17:08:02 +02:00
public function changePassword(Request $request)
{
$request->validate([
'password' => 'required|min:8|confirmed',
]);
$user = User::where('password_token', filter_var($request->token, FILTER_SANITIZE_STRING))->first();
2021-08-18 17:08:02 +02:00
if (!$user) {
2023-05-09 10:06:31 +00:00
return back()->withErrors('Der Link zum Ändern Ihres Passworts ist falsch, bitte versuchen Sie es noch einmal.');
2021-08-18 17:08:02 +02:00
}
2020-11-20 17:47:05 +01:00
// TODO : Ajouter controle du contenu du mot de passe.
$newPassword = $request->password;
2020-11-20 17:47:05 +01:00
2021-02-23 18:27:03 +01:00
$update = $this->apiCrm->changePwdCustomer($user->crm_id, $newPassword);
2021-08-18 17:08:02 +02:00
if ($update->status === 'OK') {
$user->password_token = null;
$user->save();
return redirect()->route('login')
2023-05-09 10:06:31 +00:00
->with('success', 'Sie können sich nun mit Ihrem neuen Passwort anmelden.');
2021-08-18 17:08:02 +02:00
} else {
if ($update->code === 123) {
return back()
2023-05-09 10:06:31 +00:00
->withErrors("Es scheint, dass Sie dieses Passwort bereits für Ihr Konto verwendet haben. Bitte versuchen Sie es erneut, indem Sie Ihr Passwort ändern.");
2021-08-18 17:08:02 +02:00
} else {
2023-05-09 10:06:31 +00:00
return back()->withErrors("Es ist ein Fehler aufgetreten. Bitte versuchen Sie es später noch einmal.");
}
2020-11-20 17:47:05 +01:00
}
}
2020-10-29 15:40:23 +01:00
}