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

153 lines
4.8 KiB
PHP
Raw Normal View History

2016-09-16 06:19:40 +02:00
<?php
2017-10-21 08:40:00 +02:00
/**
* RegisterController.php
2020-01-31 07:32:04 +01:00
* Copyright (c) 2019 james@firefly-iii.org
2017-10-21 08:40:00 +02:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2017-10-21 08:40:00 +02:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
2017-10-21 08:40:00 +02:00
*
* This program is distributed in the hope that it will be useful,
2017-10-21 08:40:00 +02:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2017-10-21 08:40:00 +02:00
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2017-10-21 08:40:00 +02:00
*/
2018-07-08 07:59:58 +02:00
/** @noinspection PhpDynamicAsStaticMethodCallInspection */
2017-09-14 17:40:02 +02:00
declare(strict_types=1);
2016-09-16 06:19:40 +02:00
namespace FireflyIII\Http\Controllers\Auth;
use FireflyIII\Events\RegisteredUser;
2017-09-09 22:03:27 +02:00
use FireflyIII\Http\Controllers\Controller;
2018-08-10 17:05:37 +02:00
use FireflyIII\Support\Http\Controllers\CreateStuff;
use FireflyIII\Support\Http\Controllers\RequestInformation;
2017-09-14 17:40:02 +02:00
use FireflyIII\User;
use Illuminate\Contracts\View\Factory;
2016-09-16 06:19:40 +02:00
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Routing\Redirector;
use Illuminate\View\View;
use Log;
2018-07-13 15:50:42 +02:00
2017-12-17 14:30:53 +01:00
/**
* Class RegisterController
2017-12-17 18:23:10 +01:00
*
* 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.
2018-07-22 08:10:16 +02:00
*
* @codeCoverageIgnore
2017-12-17 14:30:53 +01:00
*/
2016-09-16 06:19:40 +02:00
class RegisterController extends Controller
{
2018-08-10 17:05:37 +02:00
use RegistersUsers, RequestInformation, CreateStuff;
2016-09-16 06:19:40 +02:00
/**
2017-09-09 22:03:27 +02:00
* Where to redirect users after registration.
2016-09-16 06:19:40 +02:00
*
* @var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*/
public function __construct()
{
parent::__construct();
2016-09-16 06:19:40 +02:00
$this->middleware('guest');
}
/**
* Handle a registration request for the application.
*
2018-07-08 12:28:42 +02:00
* @param Request $request
*
* @return Factory|RedirectResponse|Redirector|View
*/
public function register(Request $request)
{
// is allowed to?
2018-10-13 15:06:56 +02:00
$allowRegistration = true;
$loginProvider = config('firefly.login_provider');
2019-02-13 17:38:41 +01:00
$singleUserMode = app('fireflyconfig')->get('single_user_mode', config('firefly.configuration.single_user_mode'))->data;
2018-10-13 15:06:56 +02:00
$userCount = User::count();
if (true === $singleUserMode && $userCount > 0 && 'eloquent' === $loginProvider) {
$allowRegistration = false;
}
if ('eloquent' !== $loginProvider) {
$allowRegistration = false;
}
if (false === $allowRegistration) {
$message = 'Registration is currently not available.';
return view('error', compact('message'));
}
$this->validator($request->all())->validate();
$user = $this->createUser($request->all());
Log::info(sprintf('Registered new user %s', $user->email));
event(new RegisteredUser($user, $request->ip()));
$this->guard()->login($user);
session()->flash('success', (string) trans('firefly.registered'));
2018-07-26 06:27:52 +02:00
$this->registered($request, $user);
2020-05-24 11:24:28 +02:00
// telemetry
2020-07-24 16:41:41 +02:00
app('telemetry')->feature('system.users.count', (string)User::count());
2020-05-24 11:24:28 +02:00
2018-07-26 06:27:52 +02:00
return redirect($this->redirectPath());
}
/**
* Show the application registration form.
*
* @param Request $request
*
* @return Factory|View
*/
public function showRegistrationForm(Request $request)
{
2018-10-13 15:06:56 +02:00
$allowRegistration = true;
$loginProvider = config('firefly.login_provider');
2019-02-13 17:38:41 +01:00
$isDemoSite = app('fireflyconfig')->get('is_demo_site', config('firefly.configuration.is_demo_site'))->data;
$singleUserMode = app('fireflyconfig')->get('single_user_mode', config('firefly.configuration.single_user_mode'))->data;
2018-10-13 15:06:56 +02:00
$userCount = User::count();
$pageTitle = (string) trans('firefly.register_page_title');
2018-10-13 15:06:56 +02:00
if (true === $isDemoSite) {
$allowRegistration = false;
}
2018-10-13 15:06:56 +02:00
if (true === $singleUserMode && $userCount > 0 && 'eloquent' === $loginProvider) {
$allowRegistration = false;
}
if ('eloquent' !== $loginProvider) {
$allowRegistration = false;
}
if (false === $allowRegistration) {
$message = 'Registration is currently not available.';
return view('error', compact('message'));
}
$email = $request->old('email');
return view('auth.register', compact('isDemoSite', 'email', 'pageTitle'));
}
2016-09-16 06:19:40 +02:00
}