add setting to disable user aircraft rank restriction #138

This commit is contained in:
Nabeel Shahzad
2018-01-19 12:46:30 -05:00
parent 333f866232
commit 7c70bb0971
10 changed files with 202 additions and 82 deletions

View File

@@ -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
*/

View File

@@ -0,0 +1,12 @@
<?php
/**
*
*/
namespace App\Exceptions;
class SettingNotFound extends \Exception
{
}

View File

@@ -2,6 +2,7 @@
namespace App\Http\Controllers\Admin;
use App\Services\UserService;
use Log;
use Flash;
use Response;
@@ -33,7 +34,8 @@ class PirepController extends BaseController
$aircraftRepo,
$pirepSvc,
$pirepRepo,
$subfleetRepo;
$subfleetRepo,
$userSvc;
public function __construct(
AirportRepository $airportRepo,
@@ -41,7 +43,8 @@ class PirepController extends BaseController
AircraftRepository $aircraftRepo,
PirepRepository $pirepRepo,
PIREPService $pirepSvc,
SubfleetRepository $subfleetRepo
SubfleetRepository $subfleetRepo,
UserService $userSvc
) {
$this->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)
{

View File

@@ -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;
}

View File

@@ -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 = [];

View File

@@ -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());

View File

@@ -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();
}

View File

@@ -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;
}
}

View File

@@ -1,66 +0,0 @@
<?php
use App\Services\UserService;
use Tests\TestData;
class PilotTest extends TestCase
{
protected $userSvc;
public function setUp()
{
parent::setUp();
$this->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);
}
}

124
tests/UserTest.php Normal file
View File

@@ -0,0 +1,124 @@
<?php
use App\Services\UserService;
use App\Repositories\SettingRepository;
use Tests\TestData;
class UserTest extends TestCase
{
protected $settingsRepo, $userSvc;
public function setUp()
{
parent::setUp();
$this->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);
}
}