File: D:/HostingSpaces/SBogers95/rentman.io/app/Komma/Users/AuthController.php
<?php
namespace App\Komma\Users;
use App\Komma\Auth\AuthService;
use App\Komma\Users\Models\User;
use Auth;
use Carbon\Carbon;
use Config;
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 Redirect;
use View;
/**
* @author Komma <info@komma.pro>
* @copyright (c) 2012-2016, Komma
*/
class AuthController extends Controller
{
/**
* Show the login form.
*
* @return \Illuminate\View\View
*/
public function login()
{
return View::make('kms/auth.login');
}
/**
* Handle the response from the login form.
*
* @return \Illuminate\Http\RedirectResponse
*/
public function processLogin(AuthService $authService, Request $request)
{
$loginMethod = 'email';
// Get user by email
$user = $authService->getAccountBy('user', 'email', $request->get('email'));
// If no user found with that email, try to get it by username
if (! $user) {
$user = $authService->getAccountBy('user', 'username', $request->get('email'));
$loginMethod = 'username';
}
//Check if the account exists
if (! $user) {
//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 user has waited long enough, so he can try again
$user->failed_login_attempts = 0;
}
//Check if the user 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_attempt to now
$user->last_login_attempt = Carbon::now();
// Attempt to login
if (! Auth::attempt([$loginMethod => Input::get('email'), 'password' => Input::get('password')])) {
//Add 1 to failed_login_attempts
$user->failed_login_attempts++;
//Save the user
$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 user
$user->save();
//Redirect
return Redirect::intended('/kms')->withSuccess('Logged in');
}
/**
* Logout and redirect to the homepage.
*
* @return \Illuminate\Http\RedirectResponse
*/
public function logout()
{
Auth::logout();
return Redirect::to('/kms/');
}
/**
* Show the forgot Password page
*
* @return mixed
*/
public function forgotPassword()
{
return View::make('kms/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 user has an account
if (! $user = $authService->getAccountBy('user', 'email', $request->get('email'))) {
//No user found. trow error
return Redirect::back()
->withErrors(['message' => \Lang::get('auth.password-forgot.no_account')]);
}
// dd($user);
//user found, so create an sendResetLinkemail
Password::user()->sendResetLink(Input::only('email'), function (Message $message) {
$message->subject(\Lang::get('auth.password-forgot-email.subject'));
});
//Add a messageBag for success
$bag = new MessageBag();
$bag->add('message', \Lang::get('auth.password-forgot-email.confirmation'));
return Redirect::to('kms/login')->withSuccess($bag);
}
/**
* This method is called to reset the password of an user/member
*
* @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('kms/login')->withErrors([
'message' => \Lang::get('auth.password-reset-page.wrong_token'),
]);
}
//Load the password rest form
if ($type == 'user') {
return View::make('kms/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
$user = User::where('email', $reset->email);
$user->update(['password' => \Hash::make($request->get('password'))]);
$bag = new MessageBag();
$bag->add('message', \Lang::get('auth.password-reset-page.password_updated'));
return Redirect::to('kms/login')->withSuccess($bag);
}
}