File: D:/HostingSpaces/Lacom/lacom.nl/app/KommaApp/Auth/AuthController.php
<?php
/**
* Short description for the file.
*
* @copyright (c) 2012-2015, Komma
*/
namespace App\KommaApp\Auth;
use App\Http\Controllers\Controller;
use Carbon\Carbon;
use Illuminate\Mail\Message;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Password;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\MessageBag;
use Illuminate\Http\Request;
class AuthController extends Controller
{
/**
* Show the login form.
*
* @return \Illuminate\View\View
*/
public function login()
{
return \View::make('auth.login');
}
/**
* Handle the response from the login form.
*
* @param AuthService $authService
* @param Request $request
* @return \Illuminate\Http\RedirectResponse
*/
public function processLogin(AuthService $authService, Request $request)
{
//Check if the account exists
if (!$user = $authService->getAccountBy('endUser', '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 last time was longer ago then the waiting time
if (Carbon::now()->diffInSeconds($user['last_login_attempt']) > Config::get('kms.main.failedLoginWaitingTime', 7200)) {
//The endUser has waited long enough, so he can try again
$user->failed_login_attempts = 0;
}
//Check if the endUser tried to many times
if ($user->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
$user->last_login_attempt = Carbon::now();
// Attemt to login
if (!Auth::attempt(['email' => \Input::get('email'), 'password' => Input::get('password')])) {
//Add 1 to failed_login_attempts
$user->failed_login_attempts++;
//Save the endUser
$user->save();
//False return with wrong_credential notice
return Redirect::back()
->withInput()
->withErrors([
'message' => \Lang::get('auth.login.wrong_credentials')
]);
}
//Reset the failed_login_attempts
$user->failed_login_attempts = 0;
//Save the endUser
$user->save();
//Switch to endUser language
$this->setLocaleToendUser($user);
//Got to the intended page
return Redirect::intended()->withSuccess('Logged in');
}
/**
* Logout and redirect to the homepage.
*
* @return \Illuminate\Http\RedirectResponse
*/
public function logout()
{
// Auth::endUser()->logout();
Auth::logout();
\Komma::setDefaultLocale();
return Redirect::to('/');
}
/**
* 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 endUser has an account
if (!$user = $authService->getAccountBy('endUser', 'email', $request->get('email'))) {
//No endUser found. trow error
return Redirect::back()
->withErrors(['message' => \Lang::get('auth.password-forgot.no_account')]);
}
//endUser found, so create an sendResetLinkemail
Password::endUser()->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::route('auth.conformation', ['password-forgot']);
}
/**
* This method is called to reset the password of an endUser
*
* @param AuthService $authService
* @param $type
* @param $token
* @return mixed
*/
public function resetPasswordPage(AuthService $authService, $token, $type = 'endUser')
{
// 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')
]);
}
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, Request $request, $token, $type = 'endUser')
{
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
$user = endUser::where('email', $reset->email)->first();
$user->update(['password' => \Hash::make($request->get('password'))]);
return Redirect::route('auth.conformation', ['password-reset']);
}
public function activateAccountPage(AuthService $authService, $token, $type = 'endUser')
{
// 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')
]);
}
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, Request $request, $token, $type = 'endUser')
{
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
$user = EndUser::where('email', $reset->email)->first();
$user->active = 1;
$user->password = \Hash::make($request->get('password'));
$user->save();
$bag = new MessageBag();
$bag->add('message', \Lang::get('auth.password-reset-page.password_updated'));
return Redirect::route('auth.conformation', ['account_activated']);
}
private function setLocaleToEndUser($user)
{
if (!$locale = $user->language->iso_2) return false;
\App::setLocale($locale);
}
public function confirmationMessagePage($type)
{
return \View::make('auth.confirmation')->withType($type);
}
}