src/Controller/AccountController.php line 52

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Tartarus;
  4. use App\Annotation\Auth;
  5. use App\Component\HttpFoundation\Payload;
  6. use App\Component\Invoker\FrontInvoker;
  7. use App\Component\Nexmo;
  8. use App\Component\NexmoVerifyError;
  9. use App\Component\UserConfirmation;
  10. use App\Entity;
  11. use Exception;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. class AccountController extends Controller
  15. {
  16.     /**
  17.      * List all accounts for user
  18.      *
  19.      * @Route("/account", name="account_list", methods="GET")
  20.      * @Auth(required=true)
  21.      */
  22.     public function listAction(FrontInvoker $fiTartarus\AuthInterface $auth)
  23.     {
  24.         // @todo fix serialization of roles array
  25.         //$accounts = $this->getRepository(Entity\Account::class)->listAccounts($auth->getUserId());
  26.         $rawAccounts $fi->accountList($auth->getUserId());
  27.         $accounts = [];
  28.         foreach ($rawAccounts as $account) {
  29.             $role $account['role'];
  30.             unset($account['role']);
  31.             $assigned $account['assigned'];
  32.             unset($account['assigned']);
  33.             if (!isset($accounts[$account['id']])) {
  34.                 $accounts[$account['id']] = array_merge($account, ['role' => []]);
  35.             }
  36.             $accounts[$account['id']]['role'][$role] = ['created' => $assigned];
  37.         }
  38.         return $this->jsonOkResponse(['account' => array_values($accounts)]);
  39.         //return $this->jsonOkResponse(['account' => $accounts], ['groups' => ['list'], ProxyAwareObjectNormalizer::RESOLVE_PROXY_CLASSES => [Entity\Role::class, Entity\Account::class]]);
  40.     }
  41.     /**
  42.      * Get zone
  43.      *
  44.      * @Route("/account/{id}", name="account_get", methods="GET")
  45.      * @Auth(required=true)
  46.      */
  47.     public function accountGetAction(FrontInvoker $fiPayload $payloadTartarus\AuthInterface $auth)
  48.     {
  49.         $accountId $payload->path->id;
  50.         $fi->privilegeCheck($auth->getUserId(), $accountId'owner');
  51.         $account $this->getRepository(Entity\Account::class)->findById($accountId);
  52.         if (!($account instanceof Entity\Account)) {
  53.             return $this->jsonSingleErrorResponse('Account not found', [], Response::HTTP_NOT_FOUND);
  54.         }
  55.         $account->fetchTrafficData();
  56.         return $this->jsonOkResponse(['account' => $account], Entity\Account::SERIALIZATION_CONTEXT['details']);
  57.     }
  58.     
  59.     /**
  60.      * Get zone
  61.      *
  62.      * @Route("/account/test/{id}", name="account_get_test", methods="GET")
  63.      */
  64.     public function accountGetTestAction(int $id)
  65.     {
  66.         $account $this->getRepository(Entity\Account::class)->findById($id);
  67.         if (!($account instanceof Entity\Account)) {
  68.             return $this->jsonSingleErrorResponse('Account not found', [], Response::HTTP_NOT_FOUND);
  69.         }
  70.         $account->fetchTrafficData();
  71.         return $this->jsonOkResponse(['account' => $account], Entity\Account::SERIALIZATION_CONTEXT['details']);
  72.     }
  73.     /**
  74.      * Update account
  75.      *
  76.      * @Route("/account/{id}", name="account_update", methods="PUT")
  77.      * @Auth(required=true)
  78.      */
  79.     public function accountUpdateAction(FrontInvoker $fiPayload $payloadTartarus\Auth $authEntity\Account $account)
  80.     {
  81.         $fi->privilegeCheck($auth->getUserId(), $payload->path->id'owner');
  82.         $account->assign($payload->body);
  83.         $this->db()->persist($account);
  84.         $this->db()->flush();
  85.         $account $this->getRepository(Entity\Account::class)->findById($account->getId());
  86.         if (!($account instanceof Entity\Account)) {
  87.             return $this->jsonSingleErrorResponse('Account not found', [], Response::HTTP_NOT_FOUND);
  88.         }
  89.         $account->fetchTrafficData();
  90.         return $this->jsonOkResponse(['account' => $account], Entity\Account::SERIALIZATION_CONTEXT['details']);
  91.     }
  92.     /**
  93.      * Start postponed phone verification
  94.      *
  95.      * @Route("/account/{id}/verify", name="account_verify_start", methods="GET")
  96.      * @Auth(required=true)
  97.      */
  98.     public function verifyStart(Payload $payloadFrontInvoker $fiTartarus\Auth $auth)
  99.     {
  100.         $user $auth->getUser();
  101.         $account current($fi->accountGetById($user['id'], $payload->path->id));
  102.         if ((int)$account['verified']) {
  103.             return $this->jsonSingleErrorResponse('Account already verified');
  104.         }
  105.         if ((int)$account['owner_id'] !== (int)$user['id']) {
  106.             return $this->jsonSingleErrorResponse('Only owner is allowed to verify account');
  107.         }
  108.         /* Initiate Nexmo validation */
  109.         $nexmo = new Nexmo();
  110.         try {
  111.             $requestId $nexmo->verify($user['phone_number'], $user['email'], $next);
  112.             return $this->jsonOkResponse([
  113.                     'call' => ($next 'next' 'first'),
  114.                     'request_id' => UserConfirmation::encryptRequestId($requestId$account['id'] . $account['name'] . $user['email']),
  115.                     'user' => ($auth->getUserPublic() + ['phone_number' => $user['phone_number']])
  116.                 ]
  117.             );
  118.         } catch (NexmoVerifyError $exception) {
  119.             return $this->jsonSingleErrorResponse($exception->getMessage());
  120.         } catch (Exception $exception) {
  121.             $this->storeException($exception);
  122.             return $this->jsonSingleErrorResponse('An error occurred during phone verification attempt');
  123.         }
  124.     }
  125.     /**
  126.      * Finalize postponed phone verification
  127.      *
  128.      * @Route("/account/{id}/verify", name="account_verify_complete", methods="POST")
  129.      * @Auth(required=true)
  130.      */
  131.     public function verifyComplete(Payload $payloadFrontInvoker $fiTartarus\Auth $auth)
  132.     {
  133.         $user $auth->getUser();
  134.         $accountId $payload->path->id;
  135.         $errors = [];
  136.         $data json_decode($payload->bodytrue);
  137.         if (!is_array($data)) {
  138.             return $this->jsonSingleErrorResponse('Malformed request body');
  139.         }
  140.         $account current($fi->accountGetById($user['id'], intval($accountId)));
  141.         $originalRequestId $data['request_id'];
  142.         try {
  143.             $data['request_id'] = UserConfirmation::decryptRequestId($data['request_id'], $account['id'] . $account['name'] . $user['email']);
  144.         } catch (Exception $e) {
  145.             $this->storeException($e);
  146.             $errors[] = ['fields' => ['request_id'], 'message' => $e->getMessage()];
  147.         }
  148.         /* Initiate Nexmo validation */
  149.         $nexmo = new Nexmo();
  150.         try {
  151.             $requestId $nexmo->check($data['request_id'], (int)$data['code']);
  152.             $presult current($fi->accountVerify(
  153.                 (int)$user['id'],
  154.                 (int)$accountId
  155.             ));
  156.             return $this->jsonOkResponse(['account' => $presult]);
  157.         } catch (NexmoVerifyError $e) {
  158.             return $this->jsonSingleErrorResponse($e->getMessage(), ['request_id' => $originalRequestId'user' => ['phone_number' => $data['phone_number']]]); // @todo before was $pdata['phone_number'], no such var, what should be here?
  159.         } catch (Exception $e) {
  160.             $this->storeException($e);
  161.             return $this->jsonSingleErrorResponse('An error occurred during user confirmation');
  162.         }
  163.     }
  164. }