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

156 lines
4.6 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
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2017-12-17 14:41:58 +01:00
* along with Firefly III. If not, see <http://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 FireflyConfig;
2017-09-09 22:03:27 +02:00
use FireflyIII\Http\Controllers\Controller;
2017-09-14 17:40:02 +02:00
use FireflyIII\User;
use Illuminate\Auth\Events\Registered;
2018-07-13 15:50:42 +02:00
use Illuminate\Contracts\Validation\Validator as ValidatorContract;
2016-09-16 06:19:40 +02:00
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Http\Request;
2017-09-14 17:40:02 +02:00
use Illuminate\Support\Facades\Validator;
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
{
use RegistersUsers;
/**
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
*
2018-07-08 12:28:42 +02:00
* @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\View\View
*/
public function register(Request $request)
{
// is allowed to?
$singleUserMode = FireflyConfig::get('single_user_mode', config('firefly.configuration.single_user_mode'))->data;
$userCount = User::count();
2017-11-15 12:25:49 +01:00
if (true === $singleUserMode && $userCount > 0) {
$message = 'Registration is currently not available.';
return view('error', compact('message'));
}
2018-07-09 19:24:08 +02:00
/** @noinspection PhpUndefinedMethodInspection */
$this->validator($request->all())->validate();
event(new Registered($user = $this->create($request->all())));
$this->guard()->login($user);
2018-04-22 17:10:11 +02:00
session()->flash('success', (string)trans('firefly.registered'));
return $this->registered($request, $user)
?: redirect($this->redirectPath());
}
/**
* Show the application registration form.
*
* @param Request $request
*
2018-07-08 12:28:42 +02:00
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function showRegistrationForm(Request $request)
{
// is demo site?
$isDemoSite = FireflyConfig::get('is_demo_site', config('firefly.configuration.is_demo_site'))->data;
// is allowed to?
$singleUserMode = FireflyConfig::get('single_user_mode', config('firefly.configuration.single_user_mode'))->data;
$userCount = User::count();
2017-11-15 12:25:49 +01:00
if (true === $singleUserMode && $userCount > 0) {
$message = 'Registration is currently not available.';
return view('error', compact('message'));
}
$email = $request->old('email');
return view('auth.register', compact('isDemoSite', 'email'));
}
2016-09-16 06:19:40 +02:00
/**
2017-09-14 17:40:02 +02:00
* Create a new user instance after a valid registration.
2016-10-09 07:58:27 +02:00
*
2017-11-15 12:25:49 +01:00
* @param array $data
2017-09-14 17:40:02 +02:00
*
* @return \FireflyIII\User
2016-09-16 09:02:35 +02:00
*/
2018-07-08 12:28:42 +02:00
protected function create(array $data): User
2016-09-16 09:02:35 +02:00
{
2017-09-14 17:40:02 +02:00
return User::create(
[
'email' => $data['email'],
'password' => bcrypt($data['password']),
]
);
2016-09-16 06:19:40 +02:00
}
/**
2017-09-14 17:40:02 +02:00
* Get a validator for an incoming registration request.
2016-09-16 06:19:40 +02:00
*
2017-11-15 12:25:49 +01:00
* @param array $data
2017-09-14 17:40:02 +02:00
*
2018-07-08 12:28:42 +02:00
* @return ValidatorContract
2016-09-16 06:19:40 +02:00
*/
2018-07-08 12:28:42 +02:00
protected function validator(array $data): ValidatorContract
2016-09-16 06:19:40 +02:00
{
2017-09-14 17:40:02 +02:00
return Validator::make(
2017-11-15 10:52:29 +01:00
$data,
[
2017-11-15 11:33:07 +01:00
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|secure_password|confirmed',
]
2017-09-14 17:40:02 +02:00
);
2016-09-16 09:02:35 +02:00
}
2016-09-16 06:19:40 +02:00
}