Skip to content

Commit

Permalink
Respect home hubs setting for registration #580
Browse files Browse the repository at this point in the history
  • Loading branch information
nabeelio committed Feb 23, 2020
1 parent 0e13905 commit ccc823b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 40 deletions.
39 changes: 8 additions & 31 deletions app/Http/Controllers/Auth/RegisterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Http\Controllers\Auth;

use App\Contracts\Controller;
use App\Http\Requests\CreateUserRequest;
use App\Models\Enums\UserState;
use App\Models\User;
use App\Repositories\AirlineRepository;
Expand Down Expand Up @@ -59,7 +60,7 @@ public function __construct(
*/
public function showRegistrationForm()
{
$airports = $this->airportRepo->selectBoxList(false, true);
$airports = $this->airportRepo->selectBoxList(false, setting('pilots.home_hubs_only'));
$airlines = $this->airlineRepo->selectBoxList();

return view('auth.register', [
Expand All @@ -81,7 +82,7 @@ protected function validator(array $data)
{
$rules = [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'email' => 'required|email|max:255|unique:users, email',
'airline_id' => 'required',
'home_airport_id' => 'required',
'password' => 'required|min:5|confirmed',
Expand All @@ -98,30 +99,24 @@ protected function validator(array $data)
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @param array $opts
*
* @throws \RuntimeException
* @throws \Exception
*
* @return User
*/
protected function create(array $data)
protected function create(array $opts)
{
// Default options
$opts = array_merge([
'api_key' => Utils::generateApiKey(),
], $data);

$opts['curr_airport_id'] = $data['home_airport_id'];
$opts['password'] = Hash::make($data['password']);
$opts['password'] = Hash::make($opts['password']);

// Convert transfer hours into minutes
if (isset($opts['transfer_time'])) {
$opts['transfer_time'] *= 60;
}

$user = User::create($opts);
$user = $this->userService->createUser($user);
$user = $this->userService->createUser($opts);

Log::info('User registered: ', $user->toArray());

Expand All @@ -137,26 +132,8 @@ protected function create(array $data)
*
* @return mixed
*/
public function register(Request $request)
public function register(CreateUserRequest $request)
{
$rules = [
'name' => 'required',
'email' => 'required|email|unique:users,email',
'airline_id' => 'required',
'home_airport_id' => 'required',
'password' => 'required|confirmed',
'timezone' => 'required',
'country' => 'required',
'transfer_time' => 'integer|min:0',
'toc_accepted' => 'accepted',
];

if (config('captcha.enabled')) {
$rules['g-recaptcha-response'] = 'required|captcha';
}

$this->validate(request(), $rules);

$user = $this->create($request->all());
if ($user->state === UserState::PENDING) {
return view('auth.pending');
Expand Down
17 changes: 14 additions & 3 deletions app/Http/Requests/CreateUserRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,21 @@ public function authorize(): bool
*/
public function rules(): array
{
$rules = User::$rules;
$rules = [
'name' => 'required',
'email' => 'required|email|unique:users,email',
'airline_id' => 'required',
'home_airport_id' => 'required',
'password' => 'required|confirmed',
'timezone' => 'required',
'country' => 'required',
'transfer_time' => 'sometimes|integer|min:0',
'toc_accepted' => 'accepted',
];

$rules['email'] .= '|unique:users,email';
$rules['pilot_id'] .= '|unique:users,pilot_id';
if (config('captcha.enabled')) {
$rules['g-recaptcha-response'] = 'required|captcha';
}

return $rules;
}
Expand Down
11 changes: 8 additions & 3 deletions app/Services/UserService.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use App\Repositories\SubfleetRepository;
use App\Repositories\UserRepository;
use App\Support\Units\Time;
use App\Support\Utils;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
use function is_array;
Expand Down Expand Up @@ -48,15 +49,19 @@ public function __construct(
* Register a pilot. Also attaches the initial roles
* required, and then triggers the UserRegistered event
*
* @param User $user User model
* @param array $attrs Array with the user data
* @param array $roles List of "display_name" of groups to assign
*
* @throws \Exception
*
* @return mixed
* @return User
*/
public function createUser(User $user, array $roles = null)
public function createUser(array $attrs, array $roles = null): User
{
$user = User::create($attrs);
$user->api_key = Utils::generateApiKey();
$user->curr_airport_id = $user->home_airport_id;

// Determine if we want to auto accept
if (setting('pilots.auto_accept') === true) {
$user->state = UserState::ACTIVE;
Expand Down
5 changes: 2 additions & 3 deletions modules/Installer/Http/Controllers/InstallerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ public function step3(Request $request)
* @throws \Prettus\Validator\Exceptions\ValidatorException
* @throws \Exception
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
* @return mixed
*/
public function usersetup(Request $request)
{
Expand Down Expand Up @@ -326,8 +326,7 @@ public function usersetup(Request $request)
'password' => Hash::make($request->get('password')),
];

$user = User::create($attrs);
$user = $this->userService->createUser($user, ['admin']);
$user = $this->userService->createUser($attrs, ['admin']);
Log::info('User registered: ', $user->toArray());

// Set the initial admin e-mail address
Expand Down

0 comments on commit ccc823b

Please sign in to comment.