src/Controller/SecurityController.php line 158

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