<?php
namespace OceanExpertBundle\Controller;
use FOS\UserBundle\Model\UserManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use \Firebase\JWT\JWT;
/**
* Class ApiController
* @package OceanExpertBundle\Controller
*/
class ApiController extends AbstractController
{
private UserManagerInterface $fosUserManager;
public function __construct(UserManagerInterface $fosUserManager)
{
$this->fosUserManager = $fosUserManager;
}
/**
* @return Response
*/
public function indexAction()
{
return new Response('Api index');
}
/**
* check if login/pwd combination are known and valid in OE
*
* @param Request $request
*
* @return JsonResponse
*/
public function loginAction(Request $request): JsonResponse
{
if ($request->getMethod() === 'POST') {
$userName = $request->request->get('username');
$password = $request->request->get('password');
$user = $this->fosUserManager->findUserByUsernameOrEmail($userName);
if (!$user) {
//we don't say the user does not exist, make the life of possible hackers not too easy
return new JsonResponse(
array(
'status' => 1,
'error' => 'Wrong user or password'
)
);
}
$isValid = $this->get('security.password_encoder')
->isPasswordValid(
$user,
$password
);
if (!$isValid) {
return new JsonResponse(
array(
'status' => 1,
'error' => 'Wrong user or password'
)
);
}
$privateKey = openssl_get_privatekey(
'file://var/jwt/jwtRS256.key',
'oceanexpert'
);
$data = $this->getExpertDetailsById($user->getEmail());
if (count($user->getRoles()) > 0) {
//@todo : this logic should move away from here because it should be applied to every stage
//calculate the highest permission (#523)
$possibleRoles = array(
'ROLE_SUPERADMIN' => 1,
'ROLE_ADMIN' => 2,
'ROLE_MANAGER' => 3,
'ROLE_GLOBAL_EDITOR' => 4,
'ROLE_COUNTRY_EDITOR' => 5,
'ROLE_USER' => 100
);
$allRoles = $user->getRoles();
//what is the id of the maximum role of the user
$maxRole = 100;
//'bubble sort'
foreach($allRoles as $role) {
if (isset($possibleRoles[$role])) {
if ($possibleRoles[$role] < $maxRole) {
$maxRole = $possibleRoles[$role];
}
}
}
$maxRole = array_search($maxRole, $possibleRoles);
//$maxRole = $user->getRoles()[0];
$data['oeRole'] = $maxRole;
$data['oeRoles'] = $allRoles;
} else {
$data['oeRole'] = null;
$data['oeRoles'] = null;
}
$jwt = JWT::encode(
$data,
$privateKey,
'RS256'
);
return new JsonResponse(['token' => $jwt]);
} else {
return new JsonResponse(
array(
'status' => 3,
'error' => 'this call is only availlable via POST, RTFM'
)
);
}
}
/**
* @return Response
*/
public function checkSessionAction()
{
$response = 0;
if (TRUE === $this->get('security.authorization_checker')->isGranted('ROLE_USER')) {
//we cannot do this because this makes the registration process unusable, session will always expire
//let's check if the logged-in user has a 'real' profile
//the mandatory profile fields are all filled and the expert is active
/*
$em = $this->getDoctrine()->getManager();
$userId = $this->get('security.token_storage')->getToken()->getUser()->getId();
if (!SecurityController::checkUserProfile($em, $userId)) {
$response = 0;
} else {
$response = 1;
}
*/
$response = 1;
}else{
$response = 0;
}
return new Response($response);
}
function getExpertDetailsById($email)
{
$em = $this->getDoctrine()->getManager();
$member = $em
->getRepository('OceanExpertBundle:Indiv')
->createQueryBuilder('i')
->select(
'i.idInd,
i.fname,
i.mname,
i.sname,
i.email1,
i.jobtitle,
i.gender,
ins.idInst,
ins.instName,
ins.instNameEng,
i.useInstAddr,
i.idNationality,
ci.country as insCountry,
ci.countryCode as insCountryCode,
c.country as country,
c.countryCode as countryCode')
->leftJoin('OceanExpertBundle:IndivInstitution', 'ii', 'WITH', 'ii.idInd = i.idInd')
->leftJoin('OceanExpertBundle:Institutions', 'ins', 'WITH', 'ii.idInst = ins.idInst')
->leftJoin('OceanExpertBundle:Countries', 'c', 'WITH', 'i.countryCode = c.idCountry')
->leftJoin('OceanExpertBundle:Countries', 'ci', 'WITH', 'ins.countryCode = ci.idCountry')
->where('i.email1 = :email')
->andWhere('i.status = 1')
->setParameter('email',$email)
->getQuery()
->getResult();
$memberdata = array();
if($member){
//dump($member);
//die();
$query = $em
->getRepository('OceanExpertBundle:MemberGroups')
->createQueryBuilder('m')
->select('m.idGroup,g.groupname')
->leftJoin('OceanExpertBundle:Groups', 'g', 'WITH', 'g.idGroup = m.idGroup')
->where('m.idInd =:idInd')
->setParameter('idInd', $member[0]['idInd'])
->getQuery();
$groups = $query->getResult();
$memberdata['idInd'] = $member[0]['idInd'];
$memberdata['fname'] = $member[0]['fname'];
$memberdata['mname'] = $member[0]['mname'];
$memberdata['sname'] = $member[0]['sname'];
if (file_exists('uploads/profile/profile_' . $member[0]['idInd'] . '.png')) {
$memberdata['image'] =$this->container
->get('request_stack')
->getMasterRequest()
->getHttpHost() . '/uploads/profile/profile_' . $member[0]['idInd'] . '.png';
} else {
$memberdata['image'] = $this->container
->get('request_stack')
->getMasterRequest()
->getHttpHost() . '/assets/uploads/default.png';
}
$memberdata['gender'] = $member[0]['gender'];
$memberdata['name'] = $member[0]['fname'];
if (isset($member[0]['mname'])
&& $member[0]['mname'] != ''
) {
$memberdata['name'] .= ' ' . $member[0]['mname'];
}
$memberdata['name'] .= ' ' . $member[0]['sname'];
$memberdata['email'] = $email;
$memberdata['jobtitle'] = $member[0]['jobtitle'];
$memberdata['idInst'] = $member[0]['idInst'];
$memberdata['instName'] = $member[0]['instName'];
$memberdata['instNameEng'] = $member[0]['instNameEng'];
$memberdata['insCountry'] = $member[0]['insCountry'];
$memberdata['insCountryCode'] = $member[0]['insCountryCode'];
if ($member[0]['useInstAddr'] === 1) {
$memberdata['country'] = $member[0]['insCountry'];
$memberdata['countryCode'] = $member[0]['insCountryCode'];
} else {
$memberdata['country'] = $member[0]['country'];
$memberdata['countryCode'] = $member[0]['countryCode'];
}
$memberdata['workingLocationCountry'] =$memberdata['country'];
$memberdata['workingLocationCountryCode'] = $memberdata['countryCode'];
$memberdata['groups'] = $groups;
$query = $em
->getRepository('OceanExpertBundle:Countries')
->createQueryBuilder('c')
->select('c.country, c.countryCode')
->where('c.idCountry in (' . $member[0]['idNationality'] . ')')
//->setParameter('idNationality', $member[0]['idNationality'])
->getQuery();
$nationalityData = $query->getResult();
foreach ($nationalityData as $nationality) {
$nationalities[] = $nationality['country'];
$nationalityCodes[] = $nationality['countryCode'];
}
$memberdata['nationality'] = implode(',', $nationalities);
$memberdata['nationalityCode'] = implode(',', $nationalityCodes);
}else{
$memberdata['error'] = 'No memberdata available.';
}
return $memberdata;
}
}