add setting to disable user aircraft rank restriction #138
This commit is contained in:
@@ -171,6 +171,18 @@ class CreateSettingsTable extends Migration
|
|||||||
'description' => 'Hide any cancelled PIREPs in the front-end',
|
'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
|
* PILOTS
|
||||||
*/
|
*/
|
||||||
|
|||||||
12
app/Exceptions/SettingNotFound.php
Normal file
12
app/Exceptions/SettingNotFound.php
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace App\Exceptions;
|
||||||
|
|
||||||
|
|
||||||
|
class SettingNotFound extends \Exception
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace App\Http\Controllers\Admin;
|
namespace App\Http\Controllers\Admin;
|
||||||
|
|
||||||
|
use App\Services\UserService;
|
||||||
use Log;
|
use Log;
|
||||||
use Flash;
|
use Flash;
|
||||||
use Response;
|
use Response;
|
||||||
@@ -33,7 +34,8 @@ class PirepController extends BaseController
|
|||||||
$aircraftRepo,
|
$aircraftRepo,
|
||||||
$pirepSvc,
|
$pirepSvc,
|
||||||
$pirepRepo,
|
$pirepRepo,
|
||||||
$subfleetRepo;
|
$subfleetRepo,
|
||||||
|
$userSvc;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
AirportRepository $airportRepo,
|
AirportRepository $airportRepo,
|
||||||
@@ -41,7 +43,8 @@ class PirepController extends BaseController
|
|||||||
AircraftRepository $aircraftRepo,
|
AircraftRepository $aircraftRepo,
|
||||||
PirepRepository $pirepRepo,
|
PirepRepository $pirepRepo,
|
||||||
PIREPService $pirepSvc,
|
PIREPService $pirepSvc,
|
||||||
SubfleetRepository $subfleetRepo
|
SubfleetRepository $subfleetRepo,
|
||||||
|
UserService $userSvc
|
||||||
) {
|
) {
|
||||||
$this->airportRepo = $airportRepo;
|
$this->airportRepo = $airportRepo;
|
||||||
$this->airlineRepo = $airlineRepo;
|
$this->airlineRepo = $airlineRepo;
|
||||||
@@ -49,10 +52,13 @@ class PirepController extends BaseController
|
|||||||
$this->pirepRepo = $pirepRepo;
|
$this->pirepRepo = $pirepRepo;
|
||||||
$this->pirepSvc = $pirepSvc;
|
$this->pirepSvc = $pirepSvc;
|
||||||
$this->subfleetRepo = $subfleetRepo;
|
$this->subfleetRepo = $subfleetRepo;
|
||||||
|
$this->userSvc = $userSvc;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dropdown with aircraft grouped by subfleet
|
* Dropdown with aircraft grouped by subfleet
|
||||||
|
* @param null $user
|
||||||
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function aircraftList($user=null)
|
public function aircraftList($user=null)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace App\Http\Controllers\Api;
|
namespace App\Http\Controllers\Api;
|
||||||
|
|
||||||
|
use App\Repositories\SubfleetRepository;
|
||||||
use App\Services\UserService;
|
use App\Services\UserService;
|
||||||
use Auth;
|
use Auth;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
@@ -17,12 +18,16 @@ use App\Http\Resources\User as UserResource;
|
|||||||
|
|
||||||
class UserController extends RestController
|
class UserController extends RestController
|
||||||
{
|
{
|
||||||
protected $userRepo, $userSvc;
|
protected $subfleetRepo,
|
||||||
|
$userRepo,
|
||||||
|
$userSvc;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
|
SubfleetRepository $subfleetRepo,
|
||||||
UserRepository $userRepo,
|
UserRepository $userRepo,
|
||||||
UserService $userSvc
|
UserService $userSvc
|
||||||
) {
|
) {
|
||||||
|
$this->subfleetRepo = $subfleetRepo;
|
||||||
$this->userRepo = $userRepo;
|
$this->userRepo = $userRepo;
|
||||||
$this->userSvc = $userSvc;
|
$this->userSvc = $userSvc;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -58,16 +58,13 @@ class PirepController extends Controller
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Dropdown with aircraft grouped by subfleet
|
* Dropdown with aircraft grouped by subfleet
|
||||||
|
* @param null $user
|
||||||
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function aircraftList($user=null)
|
public function aircraftList($user=null)
|
||||||
{
|
{
|
||||||
$aircraft = [];
|
$aircraft = [];
|
||||||
|
$subfleets = $this->userSvc->getAllowableSubfleets($user);
|
||||||
if ($user === null) {
|
|
||||||
$subfleets = $this->subfleetRepo->all();
|
|
||||||
} else {
|
|
||||||
$subfleets = $this->userSvc->getAllowableSubfleets($user);
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach ($subfleets as $subfleet) {
|
foreach ($subfleets as $subfleet) {
|
||||||
$tmp = [];
|
$tmp = [];
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ use Prettus\Repository\Contracts\CacheableInterface;
|
|||||||
|
|
||||||
use App\Models\Setting;
|
use App\Models\Setting;
|
||||||
use App\Repositories\Traits\CacheableRepository;
|
use App\Repositories\Traits\CacheableRepository;
|
||||||
|
use App\Exceptions\SettingNotFound;
|
||||||
|
|
||||||
use Prettus\Validator\Exceptions\ValidatorException;
|
use Prettus\Validator\Exceptions\ValidatorException;
|
||||||
|
|
||||||
class SettingRepository extends BaseRepository implements CacheableInterface
|
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
|
* Get a setting, reading it from the cache possibly
|
||||||
* @param string $key
|
* @param string $key
|
||||||
* @return mixed
|
* @return mixed
|
||||||
|
* @throws SettingNotFound
|
||||||
*/
|
*/
|
||||||
public function retrieve($key)
|
public function retrieve($key)
|
||||||
{
|
{
|
||||||
@@ -32,7 +35,7 @@ class SettingRepository extends BaseRepository implements CacheableInterface
|
|||||||
$setting = $this->findWhere(['id' => $key], ['type', 'value'])->first();
|
$setting = $this->findWhere(['id' => $key], ['type', 'value'])->first();
|
||||||
|
|
||||||
if(!$setting) {
|
if(!$setting) {
|
||||||
return null;
|
throw new SettingNotFound($key . ' not found');
|
||||||
}
|
}
|
||||||
|
|
||||||
# cast some types
|
# cast some types
|
||||||
@@ -67,12 +70,20 @@ class SettingRepository extends BaseRepository implements CacheableInterface
|
|||||||
public function store($key, $value)
|
public function store($key, $value)
|
||||||
{
|
{
|
||||||
$key = Setting::formatKey($key);
|
$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) {
|
if (!$setting) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
if(\is_bool($value)) {
|
||||||
|
$value = $value === true ? 1 : 0;
|
||||||
|
}
|
||||||
|
|
||||||
$this->update(['value' => $value], $setting->id);
|
$this->update(['value' => $value], $setting->id);
|
||||||
} catch (ValidatorException $e) {
|
} catch (ValidatorException $e) {
|
||||||
Log::error($e->getMessage(), $e->getTrace());
|
Log::error($e->getMessage(), $e->getTrace());
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace App\Services;
|
namespace App\Services;
|
||||||
|
|
||||||
|
use App\Repositories\SubfleetRepository;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
use Log;
|
use Log;
|
||||||
use App\Facades\Utils;
|
use App\Facades\Utils;
|
||||||
@@ -15,6 +16,18 @@ use App\Models\Enums\UserState;
|
|||||||
|
|
||||||
class UserService extends BaseService
|
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
|
* Register a pilot. Also attaches the initial roles
|
||||||
* required, and then triggers the UserRegistered event
|
* required, and then triggers the UserRegistered event
|
||||||
@@ -62,6 +75,10 @@ class UserService extends BaseService
|
|||||||
*/
|
*/
|
||||||
public function getAllowableSubfleets($user)
|
public function getAllowableSubfleets($user)
|
||||||
{
|
{
|
||||||
|
if($user === null || setting('pireps.restrict_aircraft_to_rank') === false) {
|
||||||
|
return $this->subfleetRepo->with('aircraft')->all();
|
||||||
|
}
|
||||||
|
|
||||||
$subfleets = $user->rank->subfleets();
|
$subfleets = $user->rank->subfleets();
|
||||||
return $subfleets->with('aircraft')->get();
|
return $subfleets->with('aircraft')->get();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,12 +19,14 @@ if (!function_exists('skin_view')) {
|
|||||||
if (!function_exists('setting')) {
|
if (!function_exists('setting')) {
|
||||||
function setting($key, $default = null)
|
function setting($key, $default = null)
|
||||||
{
|
{
|
||||||
$settingRepo = app('setting'); // defined in AppServiceProvider
|
$settingRepo = app('setting');
|
||||||
/*if($value !== null) {
|
try {
|
||||||
return $settingRepo->store($key, $value);
|
$value = $settingRepo->retrieve($key);
|
||||||
}*/
|
} catch (\App\Exceptions\SettingNotFound $e) {
|
||||||
|
return $default;
|
||||||
|
}
|
||||||
|
|
||||||
return $settingRepo->retrieve($key) ?: $default;
|
return $value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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
124
tests/UserTest.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user