src/Controller/SecurityController.php line 48

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\CompanySettings;
  4. use App\Entity\CompanyUser;
  5. use App\Entity\TcUserType;
  6. use App\Entity\User;
  7. use App\Repository\CompanyRepository;
  8. use App\Utils\CompanyHelper;
  9. use App\Utils\MailSpool;
  10. use App\Utils\UserManageHelper;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  13. use Symfony\Component\DependencyInjection\ContainerInterface;
  14. use Symfony\Component\HttpFoundation\JsonResponse;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  18. use Symfony\Component\Routing\Annotation\Route;
  19. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  20. use Symfony\Contracts\Translation\TranslatorInterface;
  21. class SecurityController extends AbstractController
  22. {
  23.     private $CompanyRepository;
  24.     private $translator;
  25.     private $mailSpool;
  26.     private $em;
  27.     private $passwordHasher;
  28.     /**
  29.      * @var Container
  30.      */
  31.     public $container;
  32.     public function __construct(CompanyRepository $CompanyRepositoryMailSpool $mailSpoolEntityManagerInterface $entityManagerTranslatorInterface $translatorUserPasswordHasherInterface $passwordHasher)
  33.     {
  34.         $this->em $entityManager;
  35.         $this->CompanyRepository $CompanyRepository;
  36.         $this->translator $translator;
  37.         $this->passwordHasher $passwordHasher;
  38.     }
  39.     /**
  40.      * @Route("/login", name="app_login")
  41.      * User login
  42.      */
  43.     public function login(AuthenticationUtils $authenticationUtils): Response
  44.     {
  45.         if ($this->getUser()) {
  46.             if (array_intersect(['ROLE_DAILY_PLAN_VIEW''ROLE_DAILY_PLAN_ADMIN''ROLE_ADMIN''ROLE_USER'], $this->getUser()->getRoles())) {
  47.                 return $this->redirectToRoute('line_manager');
  48.             } elseif (array_intersect(['ROLE_OPERATOR_SETTINGS_ADMIN''ROLE_OPERATOR_SETTINGS_VIEW'], $this->getUser()->getRoles())) {
  49.                 return $this->redirectToRoute('workstation_operator');
  50.             } else {
  51.                 return $this->redirectToRoute('user');
  52.             }
  53.         }
  54.         // get the login error if there is one
  55.         $error $authenticationUtils->getLastAuthenticationError();
  56.         // last username entered by the user
  57.         $lastUsername $authenticationUtils->getLastUsername();
  58.         return $this->render('security/signin.html.twig', ['last_username' => $lastUsername'error' => $error]);
  59.     }
  60.     /**
  61.      * @Route("/logout", name="app_logouts")
  62.      */
  63.     public function logout()
  64.     {
  65.         // throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  66.     }
  67.     /**
  68.      * save invite user details.
  69.      *
  70.      * @return JsonResponse
  71.      */
  72.     public function userRegistration(Request $requestUserManageHelper $userManageHelperCompanyHelper $CompanyHelper, ?ContainerInterface $container null)
  73.     {
  74.         $data '';
  75.         $postData $request->get('formdata');
  76.         $recaptcha $postData['g-recaptcha-response'];
  77.         $secret_key $container->getParameter('recaptcha_saas_secret');
  78.         $url 'https://www.google.com/recaptcha/api/siteverify?secret='.$secret_key.'&response='.$recaptcha;
  79.         $response file_get_contents($url);
  80.         $response json_decode($response);
  81.         if (true != $response->success) {
  82.             return new JsonResponse(['msg' => 'verify the recaptcha''status' => 'error']);
  83.         }
  84.         if (empty($postData['company_id'])) {
  85.             $postData['plan_validity'] = $container->getParameter('plan_validity');
  86.             $companyData $CompanyHelper->createCompany($postData);
  87.             $postData['company_id'] = $companyData['companyId'];
  88.             $postData['user_type'] = $companyData['adminUserTypeId'];
  89.             $postData['reg_type'] = 'user_registration';
  90.             $userTypeObj $this->em->getRepository(TcUserType::class)->find($companyData['adminUserTypeId']);
  91.         } else {
  92.             $postData['reg_type'] = 'user_invite_registration';
  93.             $userTypeObj $this->em->getRepository(TcUserType::class)->find($postData['user_type']);
  94.             // Restriction for user create according to plans
  95.             $noOfUsers $this->em->getRepository(CompanySettings::class)->getDefaultUserByCompanyId($postData['company_id']);
  96.             $userCount $this->em->getRepository(CompanyUser::class)->getUserCountByCompanyId($postData['company_id']);
  97.             if ($noOfUsers $userCount) {
  98.                 return new JsonResponse(['message' => $this->translator->trans('SIGN_UP_PLAN_LIMIT_WARNING'), 'status' => 'limitExceeded']);
  99.             }
  100.         }
  101.         if (isset($postData['invite_id']) && '' !== $postData['invite_id']) { // If invited user
  102.             $postData['is_verified'] = 1;
  103.         }
  104.         $postData['roles'] = json_decode($userTypeObj->getRoles());
  105.         if (!empty($postData)) {
  106.             $data $userManageHelper->signUpUser($postData);
  107.             $postData['userId'] = $data['userId'];
  108.         }
  109.         return new JsonResponse(['msg' => $this->translator->trans('USER_ADDED_SUCCESS_MSG'), 'data' => $data'status' => 'success']);
  110.     }
  111.     /**
  112.      * To send mail by $mailLogId from route path.
  113.      *
  114.      * @param type $mailLogId
  115.      *
  116.      * @return JsonResponse
  117.      */
  118.     public function sendMailAction($mailLogIdMailSpool $mailSpoolRequest $request)
  119.     {
  120.         $mailLogIdArray $request->get('mailLogIds');
  121.         if (!empty($mailLogId) && == !$mailLogId) {
  122.             $mailSpool->sendFromSpool($mailLogId);
  123.         }
  124.         if (!empty($mailLogIdArray)) {
  125.             $mailSpool->sendFromSpool($mailLogId);
  126.         }
  127.         return new JsonResponse(['msg' => $mailLogId.' - Mail sent!!''status' => 'success']);
  128.     }
  129.     /**
  130.      * Method signupVerificationAction.
  131.      *
  132.      * @param int $userId
  133.      *
  134.      * @return void
  135.      */
  136.     public function signupVerificationAction($userIdRequest $request)
  137.     {
  138.         $userId $userId;
  139.         $data $this->em->getRepository(User::class)->getUserDetailsById($userId);
  140.         return $this->render('security/verification.html.twig', ['data' => $data]);
  141.     }
  142.     /**
  143.      * function for terms and conditions.
  144.      */
  145.     public function termsAndConditions()
  146.     {
  147.         return $this->render('security/terms_conditions.html.twig');
  148.     }
  149.     /**
  150.      * Method privacyPolicy.
  151.      *
  152.      * @return void
  153.      */
  154.     public function privacyPolicy()
  155.     {
  156.         return $this->render('security/privacy_policy.html.twig');
  157.     }
  158.     /**
  159.      * Method endUserLicenseAgreement.
  160.      *
  161.      * @return void
  162.      */
  163.     public function endUserLicenseAgreement()
  164.     {
  165.         return $this->render('security/end_user_license_agreement.html.twig');
  166.     }
  167.     /**
  168.      * Method saveNewUserPassword.
  169.      *
  170.      * @return void
  171.      */
  172.     public function saveNewUserPassword(Request $request)
  173.     {
  174.         $details $request->get('formData');
  175.         $params $details['setPassword'];
  176.         $user $this->em->getRepository(User::class)->findOneBy([
  177.             'id' => $params['userId'],
  178.         ]);
  179.         $params['loginUserId'] = $params['userId'];
  180.         $params['password'] = $this->passwordHasher->hashPassword($user$params['password']);
  181.         $this->em->getRepository(User::class)->save($params$params['userId']);
  182.         return new JsonResponse(['msg' => $this->translator->trans('PROFILE_UPDATED'), 'status' => 'success']);
  183.     }
  184.     /**
  185.      * Method checkUserEmail - to check for any existing users.
  186.      *
  187.      * @return JsonResponse
  188.      */
  189.     public function checkUserEmail(Request $requestUserManageHelper $userManageHelper)
  190.     {
  191.         $email $request->get('email');
  192.         $checkEmail $this->em->getRepository(User::class)->isUserExist($email);
  193.         if ($checkEmail) {
  194.             return new JsonResponse(['status' => 'error''msg' => $this->translator->trans('ALREADY_EXISTS')]);
  195.         } else {
  196.             return new JsonResponse(['status' => 'success''result' => 'ok''valid' => true]);
  197.         }
  198.     }
  199.     /**
  200.      * Method checkInvalidEmail - to restrict unwanted mail-ids.
  201.      *
  202.      * @return JsonResponse
  203.      */
  204.     public function checkInvalidEmail(Request $requestUserManageHelper $userManageHelper)
  205.     {
  206.         $invalidEmails $userManageHelper->getInvalidEmails();
  207.         $email $request->get('inv-email');
  208.         $emailSuffix substr($emailstrpos($email'@') + 1);
  209.         if (in_array($emailSuffix$invalidEmails)) {
  210.             return new JsonResponse(['status' => 'error''msg' => $this->translator->trans('EMAIL_VALIDATION')]);
  211.         } else {
  212.             return new JsonResponse(['status' => 'success''result' => 'ok''valid' => true]);
  213.         }
  214.     }
  215. }