Respect home hubs setting for registration #580 (#581)

Respect home hubs setting for registration #580
This commit is contained in:
Nabeel S 2020-02-23 17:21:26 -05:00 committed by GitHub
parent b34dc4868e
commit a1d6fa17ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 62 additions and 64 deletions

View File

@ -74,7 +74,7 @@ reload-db:
tests: test
.PHONY: test
test:
test: phpcs
#php artisan database:create --reset
vendor/bin/phpunit --debug --verbose

View File

@ -8,7 +8,7 @@ $factory->define(App\Models\User::class, function (Faker $faker) {
return [
'id' => null,
'pilot_id' => 0,
'pilot_id' => null,
'name' => $faker->name,
'email' => $faker->safeEmail,
'password' => $password ?: $password = Hash::make('secret'),

View File

@ -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;
@ -10,7 +11,6 @@ use App\Repositories\AirportRepository;
use App\Services\UserService;
use App\Support\Countries;
use App\Support\Timezonelist;
use App\Support\Utils;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Http\Request;
@ -59,7 +59,7 @@ class RegisterController extends Controller
*/
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', [
@ -81,7 +81,7 @@ class RegisterController extends Controller
{
$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',
@ -98,30 +98,24 @@ class RegisterController extends Controller
/**
* 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());
@ -137,26 +131,8 @@ class RegisterController extends Controller
*
* @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');

View File

@ -24,10 +24,21 @@ class CreateUserRequest extends FormRequest
*/
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;
}

View File

@ -17,6 +17,7 @@ use App\Repositories\AircraftRepository;
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;
@ -48,15 +49,19 @@ class UserService extends Service
* 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;

View File

@ -59,7 +59,7 @@
"codedungeon/phpunit-result-printer": "^0.13.0",
"filp/whoops": "~2.0",
"fzaninotto/faker": "~1.9.0",
"friendsofphp/php-cs-fixer": "^2.15",
"friendsofphp/php-cs-fixer": "^2.16",
"mockery/mockery": "0.9.*",
"nunomaduro/collision": "^3.0",
"phpunit/phpunit": "~8.3",

12
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "b68aa676232997053c94551f734d2081",
"content-hash": "5f8a87c7717ad2b6ed1d6cf1c585cc3d",
"packages": [
{
"name": "akaunting/money",
@ -4232,16 +4232,16 @@
},
{
"name": "ramsey/uuid",
"version": "3.9.2",
"version": "3.9.3",
"source": {
"type": "git",
"url": "https://github.com/ramsey/uuid.git",
"reference": "7779489a47d443f845271badbdcedfe4df8e06fb"
"reference": "7e1633a6964b48589b142d60542f9ed31bd37a92"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/ramsey/uuid/zipball/7779489a47d443f845271badbdcedfe4df8e06fb",
"reference": "7779489a47d443f845271badbdcedfe4df8e06fb",
"url": "https://api.github.com/repos/ramsey/uuid/zipball/7e1633a6964b48589b142d60542f9ed31bd37a92",
"reference": "7e1633a6964b48589b142d60542f9ed31bd37a92",
"shasum": ""
},
"require": {
@ -4315,7 +4315,7 @@
"identifier",
"uuid"
],
"time": "2019-12-17T08:18:51+00:00"
"time": "2020-02-21T04:36:14+00:00"
},
{
"name": "react/event-loop",

View File

@ -283,7 +283,7 @@ class InstallerController extends Controller
* @throws \Prettus\Validator\Exceptions\ValidatorException
* @throws \Exception
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
* @return mixed
*/
public function usersetup(Request $request)
{
@ -326,8 +326,7 @@ class InstallerController extends Controller
'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

View File

@ -91,7 +91,7 @@
</form>
</div>
@endif
<table class="table table-striped">
<table class="table table-striped table-condensed">
<tr>
<td width="30%">@lang('common.state')</td>
@ -153,7 +153,7 @@
<table class="table table-hover table-condensed">
<thead>
<th>@lang('common.name')</th>
<th>@lang('common.value')</th>
<th>{{ trans_choice('common.value', 1) }}</th>
</thead>
<tbody>
@foreach($pirep->fields as $field)

View File

@ -1,7 +1,10 @@
<?php
use App\Events\UserRegistered;
use App\Models\Enums\UserState;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Notification;
class RegistrationTest extends TestCase
{
@ -15,25 +18,26 @@ class RegistrationTest extends TestCase
public function testRegistration()
{
Event::fake();
Mail::fake();
Notification::fake();
$userSvc = app('App\Services\UserService');
setting('pilots.auto_accept', true);
$user = factory(App\Models\User::class)->create();
$user = $userSvc->createUser($user);
$attrs = factory(App\Models\User::class)->make()->toArray();
$attrs['password'] = Hash::make('secret');
$user = $userSvc->createUser($attrs);
$this->assertEquals(UserState::ACTIVE, $user->state);
Event::assertDispatched(\App\Events\UserRegistered::class, function ($e) use ($user) {
Event::assertDispatched(UserRegistered::class, function ($e) use ($user) {
return $e->user->id === $user->id
&& $e->user->state === $user->state;
});
/*Mail::assertSent(\App\Mail\UserRegistered::class, function ($mail) use ($user) {
return $mail->user->id === $user->id
&& $mail->user->state === $user->state;
});*/
/*Notification::assertSentTo(
[$user],
\App\Notifications\Messages\UserRegistered::class
);*/
}
}

View File

@ -4,6 +4,7 @@ use App\Exceptions\UserPilotIdExists;
use App\Models\User;
use App\Repositories\SettingRepository;
use App\Services\UserService;
use Illuminate\Support\Facades\Hash;
class UserTest extends TestCase
{
@ -208,12 +209,14 @@ class UserTest extends TestCase
*/
public function testUserPilotIdAdded()
{
$new_user = factory(App\Models\User::class)->create(['id' => 1]);
$new_user = factory(App\Models\User::class)->make()->toArray();
$new_user['password'] = Hash::make('secret');
$user = $this->userSvc->createUser($new_user);
$this->assertEquals($user->id, $user->pilot_id);
// Add a second user
$new_user = factory(App\Models\User::class)->create(['id' => 2]);
$new_user = factory(App\Models\User::class)->make()->toArray();
$new_user['password'] = Hash::make('secret');
$user2 = $this->userSvc->createUser($new_user);
$this->assertEquals($user2->id, $user2->pilot_id);
@ -222,7 +225,7 @@ class UserTest extends TestCase
$this->assertEquals(3, $user->pilot_id);
// Create a new user and the pilot_id should be 4
$user3 = factory(App\Models\User::class)->create(['id' => 3]);
$user3 = factory(App\Models\User::class)->create();
$this->assertEquals(4, $user3->pilot_id);
}
}