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

195 lines
6.4 KiB
PHP
Raw Normal View History

2016-09-16 06:19:40 +02:00
<?php
2022-12-29 19:41:57 +01:00
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
*/
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;
2020-08-14 09:59:56 +02:00
use FireflyIII\Exceptions\FireflyException;
2017-09-09 22:03:27 +02:00
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Notifications\Notifiables\OwnerNotifiable;
2022-10-01 12:21:42 +02:00
use FireflyIII\Repositories\User\UserRepositoryInterface;
2018-08-10 17:05:37 +02:00
use FireflyIII\Support\Http\Controllers\CreateStuff;
2017-09-14 17:40:02 +02:00
use FireflyIII\User;
use Illuminate\Contracts\Foundation\Application;
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;
2021-09-18 10:20:19 +02:00
use Illuminate\Validation\ValidationException;
use Illuminate\View\View;
2022-03-19 11:19:58 +01:00
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
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.
2017-12-17 14:30:53 +01:00
*/
2016-09-16 06:19:40 +02:00
class RegisterController extends Controller
{
2022-10-30 14:24:19 +01:00
use CreateStuff;
2023-11-04 14:18:49 +01:00
use RegistersUsers;
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');
2020-08-14 09:59:56 +02:00
if ('web' !== config('firefly.authentication_guard')) {
2020-08-14 09:59:56 +02:00
throw new FireflyException('Using external identity provider. Cannot continue.');
}
2016-09-16 06:19:40 +02:00
}
/**
* Handle a registration request for the application.
*
* @return Application|Redirector|RedirectResponse
2023-12-20 19:35:52 +01:00
*
2021-05-24 08:54:58 +02:00
* @throws FireflyException
2021-09-18 10:20:19 +02:00
* @throws ValidationException
*/
public function register(Request $request)
{
2022-03-19 11:19:58 +01:00
$allowRegistration = $this->allowedToRegister();
2022-12-29 19:41:57 +01:00
$inviteCode = (string)$request->get('invite_code');
2022-10-01 12:21:42 +02:00
$repository = app(UserRepositoryInterface::class);
$validCode = $repository->validateInviteCode($inviteCode);
2018-10-13 15:06:56 +02:00
2022-10-01 12:21:42 +02:00
if (false === $allowRegistration && false === $validCode) {
2021-08-02 20:30:26 +02:00
throw new FireflyException('Registration is currently not available :(');
}
$this->validator($request->all())->validate();
$user = $this->createUser($request->all());
2023-10-29 06:31:27 +01:00
app('log')->info(sprintf('Registered new user %s', $user->email));
2024-12-14 05:45:54 +01:00
$owner = new OwnerNotifiable();
event(new RegisteredUser($owner, $user));
$this->guard()->login($user);
2022-12-29 19:41:57 +01:00
session()->flash('success', (string)trans('firefly.registered'));
2018-07-26 06:27:52 +02:00
$this->registered($request, $user);
2022-10-01 12:21:42 +02:00
if ($validCode) {
$repository->redeemCode($inviteCode);
}
2018-07-26 06:27:52 +02:00
return redirect($this->redirectPath());
}
/**
* @throws FireflyException
*/
protected function allowedToRegister(): bool
{
// is allowed to register?
$allowRegistration = true;
try {
$singleUserMode = app('fireflyconfig')->get('single_user_mode', config('firefly.configuration.single_user_mode'))->data;
} catch (ContainerExceptionInterface|NotFoundExceptionInterface $e) {
$singleUserMode = true;
}
$userCount = User::count();
$guard = config('auth.defaults.guard');
if (true === $singleUserMode && $userCount > 0 && 'web' === $guard) {
$allowRegistration = false;
}
if ('web' !== $guard) {
$allowRegistration = false;
}
return $allowRegistration;
}
/**
2022-12-29 19:41:57 +01:00
* Show the application registration form if the invitation code is valid.
*
* @return Factory|View
2023-12-20 19:35:52 +01:00
*
2021-05-24 08:54:58 +02:00
* @throws FireflyException
*/
2022-12-29 19:41:57 +01:00
public function showInviteForm(Request $request, string $code)
{
2019-02-13 17:38:41 +01:00
$isDemoSite = app('fireflyconfig')->get('is_demo_site', config('firefly.configuration.is_demo_site'))->data;
2022-12-29 19:41:57 +01:00
$pageTitle = (string)trans('firefly.register_page_title');
$repository = app(UserRepositoryInterface::class);
2022-03-19 11:19:58 +01:00
$allowRegistration = $this->allowedToRegister();
2022-12-29 19:41:57 +01:00
$inviteCode = $code;
$validCode = $repository->validateInviteCode($inviteCode);
2018-10-13 15:06:56 +02:00
2022-12-29 19:41:57 +01:00
if (true === $allowRegistration) {
$message = 'You do not need an invite code on this installation.';
return view('error', compact('message'));
}
if (false === $validCode) {
$message = 'Invalid code.';
return view('error', compact('message'));
}
$email = $request->old('email');
2022-12-29 19:41:57 +01:00
return view('auth.register', compact('isDemoSite', 'email', 'pageTitle', 'inviteCode'));
}
2022-10-01 12:21:42 +02:00
/**
2022-12-29 19:41:57 +01:00
* Show the application registration form.
2022-10-01 12:21:42 +02:00
*
* @return Factory|View
2023-12-20 19:35:52 +01:00
*
2022-10-01 12:21:42 +02:00
* @throws FireflyException
*/
2022-12-29 19:41:57 +01:00
public function showRegistrationForm(Request $request)
2022-10-01 12:21:42 +02:00
{
$isDemoSite = app('fireflyconfig')->get('is_demo_site', config('firefly.configuration.is_demo_site'))->data;
2022-12-29 19:41:57 +01:00
$pageTitle = (string)trans('firefly.register_page_title');
2022-10-01 12:21:42 +02:00
$allowRegistration = $this->allowedToRegister();
2022-12-29 19:41:57 +01:00
if (false === $allowRegistration) {
$message = 'Registration is currently not available. If you are the administrator, you can enable this in the administration.';
2022-10-01 12:21:42 +02:00
return view('error', compact('message'));
}
$email = $request->old('email');
2022-10-01 12:21:42 +02:00
2022-12-29 19:41:57 +01:00
return view('auth.register', compact('isDemoSite', 'email', 'pageTitle'));
2022-10-01 12:21:42 +02:00
}
2016-09-16 06:19:40 +02:00
}