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