2020-10-29 15:40:23 +01:00
< ? php
namespace App\Http\Controllers ;
2020-10-30 16:12:14 +01:00
use App\Mail\VerifyEmail ;
2020-10-29 15:40:23 +01:00
use Illuminate\Http\Request ;
use Illuminate\Support\Facades\Auth ;
2020-10-30 16:12:14 +01:00
use Illuminate\Support\Str ;
2020-10-29 15:40:23 +01:00
use App\User ;
use CPCConnection ;
use Exception ;
2020-10-30 16:12:14 +01:00
use Illuminate\Support\Facades\Mail ;
2020-10-29 15:40:23 +01:00
class LoginController extends Controller
{
public function __construct () {
$this -> middleware ( 'guest' ) -> except ( 'logout' );
}
public function index () {
return view ( 'front.basic.login' );
}
public function authenticate ( Request $request )
{
$email = $request -> email ;
$password = $request -> password ;
$remember = $request -> remember !== null ? true : false ;
$userLocal = User :: where ( 'email' , $email ) -> first ();
if ( $userLocal ) {
2020-10-30 16:12:14 +01:00
if ( $userLocal -> email_verified === 1 ) {
if ( $userLocal -> role !== 1 ) {
2020-10-29 15:40:23 +01:00
try {
2020-10-30 16:12:14 +01:00
$cpcConnect = new CPCConnection ( config ( 'crm.CRM_PLATOS_WSDL' ), config ( 'crm.WS_SERVCE_LOGIN' ), config ( 'crm.WS_SERVCE_PASSWORD' ));
$cpcConnect -> connect ();
try {
// ---- Authentification du user
// En attendant la mise à jour de la crm, on envoi un mot de passe bidon (le même qu'a l'inscription)
$cpcConnect -> authenticateUser ( $email , 'MOTDEPASSE' );
} catch ( Exception $e ) {
return redirect () -> back () -> withInput ( $request -> only ( " email " , " password " )) -> withErrors ( " Email ou mot de passe incorrect. " );
}
2020-10-29 15:40:23 +01:00
} catch ( Exception $e ) {
2020-10-30 16:12:14 +01:00
return redirect () -> back () -> withInput ( $request -> only ( " email " , " password " )) -> withErrors ( " La connexion est impossible pour le moment, veuillez réessayer plus tard. " );
die ();
2020-10-29 15:40:23 +01:00
}
}
2020-10-30 16:12:14 +01:00
} else {
$userLocal -> email_verify_token = Str :: uuid ();
$userLocal -> save ();
Mail :: to ( $email ) -> send ( new VerifyEmail ( $userLocal ));
return redirect () -> back () -> withInput ( $request -> only ( " email " , " password " )) -> withErrors ( " Votre compte éxiste mais votre email n'est pas confirmer, merci de vérifier vos emails. " );
2020-10-29 15:40:23 +01:00
}
if ( Auth :: attempt ([ " email " => $email , " password " => $password ], $remember )) {
if ( $userLocal -> role === 1 )
return redirect () -> route ( 'bo.dashboard' );
return redirect () -> route ( 'home' );
} else {
return redirect () -> back () -> withInput ( $request -> only ( 'email' , 'remember' )) -> withErrors ( 'Les identifiants entrés sont incorrects.' );
}
} else {
return redirect () -> back () -> withInput ( $request -> only ( 'email' , 'remember' )) -> withErrors ( 'Les identifiants entrés sont incorrects.' );
}
}
public function logout () {
Auth :: logout ();
return redirect () -> route ( 'home' );
}
2020-10-30 16:12:14 +01:00
public function confirmEmail ( $verifyToken ) {
$result = false ;
$resendButton = false ;
$user = User :: where ( 'email_verify_token' , $verifyToken ) -> first ();
if ( ! $user )
return view ( 'front.basic.confirm-email' , compact ( 'result' ));
$user -> email_verified = 1 ;
$user -> save ();
$result = true ;
return view ( 'front.basic.confirm-email' , compact ( 'result' ));
}
2020-10-29 15:40:23 +01:00
}