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

250 lines
6.9 KiB
PHP
Raw Normal View History

2015-02-06 04:52:16 +01:00
<?php namespace FireflyIII\Http\Controllers\Auth;
2015-02-06 04:39:52 +01:00
2015-06-11 21:19:40 +02:00
use Auth;
2015-02-06 04:52:16 +01:00
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\Role;
2015-05-27 07:27:05 +02:00
use FireflyIII\User;
2015-02-06 04:39:52 +01:00
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
2015-07-14 22:48:34 +02:00
use Illuminate\Foundation\Auth\ThrottlesLogins;
2015-03-03 09:29:02 +01:00
use Illuminate\Http\Request;
2015-03-29 07:43:20 +02:00
use Illuminate\Mail\Message;
2015-03-03 09:29:02 +01:00
use Mail;
use Request as Rq;
2015-03-03 09:29:02 +01:00
use Session;
2015-05-01 08:29:41 +02:00
use Twig;
2015-06-11 21:19:40 +02:00
use Validator;
2015-11-01 08:03:41 +01:00
use Log;
use Config;
2015-02-06 04:39:52 +01:00
2015-02-11 07:35:10 +01:00
/**
* Class AuthController
*
* @package FireflyIII\Http\Controllers\Auth
*/
2015-02-07 22:50:47 +01:00
class AuthController extends Controller
{
2015-07-14 22:48:34 +02:00
use AuthenticatesAndRegistersUsers, ThrottlesLogins;
2015-11-01 08:03:41 +01:00
/**
* Log the user out of the application.
*
* @return \Illuminate\Http\Response
*/
public function getLogout()
{
Auth::logout();
2015-12-05 17:45:33 +01:00
return redirect('/auth/login');
2015-11-01 08:03:41 +01:00
}
2015-07-25 18:40:45 +02:00
/**
* Show the application registration form.
*
* @return \Illuminate\Http\Response
*/
public function getRegister()
{
$host = Rq::getHttpHost();
2015-02-07 22:50:47 +01:00
2015-07-25 18:40:45 +02:00
return view('auth.register', compact('host'));
}
2015-07-14 22:48:34 +02: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 07:03:50 +02:00
$credentials['blocked'] = 0; // most not be blocked.
2015-07-14 22:48:34 +02:00
if (Auth::attempt($credentials, $request->has('remember'))) {
return $this->handleUserWasAuthenticated($request, $throttles);
}
2015-07-25 07:03:50 +02: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 08:06:51 +01:00
$code = 'general_blocked';
}
$message = trans('firefly.' . $code . '_error', ['email' => $credentials['email']]);
2015-07-25 07:03:50 +02:00
}
// try
2015-07-14 22:48:34 +02: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 07:03:50 +02:00
$this->loginUsername() => $message,
2015-07-14 22:48:34 +02:00
]
);
}
2015-02-07 22:50:47 +01:00
public $redirectTo = '/';
/**
* Create a new authentication controller instance.
*
2015-05-10 13:06:02 +02:00
* @codeCoverageIgnore
2015-02-07 22:50:47 +01:00
*
*/
2015-06-11 21:19:40 +02:00
public function __construct()
2015-02-07 22:50:47 +01:00
{
2015-07-09 06:13:39 +02:00
parent::__construct();
2015-02-07 22:50:47 +01:00
$this->middleware('guest', ['except' => 'getLogout']);
}
2015-02-06 04:39:52 +01:00
2015-05-01 08:29:41 +02:00
/**
* Show the application login form.
*
2015-05-10 13:06:02 +02:00
* @codeCoverageIgnore
2015-05-01 08:29:41 +02:00
* @return \Illuminate\Http\Response
2015-05-10 13:06:02 +02:00
*
2015-05-01 08:29:41 +02:00
*/
public function getLogin()
{
return Twig::render('auth.login');
}
2015-03-03 09:29:02 +01:00
/**
* Handle a registration request for the application.
*
* @param Request $request
*
* @return \Illuminate\Http\RedirectResponse
2015-03-03 09:29:02 +01:00
*/
public function postRegister(Request $request)
{
2015-06-11 21:19:40 +02:00
$validator = $this->validator($request->all());
2015-03-03 09:29:02 +01:00
if ($validator->fails()) {
$this->throwValidationException(
$request, $validator
);
2015-05-10 13:06:02 +02:00
// @codeCoverageIgnoreStart
2015-03-03 09:29:02 +01:00
}
2015-05-10 13:06:02 +02:00
// @codeCoverageIgnoreEnd
2015-03-03 09:29:02 +01:00
2015-03-29 21:27:51 +02:00
$data = $request->all();
2015-03-25 22:29:32 +01: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 21:19:40 +02:00
Auth::login($this->create($data));
2015-03-03 09:29:02 +01:00
// get the email address
2015-06-11 21:19:40 +02:00
if (Auth::user() instanceof User) {
$email = Auth::user()->email;
2015-06-08 18:42:19 +02:00
$address = route('index');
2015-12-07 14:41:04 +01:00
$ipAddress = $request->ip();
2015-05-27 07:27:05 +02:00
// send email.
try {
Mail::send(
2015-12-07 14:41:04 +01:00
['emails.registered-html', 'emails.registered'], ['address' => $address, 'ip' => $ipAddress], function (Message $message) use ($email) {
$message->to($email, $email)->subject('Welcome to Firefly III! ');
}
);
} catch(\Swift_TransportException $e) {
Log::error($e->getMessage());
2015-05-27 07:27:05 +02:00
}
2015-03-03 09:29:02 +01:00
2015-05-27 07:27:05 +02:00
// set flash message
Session::flash('success', 'You have registered successfully!');
Session::flash('gaEventCategory', 'user');
Session::flash('gaEventAction', 'new-registration');
2015-03-03 09:29:02 +01:00
// first user ever?
if (User::count() == 1) {
$admin = Role::where('name', 'owner')->first();
2015-06-11 21:19:40 +02:00
Auth::user()->attachRole($admin);
}
2015-03-03 09:29:02 +01:00
2015-05-27 07:27:05 +02:00
return redirect($this->redirectPath());
}
2015-06-13 08:17:38 +02:00
// @codeCoverageIgnoreStart
2015-07-07 19:09:45 +02:00
abort(500, 'Not a user!');
2015-03-03 09:29:02 +01:00
2015-05-27 07:51:33 +02:00
return redirect('/');
2015-06-13 08:17:38 +02:00
// @codeCoverageIgnoreEnd
2015-03-03 09:29:02 +01:00
}
2015-06-11 21:19:40 +02: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-06 04:39:52 +01:00
}