File: D:/HostingSpaces/SBogers85/equichecker.com/app/KommaApp/Customers/CustomerAuthController.php
<?php
namespace KommaApp\Customers;
use App\Http\Middleware\SetLanguage;
use Auth;
use Illuminate\Http\Request;
use Illuminate\Mail\Message;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Password;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\MessageBag;
use Input;
use KommaApp\Auth\AuthService;
use KommaApp\Languages\Models\Language;
use Redirect;
use View;
use Carbon\Carbon;
use Config;
use KommaApp\Users\Models\User;
/**
*
*
* @author Komma <info@komma.pro>
* @copyright (c) 2012-2016, Komma Mediadesign
*/
class CustomerAuthController extends Controller
{
/**
* Show the login form.
*
* @return \Illuminate\View\View
*/
public function login()
{
$message = (object)[
'status' => false,
'type' => 0,
];
if(\Session::has('myFlashMessage')){
$message->status = true;
$message->type = \Session::get('myFlashMessage');
\Session::forget('myFlashMessage');
}
return View::make('auth.login')->with('message', $message);
}
/**
* Handle the response from the login form.
*
* @return \Illuminate\Http\RedirectResponse
*/
public function processLogin(AuthService $authService, Request $request)
{
//Check if the account exists
if (!$customer = $authService->getAccountBy('customer', 'email', $request->get('email'))) {
//Account doesn't exist for security throw wrong_credentials error
return Redirect::back()->withInput()
->withErrors(['message' => \Lang::get('auth.login.wrong_credentials')]);
}
//Check if the account is active
if (!$authService->checkActive('customer', $customer)) {
return Redirect::back()->withInput()
->withErrors([
'message' => \Lang::get('auth.login.not_active'),
'action' => ucfirst(\Lang::get('auth.login.please'))." <a href='mailto:".\Lang::get('auth.login.mail_address')."'>".\Lang::get('auth.login.contact_us')."</a> ".\Lang::get('auth.login.to_activate_your_account')
]);
}
//Check if the last time was longer ago then the waiting time
if (Carbon::now()->diffInSeconds($customer['last_login_attempt']) > Config::get('kms.main.failedLoginWaitingTime', 7200)) {
//The customer has waited long enough, so he can try again
$customer->failed_login_attempts = 0;
}
//Check if the customer tried to many times
if ($customer->failed_login_attempts > Config::get('kms.main.maxLoginAttempts', 5)) {
//Trow too many times error
return Redirect::back()
->withInput()
->withErrors(['message' => \Lang::get('auth.login.too_many_times')]);
}
//Set the last_login_attemt to now
$customer->last_login_attempt = Carbon::now();
// Attemt to login
if (!Auth::customer()->attempt(['email' => Input::get('email'), 'password' => Input::get('password')])) {
//Add 1 to failed_login_attempts
$customer->failed_login_attempts++;
//Save the customer
$customer->save();
//False return with wrong_credential notice
return Redirect::back()
->withInput()
->withErrors([
'message' => \Lang::get('auth.login.wrong_credentials')
]);
}
//Reset the failed_login_attempts
$customer->failed_login_attempts = 0;
//Save the customer
$customer->save();
//Switch to customer language
$this->setLocaleToCustomer($customer);
\Session::put('myFlashMessage', 1);
//Redirect
return Redirect::to('/')->withSuccess('Logged in');
}
/**
* Logout and redirect to the homepage.
*
* @return \Illuminate\Http\RedirectResponse
*/
public function logout()
{
Auth::customer()->logout();
\Komma::setDefaultLocale();
\Session::put('myFlashMessage', 4);
return Redirect::to('/login');
}
/**
* Show the forgot Password page
*
* @return mixed
*/
public function forgotPassword()
{
return View::make('auth.password-forgot');
}
/**
* Process the password forgot page
* Check if account exist
* and sent an email.
*
* @param AuthService $authService
* @param Request $request
* @return mixed
*/
public function processForgotPassword(AuthService $authService, Request $request)
{
//Check if the customer has an account
if (!$customer = $authService->getAccountBy('customer', 'email', $request->get('email'))) {
//No customer found. trow error
return Redirect::back()
->withErrors(['message' => \Lang::get('auth.password-forgot.no_account')]);
}
$this->setLocaleToCustomer($customer);
\Session::put('myFlashMessage', 3);
//customer found, so create an sendResetLinkemail
Password::customer()->sendResetLink(Input::only('email'), function (Message $message) {
$message->subject(\Lang::get('email/forgot.subject'));
});
//Add a messageBag for success
$bag = new MessageBag();
$bag->add('message', \Lang::get('auth.password-forgot-email.confirmation'));
return Redirect::to('login')->withSuccess($bag);
}
/**
* This method is called to reset the password of an customer
*
* @param AuthService $authService
* @param $type
* @param $token
* @return mixed
*/
public function resetPasswordPage(AuthService $authService, $type, $token)
{
// Check if there is an record in the password_resets table
if (!$reset = $authService->getPasswordResets($type, $token)
) {
//If not redirect to login with wrong_token error
return Redirect::to('login')->withErrors([
'message' => \Lang::get('auth.password-reset-page.wrong_token')
]);
}
$customer = $authService->getAccountBy('customer', 'email', $reset->email);
$this->setLocaleToCustomer($customer);
return View::make('auth.password-reset');
}
/**
* This method processes the password reset form
*
* @param AuthService $authService
* @param $type
* @param $token
* @param Request $request
* @return mixed
*/
public function processResetPassword(AuthService $authService, $type, $token, Request $request)
{
if (!$reset = $authService->getPasswordResets($type, $token)) {
//Not an correct token
return \Redirect::back()->withErrors([
'message' => \Lang::get('auth.password-reset-page.wrong_token')
]);
}
//Validate the password reset form
$validator = Validator::make($request->all(), [
'password' => 'required',
'password_confirmation' => 'required|same:password',
]);
//Check the validation
if ($validator->fails()) {
//Return with the errorss
return \Redirect::back()
->withErrors($validator->messages());
}
//All is well, change the password
$customer = Customer::where('email', $reset->email)->first();
$customer->update(['password' => \Hash::make($request->get('password'))]);
\Session::put('myFlashMessage', 5);
$bag = new MessageBag();
$bag->add('message', \Lang::get('auth.password-reset-page.password_updated'));
return Redirect::to('login')->withSuccess($bag);
}
public function activateAccountPage(AuthService $authService, $type, $token)
{
// Check if there is an record in the password_resets table
if (!$reset = $authService->getPasswordResets($type, $token)
) {
//If not redirect to login with wrong_token error
return Redirect::to('login')->withErrors([
'message' => \Lang::get('auth.password-reset-page.wrong_token')
]);
}
$customer = $authService->getAccountBy('customer', 'email', $reset->email);
$this->setLocaleToCustomer($customer);
return View::make('auth.activate-account');
}
/**
* This method processes the password reset form
*
* @param AuthService $authService
* @param $type
* @param $token
* @param Request $request
* @return mixed
*/
public function processActivateAccount(AuthService $authService, $type, $token, Request $request)
{
if (!$reset = $authService->getPasswordResets($type, $token)) {
//Not an correct token
return \Redirect::back()->withErrors([
'message' => \Lang::get('auth.password-reset-page.wrong_token')
]);
}
//Validate the password reset form
$validator = Validator::make($request->all(), [
'password' => 'required',
'password_confirmation' => 'required|same:password',
]);
//Check the validation
if ($validator->fails()) {
//Return with the errors
return \Redirect::back()
->withErrors($validator->messages());
}
//All is well, change the password
$customer = Customer::where('email', $reset->email)->first();
$customer->update(['active' => 1, 'password' => \Hash::make($request->get('password'))]);
$bag = new MessageBag();
$bag->add('message', \Lang::get('auth.password-reset-page.password_updated'));
\Session::put('myFlashMessage', 2);
return Redirect::to('login')->withSuccess($bag);
}
private function setLocaleToCustomer($customer)
{
if (!$locale = $customer->language->iso_2) return false;
SetLanguage::setLanguage(Language::find($customer->language_id)->iso_2);
}
}