firefly-iii/app/Http/Controllers/Auth/AuthController.php

246 lines
6.8 KiB
PHP
Raw Normal View History

2015-02-05 21:52:16 -06:00
<?php namespace FireflyIII\Http\Controllers\Auth;
2015-02-05 21:39:52 -06:00
2015-06-11 14:19:40 -05:00
use Auth;
2015-02-05 21:52:16 -06:00
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\Role;
2015-05-27 00:27:05 -05:00
use FireflyIII\User;
2015-02-05 21:39:52 -06:00
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
2015-07-14 15:48:34 -05:00
use Illuminate\Foundation\Auth\ThrottlesLogins;
2015-03-03 02:29:02 -06:00
use Illuminate\Http\Request;
2015-03-29 00:43:20 -05:00
use Illuminate\Mail\Message;
2015-03-03 02:29:02 -06:00
use Mail;
use Request as Rq;
2015-03-03 02:29:02 -06:00
use Session;
2015-05-01 01:29:41 -05:00
use Twig;
2015-06-11 14:19:40 -05:00
use Validator;
2015-11-01 01:03:41 -06:00
use Log;
use Config;
2015-02-05 21:39:52 -06:00
2015-02-11 00:35:10 -06:00
/**
* Class AuthController
*
* @package FireflyIII\Http\Controllers\Auth
*/
2015-02-07 15:50:47 -06:00
class AuthController extends Controller
{
2015-07-14 15:48:34 -05:00
use AuthenticatesAndRegistersUsers, ThrottlesLogins;
2015-11-01 01:03:41 -06:00
/**
* Log the user out of the application.
*
* @return \Illuminate\Http\Response
*/
public function getLogout()
{
Auth::logout();
Log::debug('Logout and redirect to root.');
return redirect('/login');
}
2015-07-25 11:40:45 -05:00
/**
* Show the application registration form.
*
* @return \Illuminate\Http\Response
*/
public function getRegister()
{
$host = Rq::getHttpHost();
2015-02-07 15:50:47 -06:00
2015-07-25 11:40:45 -05:00
return view('auth.register', compact('host'));
}
2015-07-14 15:48:34 -05:00
/**
* Handle a login request to the application.
*
* @param \Illuminate\Http\Request $request
*
* @return \Illuminate\Http\Response
*/
public function postLogin(Request $request)
{
$this->validate(
$request, [
$this->loginUsername() => 'required', 'password' => 'required',
]
);
// If the class is using the ThrottlesLogins trait, we can automatically throttle
// the login attempts for this application. We'll key this by the username and
// the IP address of the client making these requests into this application.
$throttles = $this->isUsingThrottlesLoginsTrait();
if ($throttles && $this->hasTooManyLoginAttempts($request)) {
return $this->sendLockoutResponse($request);
}
$credentials = $this->getCredentials($request);
2015-07-25 00:03:50 -05:00
$credentials['blocked'] = 0; // most not be blocked.
2015-07-14 15:48:34 -05:00
if (Auth::attempt($credentials, $request->has('remember'))) {
return $this->handleUserWasAuthenticated($request, $throttles);
}
2015-07-25 00:03:50 -05:00
// default error message:
$message = $this->getFailedLoginMessage();
// try to find a blocked user with this email address.
/** @var User $foundUser */
$foundUser = User::where('email', $credentials['email'])->where('blocked', 1)->first();
if (!is_null($foundUser)) {
// if it exists, show message:
$code = $foundUser->blocked_code;
if (strlen($code) == 0) {
2015-11-01 01:06:51 -06:00
$code = 'general_blocked';
}
$message = trans('firefly.' . $code . '_error', ['email' => $credentials['email']]);
2015-07-25 00:03:50 -05:00
}
// try
2015-07-14 15:48:34 -05:00
// If the login attempt was unsuccessful we will increment the number of attempts
// to login and redirect the user back to the login form. Of course, when this
// user surpasses their maximum number of attempts they will get locked out.
if ($throttles) {
$this->incrementLoginAttempts($request);
}
return redirect($this->loginPath())
->withInput($request->only($this->loginUsername(), 'remember'))
->withErrors(
[
2015-07-25 00:03:50 -05:00
$this->loginUsername() => $message,
2015-07-14 15:48:34 -05:00
]
);
}
2015-02-07 15:50:47 -06:00
public $redirectTo = '/';
/**
* Create a new authentication controller instance.
*
2015-05-10 06:06:02 -05:00
* @codeCoverageIgnore
2015-02-07 15:50:47 -06:00
*
*/
2015-06-11 14:19:40 -05:00
public function __construct()
2015-02-07 15:50:47 -06:00
{
2015-07-08 23:13:39 -05:00
parent::__construct();
2015-02-07 15:50:47 -06:00
$this->middleware('guest', ['except' => 'getLogout']);
}
2015-02-05 21:39:52 -06:00
2015-05-01 01:29:41 -05:00
/**
* Show the application login form.
*
2015-05-10 06:06:02 -05:00
* @codeCoverageIgnore
2015-05-01 01:29:41 -05:00
* @return \Illuminate\Http\Response
2015-05-10 06:06:02 -05:00
*
2015-05-01 01:29:41 -05:00
*/
public function getLogin()
{
return Twig::render('auth.login');
}
2015-03-03 02:29:02 -06:00
/**
* Handle a registration request for the application.
*
* @param Request $request
*
* @return \Illuminate\Http\RedirectResponse
2015-03-03 02:29:02 -06:00
*/
public function postRegister(Request $request)
{
2015-06-11 14:19:40 -05:00
$validator = $this->validator($request->all());
2015-03-03 02:29:02 -06:00
if ($validator->fails()) {
$this->throwValidationException(
$request, $validator
);
2015-05-10 06:06:02 -05:00
// @codeCoverageIgnoreStart
2015-03-03 02:29:02 -06:00
}
2015-05-10 06:06:02 -05:00
// @codeCoverageIgnoreEnd
2015-03-03 02:29:02 -06:00
2015-03-29 14:27:51 -05:00
$data = $request->all();
2015-03-25 16:29:32 -05:00
$data['password'] = bcrypt($data['password']);
// is user email domain blocked?
$parts = explode('@', $data['email']);
if (isset($parts[1]) && in_array($parts[1], Config::get('mail.blocked_domains'))) {
$validator->getMessageBag()->add('email', trans('validation.invalid_domain'));
$this->throwValidationException(
$request, $validator
);
}
2015-06-11 14:19:40 -05:00
Auth::login($this->create($data));
2015-03-03 02:29:02 -06:00
// get the email address
2015-06-11 14:19:40 -05:00
if (Auth::user() instanceof User) {
$email = Auth::user()->email;
2015-06-08 11:42:19 -05:00
$address = route('index');
2015-05-27 00:27:05 -05:00
// send email.
Mail::send(
2015-06-08 11:42:19 -05:00
['emails.registered-html', 'emails.registered'], ['address' => $address], function (Message $message) use ($email) {
$message->to($email, $email)->subject('Welcome to Firefly III! ');
2015-05-27 00:27:05 -05:00
}
);
2015-03-03 02:29:02 -06:00
2015-05-27 00:27:05 -05:00
// set flash message
Session::flash('success', 'You have registered successfully!');
Session::flash('gaEventCategory', 'user');
Session::flash('gaEventAction', 'new-registration');
2015-03-03 02:29:02 -06:00
// first user ever?
if (User::count() == 1) {
$admin = Role::where('name', 'owner')->first();
2015-06-11 14:19:40 -05:00
Auth::user()->attachRole($admin);
}
2015-03-03 02:29:02 -06:00
2015-05-27 00:27:05 -05:00
return redirect($this->redirectPath());
}
2015-06-13 01:17:38 -05:00
// @codeCoverageIgnoreStart
2015-07-07 12:09:45 -05:00
abort(500, 'Not a user!');
2015-03-03 02:29:02 -06:00
2015-05-27 00:51:33 -05:00
return redirect('/');
2015-06-13 01:17:38 -05:00
// @codeCoverageIgnoreEnd
2015-03-03 02:29:02 -06:00
}
2015-06-11 14:19:40 -05:00
/**
* Get a validator for an incoming registration request.
*
* @param array $data
*
* @return \Illuminate\Contracts\Validation\Validator
*/
public function validator(array $data)
{
return Validator::make(
$data, [
'email' => 'required|email|max:255|unique:users',
'password' => 'required|confirmed|min:6',
]
);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
*
* @return User
*/
public function create(array $data)
{
return User::create(
[
'email' => $data['email'],
'password' => $data['password'],
]
);
}
2015-02-05 21:39:52 -06:00
}