From 7c70bb097192cc81cb608d93ad4eab5eb8556f53 Mon Sep 17 00:00:00 2001 From: Nabeel Shahzad Date: Fri, 19 Jan 2018 12:46:30 -0500 Subject: [PATCH] add setting to disable user aircraft rank restriction #138 --- ...017_06_07_014930_create_settings_table.php | 12 ++ app/Exceptions/SettingNotFound.php | 12 ++ .../Controllers/Admin/PirepController.php | 10 +- app/Http/Controllers/Api/UserController.php | 7 +- .../Controllers/Frontend/PirepController.php | 9 +- app/Repositories/SettingRepository.php | 15 ++- app/Services/UserService.php | 17 +++ app/helpers.php | 12 +- tests/PilotTest.php | 66 ---------- tests/UserTest.php | 124 ++++++++++++++++++ 10 files changed, 202 insertions(+), 82 deletions(-) create mode 100644 app/Exceptions/SettingNotFound.php delete mode 100644 tests/PilotTest.php create mode 100644 tests/UserTest.php diff --git a/app/Database/migrations/2017_06_07_014930_create_settings_table.php b/app/Database/migrations/2017_06_07_014930_create_settings_table.php index 110407ec..e7103b9f 100644 --- a/app/Database/migrations/2017_06_07_014930_create_settings_table.php +++ b/app/Database/migrations/2017_06_07_014930_create_settings_table.php @@ -171,6 +171,18 @@ class CreateSettingsTable extends Migration 'description' => 'Hide any cancelled PIREPs in the front-end', ], + [ + 'id' => $this->formatSettingId('pireps.restrict_aircraft_to_rank'), + 'order' => $this->getNextOrderNumber('pireps'), + 'name' => 'Restrict Aircraft to Ranks', + 'group' => 'pireps', + 'value' => true, + 'default' => true, + 'type' => 'boolean', + 'description' => 'Aircraft that can be flown are restricted to a user\'s rank', + ], + + /** * PILOTS */ diff --git a/app/Exceptions/SettingNotFound.php b/app/Exceptions/SettingNotFound.php new file mode 100644 index 00000000..516cee1e --- /dev/null +++ b/app/Exceptions/SettingNotFound.php @@ -0,0 +1,12 @@ +airportRepo = $airportRepo; $this->airlineRepo = $airlineRepo; @@ -49,10 +52,13 @@ class PirepController extends BaseController $this->pirepRepo = $pirepRepo; $this->pirepSvc = $pirepSvc; $this->subfleetRepo = $subfleetRepo; + $this->userSvc = $userSvc; } /** * Dropdown with aircraft grouped by subfleet + * @param null $user + * @return array */ public function aircraftList($user=null) { diff --git a/app/Http/Controllers/Api/UserController.php b/app/Http/Controllers/Api/UserController.php index 681b5d0e..56825d04 100644 --- a/app/Http/Controllers/Api/UserController.php +++ b/app/Http/Controllers/Api/UserController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers\Api; +use App\Repositories\SubfleetRepository; use App\Services\UserService; use Auth; use Illuminate\Http\Request; @@ -17,12 +18,16 @@ use App\Http\Resources\User as UserResource; class UserController extends RestController { - protected $userRepo, $userSvc; + protected $subfleetRepo, + $userRepo, + $userSvc; public function __construct( + SubfleetRepository $subfleetRepo, UserRepository $userRepo, UserService $userSvc ) { + $this->subfleetRepo = $subfleetRepo; $this->userRepo = $userRepo; $this->userSvc = $userSvc; } diff --git a/app/Http/Controllers/Frontend/PirepController.php b/app/Http/Controllers/Frontend/PirepController.php index 404139f1..10a775c8 100644 --- a/app/Http/Controllers/Frontend/PirepController.php +++ b/app/Http/Controllers/Frontend/PirepController.php @@ -58,16 +58,13 @@ class PirepController extends Controller /** * Dropdown with aircraft grouped by subfleet + * @param null $user + * @return array */ public function aircraftList($user=null) { $aircraft = []; - - if ($user === null) { - $subfleets = $this->subfleetRepo->all(); - } else { - $subfleets = $this->userSvc->getAllowableSubfleets($user); - } + $subfleets = $this->userSvc->getAllowableSubfleets($user); foreach ($subfleets as $subfleet) { $tmp = []; diff --git a/app/Repositories/SettingRepository.php b/app/Repositories/SettingRepository.php index 9820b83f..8493b99c 100644 --- a/app/Repositories/SettingRepository.php +++ b/app/Repositories/SettingRepository.php @@ -8,6 +8,8 @@ use Prettus\Repository\Contracts\CacheableInterface; use App\Models\Setting; use App\Repositories\Traits\CacheableRepository; +use App\Exceptions\SettingNotFound; + use Prettus\Validator\Exceptions\ValidatorException; class SettingRepository extends BaseRepository implements CacheableInterface @@ -25,6 +27,7 @@ class SettingRepository extends BaseRepository implements CacheableInterface * Get a setting, reading it from the cache possibly * @param string $key * @return mixed + * @throws SettingNotFound */ public function retrieve($key) { @@ -32,7 +35,7 @@ class SettingRepository extends BaseRepository implements CacheableInterface $setting = $this->findWhere(['id' => $key], ['type', 'value'])->first(); if(!$setting) { - return null; + throw new SettingNotFound($key . ' not found'); } # cast some types @@ -67,12 +70,20 @@ class SettingRepository extends BaseRepository implements CacheableInterface public function store($key, $value) { $key = Setting::formatKey($key); - $setting = $this->findWhere(['id' => $key], ['id'])->first(); + $setting = $this->findWhere( + ['id' => $key], + ['id', 'value'] # only get these columns + )->first(); + if (!$setting) { return null; } try { + if(\is_bool($value)) { + $value = $value === true ? 1 : 0; + } + $this->update(['value' => $value], $setting->id); } catch (ValidatorException $e) { Log::error($e->getMessage(), $e->getTrace()); diff --git a/app/Services/UserService.php b/app/Services/UserService.php index 712d48dd..104c2c7c 100644 --- a/app/Services/UserService.php +++ b/app/Services/UserService.php @@ -2,6 +2,7 @@ namespace App\Services; +use App\Repositories\SubfleetRepository; use Illuminate\Support\Collection; use Log; use App\Facades\Utils; @@ -15,6 +16,18 @@ use App\Models\Enums\UserState; class UserService extends BaseService { + protected $subfleetRepo; + + /** + * UserService constructor. + * @param SubfleetRepository $subfleetRepo + */ + public function __construct( + SubfleetRepository $subfleetRepo + ) { + $this->subfleetRepo = $subfleetRepo; + } + /** * Register a pilot. Also attaches the initial roles * required, and then triggers the UserRegistered event @@ -62,6 +75,10 @@ class UserService extends BaseService */ public function getAllowableSubfleets($user) { + if($user === null || setting('pireps.restrict_aircraft_to_rank') === false) { + return $this->subfleetRepo->with('aircraft')->all(); + } + $subfleets = $user->rank->subfleets(); return $subfleets->with('aircraft')->get(); } diff --git a/app/helpers.php b/app/helpers.php index 2c52a972..b91736ca 100644 --- a/app/helpers.php +++ b/app/helpers.php @@ -19,12 +19,14 @@ if (!function_exists('skin_view')) { if (!function_exists('setting')) { function setting($key, $default = null) { - $settingRepo = app('setting'); // defined in AppServiceProvider - /*if($value !== null) { - return $settingRepo->store($key, $value); - }*/ + $settingRepo = app('setting'); + try { + $value = $settingRepo->retrieve($key); + } catch (\App\Exceptions\SettingNotFound $e) { + return $default; + } - return $settingRepo->retrieve($key) ?: $default; + return $value; } } diff --git a/tests/PilotTest.php b/tests/PilotTest.php deleted file mode 100644 index 0b0b3fb2..00000000 --- a/tests/PilotTest.php +++ /dev/null @@ -1,66 +0,0 @@ -userSvc = app(UserService::class); - } - - public function testRankSubfleets() - { - # Add subfleets and aircraft, but also add another - # set of subfleets - $subfleetA = TestData::createSubfleetWithAircraft(); - TestData::createSubfleetWithAircraft(); - - $rank = TestData::createRank(10, [$subfleetA['subfleet']->id]); - - $user = factory(App\Models\User::class)->create([ - 'rank_id' => $rank->id, - ]); - - $added_aircraft = $subfleetA['aircraft']->pluck('id'); - - $subfleets = $this->userSvc->getAllowableSubfleets($user); - $this->assertEquals(1, $subfleets->count()); - - $subfleet = $subfleets[0]; - $all_aircraft = $subfleet->aircraft->pluck('id'); - $this->assertEquals($added_aircraft, $all_aircraft); - - /** - * Check via API - */ - $resp = $this->get('/api/user/fleet', [], $user)->assertStatus(200); - $body = $resp->json(); - - # Get the subfleet that's been added in - $subfleet_from_api = $body[0]; - $this->assertEquals($subfleet->id, $subfleet_from_api['id']); - - # Get all the aircraft from that subfleet - $aircraft_from_api = collect($subfleet_from_api['aircraft'])->pluck('id'); - $this->assertEquals($added_aircraft, $aircraft_from_api); - - /** - * Check the user ID call - */ - $resp = $this->get('/api/users/'.$user->id.'/fleet', [], $user)->assertStatus(200); - $body = $resp->json(); - - # Get the subfleet that's been added in - $subfleet_from_api = $body[0]; - $this->assertEquals($subfleet->id, $subfleet_from_api['id']); - - # Get all the aircraft from that subfleet - $aircraft_from_api = collect($subfleet_from_api['aircraft'])->pluck('id'); - $this->assertEquals($added_aircraft, $aircraft_from_api); - } -} diff --git a/tests/UserTest.php b/tests/UserTest.php new file mode 100644 index 00000000..908a0ea6 --- /dev/null +++ b/tests/UserTest.php @@ -0,0 +1,124 @@ +userSvc = app(UserService::class); + $this->settingsRepo = app(SettingRepository::class); + } + + /** + * Makes sure that the subfleet/aircraft returned are allowable + * by the users rank. + */ + public function testRankSubfleets() + { + # Add subfleets and aircraft, but also add another + # set of subfleets + $subfleetA = TestData::createSubfleetWithAircraft(); + TestData::createSubfleetWithAircraft(); + + $rank = TestData::createRank(10, [$subfleetA['subfleet']->id]); + + $user = factory(App\Models\User::class)->create([ + 'rank_id' => $rank->id, + ]); + + $added_aircraft = $subfleetA['aircraft']->pluck('id'); + + $subfleets = $this->userSvc->getAllowableSubfleets($user); + $this->assertEquals(1, $subfleets->count()); + + $subfleet = $subfleets[0]; + $all_aircraft = $subfleet->aircraft->pluck('id'); + $this->assertEquals($added_aircraft, $all_aircraft); + + /** + * Check via API + */ + $resp = $this->get('/api/user/fleet', [], $user)->assertStatus(200); + $body = $resp->json(); + + # Get the subfleet that's been added in + $subfleet_from_api = $body[0]; + $this->assertEquals($subfleet->id, $subfleet_from_api['id']); + + # Get all the aircraft from that subfleet + $aircraft_from_api = collect($subfleet_from_api['aircraft'])->pluck('id'); + $this->assertEquals($added_aircraft, $aircraft_from_api); + + /** + * Check the user ID call + */ + $resp = $this->get('/api/users/' . $user->id . '/fleet', [], $user)->assertStatus(200); + $body = $resp->json(); + + # Get the subfleet that's been added in + $subfleet_from_api = $body[0]; + $this->assertEquals($subfleet->id, $subfleet_from_api['id']); + + # Get all the aircraft from that subfleet + $aircraft_from_api = collect($subfleet_from_api['aircraft'])->pluck('id'); + $this->assertEquals($added_aircraft, $aircraft_from_api); + } + + + /** + * Flip the setting for getting all of the user's aircraft restricted + * by rank. Make sure that they're all returned + */ + public function testGetAllAircraft() + { + # Add subfleets and aircraft, but also add another + # set of subfleets + $subfleetA = TestData::createSubfleetWithAircraft(); + $subfleetB = TestData::createSubfleetWithAircraft(); + + $added_aircraft = array_merge( + $subfleetA['aircraft']->pluck('id')->toArray(), + $subfleetB['aircraft']->pluck('id')->toArray() + ); + + $rank = TestData::createRank(10, [$subfleetA['subfleet']->id]); + + $user = factory(App\Models\User::class)->create([ + 'rank_id' => $rank->id, + ]); + + $this->settingsRepo->store('pireps.restrict_aircraft_to_rank', false); + + $subfleets = $this->userSvc->getAllowableSubfleets($user); + $this->assertEquals(2, $subfleets->count()); + + $all_aircraft = array_merge( + $subfleets[0]->aircraft->pluck('id')->toArray(), + $subfleets[1]->aircraft->pluck('id')->toArray() + ); + + $this->assertEquals($added_aircraft, $all_aircraft); + + + /** + * Check via API + */ + $resp = $this->get('/api/user/fleet', [], $user)->assertStatus(200); + $body = $resp->json(); + + # Get all the aircraft from that subfleet + $aircraft_from_api = array_merge( + collect($body[0]['aircraft'])->pluck('id')->toArray(), + collect($body[1]['aircraft'])->pluck('id')->toArray() + ); + + $this->assertEquals($added_aircraft, $aircraft_from_api); + } +}