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

277 lines
7.4 KiB
PHP
Raw Normal View History

<?php
2016-02-05 05:08:25 -06:00
declare(strict_types = 1);
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;
use Illuminate\Support\Facades\Lang;
use Log;
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-06-11 14:19:40 -05:00
use Validator;
2015-02-05 21:39:52 -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
/**
* Where to redirect users after login / registration.
2015-11-01 01:03:41 -06:00
*
* @var string
2015-11-01 01:03:41 -06:00
*/
protected $redirectTo = '/home';
/**
* Create a new authentication controller instance.
*
*/
public function __construct()
{
$this->middleware('guest', ['except' => 'logout']);
parent::__construct();
2015-11-01 01:03:41 -06:00
}
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 login(Request $request)
2015-07-14 15:48:34 -05:00
{
$this->validate($request, [$this->loginUsername() => 'required', 'password' => 'required',]);
2015-07-14 15:48:34 -05:00
$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
2016-01-08 11:29:47 -06:00
if (Auth::guard($this->getGuard())->attempt($credentials, $request->has('remember'))) {
2016-01-19 11:10:07 -06:00
2015-07-14 15:48:34 -05:00
return $this->handleUserWasAuthenticated($request, $throttles);
}
// check if user is blocked:
$message = '';
/** @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
}
2015-07-14 15:48:34 -05:00
if ($throttles) {
$this->incrementLoginAttempts($request);
}
return $this->sendFailedLoginResponse($request, $message);
}
2015-03-03 02:29:02 -06:00
/**
* Handle a registration request for the application.
*
* @param \Illuminate\Http\Request $request
2015-03-03 02:29:02 -06:00
*
* @return \Illuminate\Http\Response
2015-03-03 02:29:02 -06:00
*/
public function register(Request $request)
2015-03-03 02:29:02 -06:00
{
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-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?
if ($this->isBlockedDomain($data['email'])) {
$validator->getMessageBag()->add('email', (string)trans('validation.invalid_domain'));
$this->throwValidationException(
$request, $validator
);
}
Auth::login($this->create($request->all()));
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;
$address = route('index');
2015-12-07 07:41:04 -06:00
$ipAddress = $request->ip();
2015-05-27 00:27:05 -05:00
// send email.
try {
Mail::send(
2015-12-07 07:41:04 -06: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 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-07-07 12:09:45 -05:00
abort(500, 'Not a user!');
2015-03-03 02:29:02 -06:00
return redirect($this->redirectPath());
2015-03-03 02:29:02 -06:00
}
2016-01-19 11:10:07 -06:00
/**
* Show the application registration form.
*
* @return \Illuminate\Http\Response
*/
public function showRegistrationForm()
{
$host = Rq::getHttpHost();
return view('auth.register', compact('host'));
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
*
* @return User
*/
protected function create(array $data)
{
return User::create(
[
'email' => $data['email'],
'password' => bcrypt($data['password']),
]
);
}
/**
* @return array
*/
2015-12-18 09:38:50 -06:00
protected function getBlockedDomains()
{
2016-01-08 11:51:10 -06:00
$set = explode(',', env('BLOCKED_DOMAINS', ''));
$domains = [];
2015-12-18 09:38:50 -06:00
foreach ($set as $entry) {
$domain = trim($entry);
2015-12-18 09:38:50 -06:00
if (strlen($domain) > 0) {
$domains[] = $domain;
}
}
2015-12-18 09:38:50 -06:00
return $domains;
}
2016-01-19 11:10:07 -06:00
/**
* Get the failed login message.
*
2016-02-05 02:25:15 -06:00
* @param string $message
2016-01-19 11:10:07 -06:00
*
* @return string
*/
2016-02-05 02:25:15 -06:00
protected function getFailedLoginMessage(string $message)
2016-01-19 11:10:07 -06:00
{
if (strlen($message) > 0) {
return $message;
}
return Lang::has('auth.failed')
? Lang::get('auth.failed')
: 'These credentials do not match our records.';
}
/**
2016-02-05 02:25:15 -06:00
* @param string $email
*
* @return bool
*/
2016-02-05 02:25:15 -06:00
protected function isBlockedDomain(string $email)
{
$parts = explode('@', $email);
$blocked = $this->getBlockedDomains();
if (isset($parts[1]) && in_array($parts[1], $blocked)) {
return true;
}
2015-12-18 09:38:50 -06:00
return false;
}
2016-01-19 11:10:07 -06:00
/**
* Get the failed login response instance.
*
* @param \Illuminate\Http\Request $request
2016-02-05 02:25:15 -06:00
* @param string $message
2016-01-19 11:10:07 -06:00
*
* @return \Illuminate\Http\Response
*/
2016-02-05 02:25:15 -06:00
protected function sendFailedLoginResponse(Request $request, string $message)
2016-01-19 11:10:07 -06:00
{
return redirect()->back()
->withInput($request->only($this->loginUsername(), 'remember'))
->withErrors(
[
$this->loginUsername() => $this->getFailedLoginMessage($message),
]
);
}
/**
* Get a validator for an incoming registration request.
*
* @param array $data
*
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make(
$data, [
'email' => 'required|email|max:255|unique:users',
'password' => 'required|confirmed|min:6',
]
);
}
2015-02-05 21:39:52 -06:00
}