fix bug where aircraft restrictions aren't respected in flight calls #170

This commit is contained in:
Nabeel Shahzad
2018-02-09 14:26:14 -06:00
parent 71189e4f2d
commit f6b2102e48
6 changed files with 135 additions and 15 deletions

View File

@@ -2,20 +2,52 @@
namespace App\Http\Controllers\Api;
use Auth;
use Illuminate\Http\Request;
use Prettus\Repository\Criteria\RequestCriteria;
use App\Repositories\FlightRepository;
use App\Http\Resources\Flight as FlightResource;
use Prettus\Repository\Exceptions\RepositoryException;
use App\Services\UserService;
use App\Repositories\FlightRepository;
use App\Http\Resources\Flight as FlightResource;
/**
* Class FlightController
* @package App\Http\Controllers\Api
*/
class FlightController extends RestController
{
protected $flightRepo;
protected $flightRepo, $userSvc;
public function __construct(FlightRepository $flightRepo) {
public function __construct(
FlightRepository $flightRepo,
UserService $userSvc
) {
$this->flightRepo = $flightRepo;
$this->userSvc = $userSvc;
}
/**
* Filter out subfleets to only include aircraft that a user has access to
* @param $user
* @param $flight
* @return mixed
*/
public function filterSubfleets($user, $flight)
{
if(setting('pireps.restrict_aircraft_to_rank', false) === false) {
return $flight;
}
$allowed_subfleets = $this->userSvc->getAllowableSubfleets($user)->pluck('id');
$flight->subfleets = $flight->subfleets->filter(
function($subfleet, $item) use ($allowed_subfleets) {
if ($allowed_subfleets->contains($subfleet->id)) {
return true;
}
});
return $flight;
}
/**
@@ -27,12 +59,23 @@ class FlightController extends RestController
->orderBy('flight_number', 'asc')
->paginate(50);
$user = Auth::user();
foreach($flights as $flight) {
$this->filterSubfleets($user, $flight);
}
return FlightResource::collection($flights);
}
/**
* @param $id
* @return FlightResource
*/
public function get($id)
{
$flight = $this->flightRepo->find($id);
$this->filterSubfleets(Auth::user(), $flight);
FlightResource::withoutWrapping();
return new FlightResource($flight);
}
@@ -51,6 +94,11 @@ class FlightController extends RestController
return response($e, 503);
}
$user = Auth::user();
foreach ($flights as $flight) {
$this->filterSubfleets($user, $flight);
}
return FlightResource::collection($flights);
}
}