src/OceanExpertBundle/Controller/ApiController.php line 132

Open in your IDE?
  1. <?php
  2. namespace OceanExpertBundle\Controller;
  3. use FOS\UserBundle\Model\UserManagerInterface;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\HttpFoundation\JsonResponse;
  8. use \Firebase\JWT\JWT;
  9. /**
  10.  * Class ApiController
  11.  * @package OceanExpertBundle\Controller
  12.  */
  13. class ApiController extends AbstractController
  14. {
  15.     private UserManagerInterface $fosUserManager;
  16.     public function __construct(UserManagerInterface $fosUserManager)
  17.     {
  18.         $this->fosUserManager $fosUserManager;
  19.     }
  20.     /**
  21.      * @return Response
  22.      */
  23.     public function indexAction()
  24.     {
  25.         return new Response('Api index');
  26.     }
  27.     /**
  28.      * check if login/pwd combination are known and valid in OE
  29.      *
  30.      * @param Request $request
  31.      *
  32.      * @return JsonResponse
  33.      */
  34.     public function loginAction(Request $request): JsonResponse
  35.     {
  36.         if ($request->getMethod() === 'POST') {
  37.             $userName $request->request->get('username');
  38.             $password $request->request->get('password');
  39.             $user $this->fosUserManager->findUserByUsernameOrEmail($userName);
  40.             if (!$user) {
  41.                 //we don't say the user does not exist, make the life of possible hackers not too easy
  42.                 return new JsonResponse(
  43.                     array(
  44.                         'status' => 1,
  45.                         'error' => 'Wrong user or password'
  46.                     )
  47.                 );
  48.             }
  49.             $isValid $this->get('security.password_encoder')
  50.                 ->isPasswordValid(
  51.                     $user,
  52.                     $password
  53.                 );
  54.             if (!$isValid) {
  55.                 return new JsonResponse(
  56.                     array(
  57.                         'status' => 1,
  58.                         'error' => 'Wrong user or password'
  59.                     )
  60.                 );
  61.             }
  62.             $privateKey openssl_get_privatekey(
  63.                 'file://var/jwt/jwtRS256.key',
  64.                 'oceanexpert'
  65.             );
  66.             $data $this->getExpertDetailsById($user->getEmail());
  67.             if (count($user->getRoles()) > 0) {
  68.                 //@todo : this logic should move away from here because it should be applied to every stage
  69.                 //calculate the highest permission (#523)
  70.                 $possibleRoles = array(
  71.                     'ROLE_SUPERADMIN' => 1,
  72.                     'ROLE_ADMIN' => 2,
  73.                     'ROLE_MANAGER' => 3,
  74.                     'ROLE_GLOBAL_EDITOR' => 4,
  75.                     'ROLE_COUNTRY_EDITOR' => 5,
  76.                     'ROLE_USER' => 100
  77.                 );
  78.                 $allRoles $user->getRoles();
  79.                 //what is the id of the maximum role of the user
  80.                 $maxRole 100;
  81.                 //'bubble sort'
  82.                 foreach($allRoles as $role) {
  83.                     if (isset($possibleRoles[$role])) {
  84.                         if ($possibleRoles[$role] < $maxRole) {
  85.                             $maxRole $possibleRoles[$role];
  86.                         }
  87.                     }
  88.                 }
  89.                 $maxRole array_search($maxRole$possibleRoles);
  90.                 //$maxRole = $user->getRoles()[0];
  91.                 $data['oeRole'] = $maxRole;
  92.                 $data['oeRoles'] = $allRoles;
  93.             } else {
  94.                 $data['oeRole'] = null;
  95.                 $data['oeRoles'] = null;
  96.             }
  97.             $jwt JWT::encode(
  98.                 $data,
  99.                 $privateKey,
  100.                 'RS256'
  101.             );
  102.             return new JsonResponse(['token' => $jwt]);
  103.         } else {
  104.             return new JsonResponse(
  105.                 array(
  106.                     'status' => 3,
  107.                     'error' => 'this call is only availlable via POST, RTFM'
  108.                 )
  109.             );
  110.         }
  111.     }
  112.     /**
  113.      * @return Response
  114.      */
  115.     public function checkSessionAction()
  116.     {
  117.         $response 0;
  118.         if (TRUE === $this->get('security.authorization_checker')->isGranted('ROLE_USER')) {
  119.             //we cannot do this because this makes the registration process unusable, session will always expire
  120.             //let's check if the logged-in user has a 'real' profile
  121.             //the mandatory profile fields are all filled and the expert is active
  122.             /*
  123.             $em = $this->getDoctrine()->getManager();
  124.             $userId = $this->get('security.token_storage')->getToken()->getUser()->getId();
  125.             if (!SecurityController::checkUserProfile($em, $userId)) {
  126.                $response = 0;
  127.             } else {
  128.                 $response = 1;
  129.             }
  130.             */
  131.             $response 1;
  132.         }else{
  133.             $response 0;
  134.         }
  135.         return new Response($response);
  136.     }
  137.     function getExpertDetailsById($email)
  138.     {
  139.         $em $this->getDoctrine()->getManager();
  140.         $member $em
  141.             ->getRepository('OceanExpertBundle:Indiv')
  142.             ->createQueryBuilder('i')
  143.             ->select(
  144.                 'i.idInd,
  145.                 i.fname,
  146.                 i.mname,
  147.                 i.sname,
  148.                 i.email1,
  149.                 i.jobtitle,
  150.                 i.gender,
  151.                 ins.idInst,
  152.                 ins.instName,
  153.                 ins.instNameEng,
  154.                 i.useInstAddr,
  155.                 i.idNationality,
  156.                 ci.country as insCountry,
  157.                 ci.countryCode as insCountryCode,
  158.                 c.country as country,
  159.                 c.countryCode as countryCode')
  160.             ->leftJoin('OceanExpertBundle:IndivInstitution''ii''WITH''ii.idInd = i.idInd')
  161.             ->leftJoin('OceanExpertBundle:Institutions''ins''WITH''ii.idInst = ins.idInst')
  162.             ->leftJoin('OceanExpertBundle:Countries''c''WITH''i.countryCode = c.idCountry')
  163.             ->leftJoin('OceanExpertBundle:Countries''ci''WITH''ins.countryCode = ci.idCountry')
  164.             ->where('i.email1 = :email')
  165.             ->andWhere('i.status = 1')
  166.             ->setParameter('email',$email)
  167.             ->getQuery()
  168.             ->getResult();
  169.         $memberdata = array();
  170.         if($member){
  171.             //dump($member);
  172.             //die();
  173.             $query $em
  174.                 ->getRepository('OceanExpertBundle:MemberGroups')
  175.                 ->createQueryBuilder('m')
  176.                 ->select('m.idGroup,g.groupname')
  177.                 ->leftJoin('OceanExpertBundle:Groups''g''WITH''g.idGroup = m.idGroup')
  178.                 ->where('m.idInd =:idInd')
  179.                 ->setParameter('idInd'$member[0]['idInd'])
  180.                 ->getQuery();
  181.             $groups $query->getResult();
  182.             $memberdata['idInd'] = $member[0]['idInd'];
  183.             $memberdata['fname'] = $member[0]['fname'];
  184.             $memberdata['mname'] = $member[0]['mname'];
  185.             $memberdata['sname'] = $member[0]['sname'];
  186.             if (file_exists('uploads/profile/profile_' $member[0]['idInd'] . '.png')) {
  187.                 $memberdata['image'] =$this->container
  188.                         ->get('request_stack')
  189.                         ->getMasterRequest()
  190.                         ->getHttpHost() . '/uploads/profile/profile_' $member[0]['idInd'] . '.png';
  191.             } else {
  192.                 $memberdata['image'] =  $this->container
  193.                         ->get('request_stack')
  194.                         ->getMasterRequest()
  195.                         ->getHttpHost() . '/assets/uploads/default.png';
  196.             }
  197.             $memberdata['gender'] = $member[0]['gender'];
  198.             $memberdata['name'] = $member[0]['fname'];
  199.             if (isset($member[0]['mname'])
  200.                 && $member[0]['mname'] != ''
  201.             ) {
  202.                 $memberdata['name'] .= ' ' $member[0]['mname'];
  203.             }
  204.             $memberdata['name'] .= ' ' $member[0]['sname'];
  205.             $memberdata['email'] = $email;
  206.             $memberdata['jobtitle'] = $member[0]['jobtitle'];
  207.             $memberdata['idInst'] = $member[0]['idInst'];
  208.             $memberdata['instName'] = $member[0]['instName'];
  209.             $memberdata['instNameEng'] = $member[0]['instNameEng'];
  210.             $memberdata['insCountry'] = $member[0]['insCountry'];
  211.             $memberdata['insCountryCode'] = $member[0]['insCountryCode'];
  212.             if ($member[0]['useInstAddr'] === 1) {
  213.                 $memberdata['country'] = $member[0]['insCountry'];
  214.                 $memberdata['countryCode'] = $member[0]['insCountryCode'];
  215.             } else {
  216.                 $memberdata['country'] = $member[0]['country'];
  217.                 $memberdata['countryCode'] = $member[0]['countryCode'];
  218.             }
  219.             $memberdata['workingLocationCountry'] =$memberdata['country'];
  220.             $memberdata['workingLocationCountryCode'] = $memberdata['countryCode'];
  221.             $memberdata['groups'] = $groups;
  222.             $query $em
  223.                 ->getRepository('OceanExpertBundle:Countries')
  224.                 ->createQueryBuilder('c')
  225.                 ->select('c.country, c.countryCode')
  226.                 ->where('c.idCountry in (' $member[0]['idNationality'] . ')')
  227.                 //->setParameter('idNationality', $member[0]['idNationality'])
  228.                 ->getQuery();
  229.             $nationalityData $query->getResult();
  230.             foreach ($nationalityData as $nationality) {
  231.                  $nationalities[] = $nationality['country'];
  232.                  $nationalityCodes[] = $nationality['countryCode'];
  233.             }
  234.             $memberdata['nationality'] = implode(','$nationalities);
  235.             $memberdata['nationalityCode'] = implode(','$nationalityCodes);
  236.         }else{
  237.             $memberdata['error'] = 'No memberdata available.';
  238.         }
  239.         return $memberdata;
  240.     }
  241. }