From a1d6fa17addda2e86a622c032ef3a949a8a20b29 Mon Sep 17 00:00:00 2001 From: Nabeel S Date: Sun, 23 Feb 2020 17:21:26 -0500 Subject: [PATCH] Respect home hubs setting for registration #580 (#581) Respect home hubs setting for registration #580 --- Makefile | 2 +- app/Database/factories/UserFactory.php | 2 +- .../Controllers/Auth/RegisterController.php | 40 ++++--------------- app/Http/Requests/CreateUserRequest.php | 17 ++++++-- app/Services/UserService.php | 11 +++-- composer.json | 2 +- composer.lock | 12 +++--- .../Http/Controllers/InstallerController.php | 5 +-- .../layouts/default/pireps/show.blade.php | 4 +- tests/RegistrationTest.php | 22 +++++----- tests/UserTest.php | 9 +++-- 11 files changed, 62 insertions(+), 64 deletions(-) diff --git a/Makefile b/Makefile index dce22ee5..bb4b53fe 100644 --- a/Makefile +++ b/Makefile @@ -74,7 +74,7 @@ reload-db: tests: test .PHONY: test -test: +test: phpcs #php artisan database:create --reset vendor/bin/phpunit --debug --verbose diff --git a/app/Database/factories/UserFactory.php b/app/Database/factories/UserFactory.php index 8d18fc15..85ab8cbf 100644 --- a/app/Database/factories/UserFactory.php +++ b/app/Database/factories/UserFactory.php @@ -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'), diff --git a/app/Http/Controllers/Auth/RegisterController.php b/app/Http/Controllers/Auth/RegisterController.php index a10f45eb..7775b21e 100755 --- a/app/Http/Controllers/Auth/RegisterController.php +++ b/app/Http/Controllers/Auth/RegisterController.php @@ -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'); diff --git a/app/Http/Requests/CreateUserRequest.php b/app/Http/Requests/CreateUserRequest.php index 65d01813..277db6c3 100644 --- a/app/Http/Requests/CreateUserRequest.php +++ b/app/Http/Requests/CreateUserRequest.php @@ -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; } diff --git a/app/Services/UserService.php b/app/Services/UserService.php index 9d46c1ac..9be4e8a4 100644 --- a/app/Services/UserService.php +++ b/app/Services/UserService.php @@ -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; diff --git a/composer.json b/composer.json index 192af2e9..f2e8bf53 100755 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/composer.lock b/composer.lock index bb235e88..2f20e358 100644 --- a/composer.lock +++ b/composer.lock @@ -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", diff --git a/modules/Installer/Http/Controllers/InstallerController.php b/modules/Installer/Http/Controllers/InstallerController.php index b07d4686..fb4e5720 100644 --- a/modules/Installer/Http/Controllers/InstallerController.php +++ b/modules/Installer/Http/Controllers/InstallerController.php @@ -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 diff --git a/resources/views/layouts/default/pireps/show.blade.php b/resources/views/layouts/default/pireps/show.blade.php index 16637d03..158a41d6 100644 --- a/resources/views/layouts/default/pireps/show.blade.php +++ b/resources/views/layouts/default/pireps/show.blade.php @@ -91,7 +91,7 @@ @endif - +
@@ -153,7 +153,7 @@
@lang('common.state')
- + @foreach($pirep->fields as $field) diff --git a/tests/RegistrationTest.php b/tests/RegistrationTest.php index 1f5f43c5..a57c1777 100644 --- a/tests/RegistrationTest.php +++ b/tests/RegistrationTest.php @@ -1,7 +1,10 @@ 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 + );*/ } } diff --git a/tests/UserTest.php b/tests/UserTest.php index c25c67ec..8546c95a 100644 --- a/tests/UserTest.php +++ b/tests/UserTest.php @@ -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); } }
@lang('common.name')@lang('common.value'){{ trans_choice('common.value', 1) }}