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;
|
2015-05-27 23:43:07 -05:00
|
|
|
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-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 Session;
|
2015-05-01 01:29:41 -05:00
|
|
|
use Twig;
|
2015-06-11 14:19:40 -05:00
|
|
|
use Validator;
|
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
|
|
|
|
{
|
|
|
|
|
|
|
|
/*
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
| Registration & Login Controller
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
| This controller handles the registration of new users, as well as the
|
|
|
|
| authentication of existing users. By default, this controller uses
|
|
|
|
| a simple trait to add these behaviors. Why don't you explore it?
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
use AuthenticatesAndRegistersUsers;
|
|
|
|
|
|
|
|
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
|
|
|
|
*
|
2015-05-26 12:50:09 -05:00
|
|
|
* @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']);
|
|
|
|
|
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
|
|
|
|
2015-05-27 23:43:07 -05: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-05-27 23:43:07 -05:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|