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

227 lines
6.3 KiB
PHP
Raw Normal View History

2016-09-16 06:19:40 +02:00
<?php
/**
* RegisterController.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
declare(strict_types = 1);
2016-09-16 06:19:40 +02:00
namespace FireflyIII\Http\Controllers\Auth;
2016-09-16 09:02:35 +02:00
use Auth;
use Config;
use FireflyIII\Events\UserRegistration;
2016-09-16 06:19:40 +02:00
use FireflyIII\Http\Controllers\Controller;
2016-09-16 09:02:35 +02:00
use FireflyIII\Support\Facades\FireflyConfig;
use FireflyIII\User;
2016-09-16 06:19:40 +02:00
use Illuminate\Foundation\Auth\RegistersUsers;
2016-09-16 09:02:35 +02:00
use Illuminate\Http\Request;
use Illuminate\Mail\Message;
use Log;
use Mail;
use Session;
use Swift_TransportException;
use Validator;
2016-09-16 06:19:40 +02:00
2016-09-16 09:02:35 +02:00
/**
* Class RegisterController
*
* @package FireflyIII\Http\Controllers\Auth
*/
2016-09-16 06:19:40 +02:00
class RegisterController extends Controller
{
/*
|--------------------------------------------------------------------------
| Register Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users as well as their
| validation and creation. By default this controller uses a trait to
| provide this functionality without requiring any additional code.
|
*/
use RegistersUsers;
/**
* Where to redirect users after login / registration.
*
* @var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*/
public function __construct()
{
2016-09-16 09:02:35 +02:00
parent::__construct();
2016-09-16 06:19:40 +02:00
$this->middleware('guest');
}
/**
2016-09-16 09:02:35 +02:00
* @param Request $request
2016-09-16 06:19:40 +02:00
*
2016-09-16 09:02:35 +02:00
* @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\View\View
2016-09-16 06:19:40 +02:00
*/
2016-09-16 09:02:35 +02:00
public function register(Request $request)
2016-09-16 06:19:40 +02:00
{
2016-09-16 09:02:35 +02:00
// is allowed to?
$singleUserMode = FireflyConfig::get('single_user_mode', Config::get('firefly.configuration.single_user_mode'))->data;
$userCount = User::count();
if ($singleUserMode === true && $userCount > 0) {
$message = 'Registration is currently not available.';
return view('error', compact('message'));
}
$validator = $this->validator($request->all());
if ($validator->fails()) {
$this->throwValidationException($request, $validator);
}
$data = $request->all();
$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->reportBlockedDomainRegistrationAttempt($data['email'], $request->ip());
$this->throwValidationException($request, $validator);
}
$user = $this->create($request->all());
// trigger user registration event:
event(new UserRegistration($user, $request->ip()));
Auth::login($user);
Session::flash('success', strval(trans('firefly.registered')));
Session::flash('gaEventCategory', 'user');
Session::flash('gaEventAction', 'new-registration');
return redirect($this->redirectPath());
}
/**
* OLD
* Show the application registration form.
*
* @return \Illuminate\Http\Response
*/
public function showRegistrationForm(Request $request)
2016-09-16 09:02:35 +02:00
{
2016-09-24 10:55:13 +02:00
$showDemoWarning = config('firefly.show-demo-warning', false);
2016-09-16 09:02:35 +02:00
// is allowed to?
$singleUserMode = FireflyConfig::get('single_user_mode', Config::get('firefly.configuration.single_user_mode'))->data;
$userCount = User::count();
if ($singleUserMode === true && $userCount > 0) {
$message = 'Registration is currently not available.';
return view('error', compact('message'));
}
$email = $request->old('email');
return view('auth.register', compact('showDemoWarning', 'email'));
2016-09-16 06:19:40 +02:00
}
/**
* Create a new user instance after a valid registration.
*
2016-09-16 09:02:35 +02:00
* @param array $data
*
2016-09-16 06:19:40 +02:00
* @return User
*/
protected function create(array $data)
{
2016-09-16 09:02:35 +02:00
return User::create(
[
'email' => $data['email'],
'password' => bcrypt($data['password']),
]
);
}
/**
* 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|min:6|confirmed',
]
);
}
/**
* @return array
*/
private function getBlockedDomains()
{
return FireflyConfig::get('blocked-domains', [])->data;
}
/**
* @param string $email
*
* @return bool
*/
private function isBlockedDomain(string $email)
{
$parts = explode('@', $email);
$blocked = $this->getBlockedDomains();
if (isset($parts[1]) && in_array($parts[1], $blocked)) {
return true;
}
return false;
}
/**
* Send a message home about a blocked domain and the address attempted to register.
*
* @param string $registrationMail
* @param string $ipAddress
*/
private function reportBlockedDomainRegistrationAttempt(string $registrationMail, string $ipAddress)
{
try {
$email = env('SITE_OWNER', false);
$parts = explode('@', $registrationMail);
$domain = $parts[1];
$fields = [
'email_address' => $registrationMail,
'blocked_domain' => $domain,
'ip' => $ipAddress,
];
Mail::send(
['emails.blocked-registration-html', 'emails.blocked-registration'], $fields, function (Message $message) use ($email, $domain) {
$message->to($email, $email)->subject('Blocked a registration attempt with domain ' . $domain . '.');
}
);
} catch (Swift_TransportException $e) {
Log::error($e->getMessage());
}
2016-09-16 06:19:40 +02:00
}
}