Add service for mobile API common functions. Modify VehicleController. #591
This commit is contained in:
parent
6d42f1dcc5
commit
89a0acf380
5 changed files with 134 additions and 137 deletions
|
|
@ -69,7 +69,7 @@ access_keys:
|
|||
label: List
|
||||
|
||||
- id: mobile_user
|
||||
label: Mobile User
|
||||
label: Mobile User Access
|
||||
acls:
|
||||
- id: mobile_user.register
|
||||
label: Register Mobile User
|
||||
|
|
@ -91,3 +91,13 @@ access_keys:
|
|||
label: Update Device ID
|
||||
- id: mobile_user.privacy.settings
|
||||
label: Privacy Settings
|
||||
- id: mobile_vmanufacturer
|
||||
label: Mobile Vehicle Manufacturer Access
|
||||
acls:
|
||||
- id: mobile_vmanufacturer.list
|
||||
label: List Vehicle Manufacturers
|
||||
- id: mobile_vehicle
|
||||
label: Mobile Vehicle Make Access
|
||||
acls:
|
||||
- id: mobile_vehicle.list
|
||||
label: List Vehicle Makes
|
||||
|
|
|
|||
|
|
@ -299,3 +299,8 @@ services:
|
|||
App\Service\HubFilteringGeoChecker:
|
||||
arguments:
|
||||
$geofence_flag: "%env(HUB_GEOFENCE_ENABLE)%"
|
||||
|
||||
# mobile api handler
|
||||
App\Service\MobileAPIHandler:
|
||||
arguments:
|
||||
$em: "@doctrine.orm.entity_manager"
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@ use App\Entity\Customer;
|
|||
use App\Entity\PrivacyPolicy;
|
||||
|
||||
use App\Service\RisingTideGateway;
|
||||
use App\Service\MobileAPIHandler;
|
||||
|
||||
use App\Ramcar\CustomerSource;
|
||||
|
||||
|
|
@ -33,7 +34,7 @@ class CustomerController extends APIController
|
|||
$this->acl_gen = $acl_gen;
|
||||
}
|
||||
|
||||
public function register(Request $req, EntityManagerInterface $em)
|
||||
public function register(Request $req, EntityManagerInterface $em, MobileAPIHandler $mah)
|
||||
{
|
||||
$this->denyAccessUnlessGranted('mobile_user.register', null, 'No access.');
|
||||
|
||||
|
|
@ -50,8 +51,11 @@ class CustomerController extends APIController
|
|||
if ($msg)
|
||||
return new APIResponse(false, $msg);
|
||||
|
||||
// get capi user to link to mobile user
|
||||
$user_id = $this->getUser()->getID();
|
||||
|
||||
// check if capi user already has a mobile user
|
||||
$mobile_user = $this->findMobileUser($em);
|
||||
$mobile_user = $mah->findMobileUser($em,$user_id);
|
||||
if ($mobile_user != null)
|
||||
return new APIResponse(false, 'User already registered');
|
||||
|
||||
|
|
@ -103,7 +107,8 @@ class CustomerController extends APIController
|
|||
return new APIResponse(true, 'Mobile user created.', $data);
|
||||
}
|
||||
|
||||
public function confirmNumber(RisingTideGateway $rt, Request $req, EntityManagerInterface $em)
|
||||
public function confirmNumber(RisingTideGateway $rt, Request $req, EntityManagerInterface $em,
|
||||
MobileAPIHandler $mah)
|
||||
{
|
||||
$this->denyAccessUnlessGranted('mobile_user.confirm.number', null, 'No access.');
|
||||
|
||||
|
|
@ -117,8 +122,11 @@ class CustomerController extends APIController
|
|||
if ($msg)
|
||||
return new APIResponse(false, $msg);
|
||||
|
||||
// get capi user to link to mobile user
|
||||
$user_id = $this->getUser()->getID();
|
||||
|
||||
// get mobile user
|
||||
$mobile_user = $this->findMobileUser($em);
|
||||
$mobile_user = $mah->findMobileUser($em, $user_id);
|
||||
|
||||
if ($mobile_user == null)
|
||||
return new APIResponse(false, 'No mobile user found.');
|
||||
|
|
@ -174,7 +182,7 @@ class CustomerController extends APIController
|
|||
return new APIResponse(true, 'Number confirmed.');
|
||||
}
|
||||
|
||||
public function validateCode(Request $req, EntityManagerInterface $em)
|
||||
public function validateCode(Request $req, EntityManagerInterface $em, MobileAPIHandler $mah)
|
||||
{
|
||||
$this->denyAccessUnlessGranted('mobile_user.validate.code', null, 'No access.');
|
||||
|
||||
|
|
@ -188,8 +196,11 @@ class CustomerController extends APIController
|
|||
if ($msg)
|
||||
return new APIResponse(false, $msg);
|
||||
|
||||
// get capi user to link to mobile user
|
||||
$user_id = $this->getUser()->getID();
|
||||
|
||||
// get mobile user
|
||||
$mobile_user = $this->findMobileUser($em);
|
||||
$mobile_user = $mah->findMobileUser($em, $user_id);
|
||||
|
||||
if ($mobile_user == null)
|
||||
return new APIResponse(false, 'No mobile user found.');
|
||||
|
|
@ -227,12 +238,15 @@ class CustomerController extends APIController
|
|||
return new APIResponse(true, 'Code validated');
|
||||
}
|
||||
|
||||
public function getInfo(Request $req, EntityManagerInterface $em)
|
||||
public function getInfo(Request $req, EntityManagerInterface $em, MobileAPIHandler $mah)
|
||||
{
|
||||
$this->denyAccessUnlessGranted('mobile_user.get.info', null, 'No access.');
|
||||
|
||||
// get capi user to link to mobile user
|
||||
$user_id = $this->getUser()->getID();
|
||||
|
||||
// get mobile user
|
||||
$mobile_user = $this->findMobileUser($em);
|
||||
$mobile_user = $mah->findMobileUser($em, $user_id);
|
||||
|
||||
if ($mobile_user == null)
|
||||
return new APIResponse(false, 'No mobile user found.');
|
||||
|
|
@ -262,7 +276,7 @@ class CustomerController extends APIController
|
|||
return new APIResponse(true, 'Customer info found', $data);
|
||||
}
|
||||
|
||||
public function updateInfo(Request $req, EntityManagerInterface $em)
|
||||
public function updateInfo(Request $req, EntityManagerInterface $em, MobileAPIHandler $mah)
|
||||
{
|
||||
$this->denyAccessUnlessGranted('mobile_user.update.info', null, 'No access.');
|
||||
|
||||
|
|
@ -277,8 +291,11 @@ class CustomerController extends APIController
|
|||
if ($msg)
|
||||
return new APIResponse(false, $msg);
|
||||
|
||||
// get capi user to link to mobile user
|
||||
$user_id = $this->getUser()->getID();
|
||||
|
||||
// get mobile user
|
||||
$mobile_user = $this->findMobileUser($em);
|
||||
$mobile_user = $mah->findMobileUser($em, $user_id);
|
||||
|
||||
if ($mobile_user == null)
|
||||
return new APIResponse(false, 'No mobile user found.');
|
||||
|
|
@ -304,12 +321,15 @@ class CustomerController extends APIController
|
|||
return new APIResponse(true, 'Customer info updated');
|
||||
}
|
||||
|
||||
public function getStatus(Request $req, EntityManagerInterface $em)
|
||||
public function getStatus(Request $req, EntityManagerInterface $em, MobileAPIHandler $mah)
|
||||
{
|
||||
$this->denyAccessUnlessGranted('mobile_user.get.status', null, 'No access.');
|
||||
|
||||
// get capi user to link to mobile user
|
||||
$user_id = $this->getUser()->getID();
|
||||
|
||||
// get mobile user
|
||||
$mobile_user = $this->findMobileUser($em);
|
||||
$mobile_user = $mah->findMobileUser($em, $user_id);
|
||||
|
||||
if ($mobile_user == null)
|
||||
return new APIResponse(false, 'No mobile user found.');
|
||||
|
|
@ -324,12 +344,16 @@ class CustomerController extends APIController
|
|||
return new APIResponse(true, 'Customer status', $data);
|
||||
}
|
||||
|
||||
public function resendCode(Request $req, RisingTideGateway $rt, EntityManagerInterface $em)
|
||||
public function resendCode(Request $req, RisingTideGateway $rt, EntityManagerInterface $em,
|
||||
MobileAPIHandler $mah)
|
||||
{
|
||||
$this->denyAccessUnlessGranted('mobile_user.resend.code', null, 'No access.');
|
||||
|
||||
// get capi user to link to mobile user
|
||||
$user_id = $this->getUser()->getID();
|
||||
|
||||
// get mobile user
|
||||
$mobile_user = $this->findMobileUser($em);
|
||||
$mobile_user = $mah->findMobileUser($em, $user_id);
|
||||
|
||||
if ($mobile_user == null)
|
||||
return new APIResponse(false, 'No mobile user found.');
|
||||
|
|
@ -350,7 +374,7 @@ class CustomerController extends APIController
|
|||
return new APIResponse(true, 'Code re-sent');
|
||||
}
|
||||
|
||||
public function versionCheck(Request $req, EntityManagerInterface $em)
|
||||
public function versionCheck(Request $req, EntityManagerInterface $em, MobileAPIHandler $mah)
|
||||
{
|
||||
$this->denyAccessUnlessGranted('mobile_user.version.check', null, 'No access.');
|
||||
|
||||
|
|
@ -363,8 +387,11 @@ class CustomerController extends APIController
|
|||
if ($msg)
|
||||
return new APIResponse(false, $msg);
|
||||
|
||||
// get capi user to link to mobile user
|
||||
$user_id = $this->getUser()->getID();
|
||||
|
||||
// get mobile user
|
||||
$mobile_user = $this->findMobileUser($em);
|
||||
$mobile_user = $mah->findMobileUser($em, $user_id);
|
||||
|
||||
if ($mobile_user == null)
|
||||
return new APIResponse(false, 'No mobile user found.');
|
||||
|
|
@ -397,7 +424,7 @@ class CustomerController extends APIController
|
|||
return new APIResponse(true, 'Version checked', $data);
|
||||
}
|
||||
|
||||
public function updateDeviceID(Request $req, EntityManagerInterface $em)
|
||||
public function updateDeviceID(Request $req, EntityManagerInterface $em, MobileAPIHandler $mah)
|
||||
{
|
||||
$this->denyAccessUnlessGranted('mobile_user.update.deviceid', null, 'No access.');
|
||||
|
||||
|
|
@ -410,8 +437,11 @@ class CustomerController extends APIController
|
|||
if ($msg)
|
||||
return new APIResponse(false, $msg);
|
||||
|
||||
// get capi user to link to mobile user
|
||||
$user_id = $this->getUser()->getID();
|
||||
|
||||
// get mobile user
|
||||
$mobile_user = $this->findMobileUser($em);
|
||||
$mobile_user = $mah->findMobileUser($em, $user_id);
|
||||
|
||||
if ($mobile_user == null)
|
||||
return new APIResponse(false, 'No mobile user found.');
|
||||
|
|
@ -425,7 +455,7 @@ class CustomerController extends APIController
|
|||
return new APIResponse(true, 'Device ID updated');
|
||||
}
|
||||
|
||||
public function privacySettings(Request $req, EntityManagerInterface $em)
|
||||
public function privacySettings(Request $req, EntityManagerInterface $em, MobileAPIHandler $mah)
|
||||
{
|
||||
$this->denyAccessUnlessGranted('mobile_user.privacy.settings', null, 'No access.');
|
||||
|
||||
|
|
@ -439,8 +469,11 @@ class CustomerController extends APIController
|
|||
if ($msg)
|
||||
return new APIResponse(false, $msg);
|
||||
|
||||
// get capi user to link to mobile user
|
||||
$user_id = $this->getUser()->getID();
|
||||
|
||||
// get mobile user
|
||||
$mobile_user = $this->findMobileUser($em);
|
||||
$mobile_user = $mah->findMobileUser($em, $user_id);
|
||||
|
||||
if ($mobile_user == null)
|
||||
return new APIResponse(false, 'No mobile user found.');
|
||||
|
|
@ -494,15 +527,6 @@ class CustomerController extends APIController
|
|||
return new APIResponse(true, 'Privacy policy settings set');
|
||||
}
|
||||
|
||||
protected function findMobileUser($em)
|
||||
{
|
||||
// get capi user to link to mobile user
|
||||
$user_id = $this->getUser()->getID();
|
||||
$mobile_user = $em->getRepository(MobileUser::class)->findOneBy(['capi_user_id' => $user_id]);
|
||||
|
||||
return $mobile_user;
|
||||
}
|
||||
|
||||
// TODO: find session customer by phone number
|
||||
protected function findNumberMobileUser($number, $em)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -9,14 +9,13 @@ use Doctrine\ORM\Query;
|
|||
use Doctrine\ORM\EntityManagerInterface;
|
||||
|
||||
use Catalyst\APIBundle\Controller\APIController;
|
||||
// TODO: what do we use for response? APIResponse or APIResult?
|
||||
// APIResult is what is used by APIController. APIResponse is what is used by CAPI
|
||||
use Catalyst\APIBundle\Response\APIResponse;
|
||||
use App\Ramcar\APIResult;
|
||||
|
||||
use App\Entity\VehicleManufacturer;
|
||||
use App\Entity\Vehicle;
|
||||
|
||||
use App\Service\MobileAPIHandler;
|
||||
|
||||
use Catalyst\APIBundle\Access\Generator as ACLGenerator;
|
||||
|
||||
class VehicleController extends APIController
|
||||
|
|
@ -28,13 +27,25 @@ class VehicleController extends APIController
|
|||
$this->acl_gen = $acl_gen;
|
||||
}
|
||||
|
||||
public function listVehicleManufacturers(Request $req, EntityManagerInterface $em)
|
||||
public function listVehicleManufacturers(Request $req, EntityManagerInterface $em,
|
||||
MobileAPIHandler $mah)
|
||||
{
|
||||
// check required parameters and api key
|
||||
$this->denyAccessUnlessGranted('mobile_vmanufacturer.list', null, 'No access.');
|
||||
|
||||
// check required parameters
|
||||
$required_params = [];
|
||||
$res = $this->checkParamsAndKey($req, $em, $required_params);
|
||||
if ($res->isError())
|
||||
return $res->getReturnResponse();
|
||||
$msg = $this->checkRequiredParameters($req, $required_params);
|
||||
if ($msg)
|
||||
return new APIResponse(false, $msg);
|
||||
|
||||
// get capi user to link to mobile user
|
||||
$user_id = $this->getUser()->getID();
|
||||
|
||||
// get mobile user
|
||||
$mobile_user = $mah->findMobileUser($em, $user_id);
|
||||
|
||||
if ($mobile_user == null)
|
||||
return new APIResponse(false, 'No mobile user found.');
|
||||
|
||||
// get manufacturer list
|
||||
$mfgs = $em->getRepository(VehicleManufacturer::class)->findBy(['flag_mobile' => true], ['name' => 'asc']);
|
||||
|
|
@ -50,27 +61,34 @@ class VehicleController extends APIController
|
|||
$data = [
|
||||
'manufacturers' => $mfg_list
|
||||
];
|
||||
$res->setData($data);
|
||||
|
||||
return $res->getReturnResponse();
|
||||
return new APIResponse(true, 'Vehicle manufacturers listed.', $data);
|
||||
}
|
||||
|
||||
public function listVehicleMakes(Request $req, $mfg_id, EntityManagerInterface $em)
|
||||
public function listVehicleMakes(Request $req, $mfg_id, EntityManagerInterface $em,
|
||||
MobileAPIHandler $mah)
|
||||
{
|
||||
// check required parameters and api key
|
||||
$this->denyAccessUnlessGranted('mobile_vehicle.list', null, 'No access.');
|
||||
|
||||
// check required parameters
|
||||
$required_params = [];
|
||||
$res = $this->checkParamsAndKey($req, $em, $required_params);
|
||||
if ($res->isError())
|
||||
return $res->getReturnResponse();
|
||||
$msg = $this->checkRequiredParameters($req, $required_params);
|
||||
if ($msg)
|
||||
return new APIResponse(false, $msg);
|
||||
|
||||
// get capi user to link to mobile user
|
||||
$user_id = $this->getUser()->getID();
|
||||
|
||||
// get mobile user
|
||||
$mobile_user = $mah->findMobileUser($em, $user_id);
|
||||
|
||||
if ($mobile_user == null)
|
||||
return new APIResponse(false, 'No mobile user found.');
|
||||
|
||||
// get manufacturer
|
||||
$mfg = $em->getRepository(VehicleManufacturer::class)->find($mfg_id);
|
||||
if ($mfg == null)
|
||||
{
|
||||
$res->setError(true)
|
||||
->setErrorMessage('Invalid vehicle manufacturer id');
|
||||
return $res->getReturnResponse();
|
||||
}
|
||||
return new APIResponse(false, 'Invalid vehicle manufacturer id');
|
||||
|
||||
// get makes
|
||||
$vehicles = $em->getRepository(Vehicle::class)->findBy(
|
||||
|
|
@ -87,7 +105,6 @@ class VehicleController extends APIController
|
|||
$vlist[] = [
|
||||
'id' => $v->getID(),
|
||||
'make' => trim($v->getMake() . ' ' . $v->getModelYearFormatted(false)),
|
||||
// 'make' => $v->getMake() . ' ' . $v->getModelYearFrom() . '-' . $v->getModelYearTo(),
|
||||
];
|
||||
}
|
||||
|
||||
|
|
@ -99,92 +116,7 @@ class VehicleController extends APIController
|
|||
'makes' => $vlist,
|
||||
];
|
||||
|
||||
$res->setData($data);
|
||||
|
||||
return $res->getReturnResponse();
|
||||
return new APIResponse(true, 'Vehicle makes listed.', $data);
|
||||
}
|
||||
|
||||
// TODO: since we broke the functions into separate files, we need
|
||||
// to figure out how to make this accessible to all ResqAPI controllers
|
||||
protected function checkParamsAndKey(Request $req, $em, $params)
|
||||
{
|
||||
// TODO: depends on what we decide to return
|
||||
// returns APIResult object
|
||||
$res = new APIResult();
|
||||
|
||||
// check for api_key in query string
|
||||
$api_key = $req->query->get('api_key');
|
||||
if (empty($api_key))
|
||||
{
|
||||
$res->setError(true)
|
||||
->setErrorMessage('Missing API key');
|
||||
return $res;
|
||||
}
|
||||
|
||||
// check missing parameters
|
||||
$missing = $this->checkMissingParameters($req, $params);
|
||||
if (count($missing) > 0)
|
||||
{
|
||||
$miss_string = implode(', ', $missing);
|
||||
$res->setError(true)
|
||||
->setErrorMessage('Missing parameter(s): ' . $miss_string);
|
||||
return $res;
|
||||
}
|
||||
|
||||
// check api key
|
||||
$mobile_user = $this->checkAPIKey($em, $req->query->get('api_key'));
|
||||
if ($mobile_user == null)
|
||||
{
|
||||
$res->setError(true)
|
||||
->setErrorMessage('Invalid API Key');
|
||||
return $res;
|
||||
}
|
||||
|
||||
// store session
|
||||
$this->session = $sess;
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
// TODO: this might not be needed if we use APIController's checkRequiredParameters
|
||||
// or we put this into a service?
|
||||
protected function checkMissingParameters(Request $req, $params = [])
|
||||
{
|
||||
$missing = [];
|
||||
|
||||
// check if parameters are there
|
||||
foreach ($params as $param)
|
||||
{
|
||||
if ($req->getMethod() == 'GET')
|
||||
{
|
||||
$check = $req->query->get($param);
|
||||
if (empty($check))
|
||||
$missing[] = $param;
|
||||
}
|
||||
else if ($req->getMethod() == 'POST')
|
||||
{
|
||||
$check = $req->request->get($param);
|
||||
if (empty($check))
|
||||
$missing[] = $param;
|
||||
}
|
||||
else
|
||||
return $params;
|
||||
}
|
||||
|
||||
return $missing;
|
||||
}
|
||||
|
||||
// TODO: type hint entity manager
|
||||
// TODO: since we broke the functions into separate files, we need
|
||||
// to figure out how to make this accessible to all ResqAPI controllers
|
||||
protected function checkAPIKey($em, $api_key)
|
||||
{
|
||||
// find the api key (session id)
|
||||
// TODO: user validation needs to be changed
|
||||
$m_user = $em->getRepository(MobileUser::class)->find($api_key);
|
||||
if ($m_user == null)
|
||||
return null;
|
||||
|
||||
return $m_user;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
26
src/Service/MobileAPIHandler.php
Normal file
26
src/Service/MobileAPIHandler.php
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
|
||||
use App\Entity\MobileUser;
|
||||
|
||||
class MobileAPIHandler
|
||||
{
|
||||
protected $em;
|
||||
|
||||
public function __construct(EntityManagerInterface $em)
|
||||
{
|
||||
$this->em = $em;
|
||||
}
|
||||
|
||||
public function findMobileUser($em, $user_id)
|
||||
{
|
||||
// get capi user to link to mobile user
|
||||
$mobile_user = $em->getRepository(MobileUser::class)->findOneBy(['capi_user_id' => $user_id]);
|
||||
|
||||
return $mobile_user;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in a new issue