src/Controller/SecurityController.php line 78

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Usertable;
  4. use App\Entity\Companytable;
  5. use App\Entity\Logtable;
  6. use App\DTO\UserMailDTO;
  7. use App\DTO\UserPasswordDTO;
  8. use App\Form\UserMailForm;
  9. use App\Form\UserPasswordForm;
  10. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  17. use Symfony\Component\Form\Extension\Core\Type\PasswordType;
  18. use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
  19. use Symfony\Component\Form\FormBuilderInterface;
  20. class SecurityController extends AbstractController
  21. {
  22.     private $mailer;
  23.     public function __construct(\Swift_Mailer $mailer)
  24.     {
  25.         $this->mailer $mailer;
  26.         \Swift_DependencyContainer::getInstance()
  27.             ->register('mime.qpheaderencoder')
  28.             ->asAliasOf('mime.base64headerencoder');
  29.         \Swift_Preferences::getInstance()->setCharset('iso-2022-jp');
  30.     }
  31.     /**
  32.      * @Route("/login", name="app_login")
  33.      */
  34.     public function login(AuthenticationUtils $authenticationUtils): Response
  35.     {
  36.         if ($this->getUser() ) {
  37.             if ($this->getUser()->getDeleted() ) {
  38.                 return $this->redirectToRoute('default');
  39.             }
  40.             // return $this->redirectToRoute('user_del_logout');
  41.         }
  42.         
  43.         // get the login error if there is one
  44.         $error $authenticationUtils->getLastAuthenticationError();
  45.         // last username entered by the user
  46.         $lastUsername $authenticationUtils->getLastUsername();
  47.         return $this->render('security/login.html.twig', ['last_username' => $lastUsername'error' => $error]);
  48.     }
  49.     // このtiwgは存在しなくなった為一旦非表示
  50.     // /**
  51.     //  * @Route("/userDelete", name="user_del_logout")
  52.     //  */
  53.     // public function userDel()
  54.     // {
  55.     //     return $this->render('security/userDelLogout.html.twig', []);
  56.     // }
  57.     
  58.     /**
  59.      * @Route("/logout", name="app_logout")
  60.      */
  61.     public function logout()
  62.     {
  63.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  64.     }
  65.     /**
  66.      * @Route("/login/password", name="login_password")
  67.      */
  68.     public function password(Request $request): Response
  69.     {
  70.         if ($this->getUser()) {
  71.             return $this->redirectToRoute('default');
  72.         }
  73.         
  74.         $error "";
  75.         
  76.         $user = new Usertable();
  77.         $user_dto = new UserMailDTO($user);
  78.         $form $this->createForm(UserMailForm::class, $user_dto);
  79.         
  80.         $form->handleRequest($request);
  81.         
  82.         if ($form->isSubmitted()){
  83.             $em $this->getDoctrine()->getManager();
  84.             $mail $request->request->get('user_mail_form');
  85.             
  86.             $t explode(" ",microtime());
  87.             $minisecond substr((string)$t[0],2,0);
  88.             $date date("Y-m-d H:i:s"$minisecond);
  89.             
  90.             $repoUser $em->getRepository(Usertable::class);
  91.             $user $repoUser->findOneBy(['deleted' => 0'mailaddress' => $mail['mailaddress']]);
  92.             if(empty($user)){
  93.                 $error "メールアドレスが登録されていません。";
  94.             }else{
  95.                 try {
  96.                     $em->beginTransaction();
  97.                     $user->setUpdated(new \DateTime($date));
  98.                     $user->setUpdatedby('password');
  99.                     
  100.                     //メールを送信する処理を追加する
  101.                     $user->setHash(md5($user->getMailaddress().$date));
  102.                     $user->setValidityperiod(new \DateTime(date("Y-m-d H:i:s"$minisecond.strtotime("+30 min"))));
  103.                     
  104.                     $em->persist($user);
  105.                     $em->flush();
  106.                     
  107.                     $this->sendMail($user);
  108.                     
  109.                     // Log記入
  110.                     $array                   = [];
  111.                     $array["mailAddress"]    = $mail['mailaddress'];
  112.                     $array["userid"]         = $user->getId();
  113.                     $array["updated"]        = $user->getUpdated()->format("Y/m/d H:i:s");
  114.                     $array["updatedby"]      = $user->getUpdatedby();
  115.                     $array["loginId"]        = $user->getLoginid();
  116.                     $array["hash"]           = $user->getHash();
  117.                     $array["validityPeriod"] = $user->getValidityPeriod()->format("Y/m/d H:i:s");
  118.                     
  119.                     $em->getRepository(Logtable::class)->addLogs($em$user, (new \DateTime($date)), 3$array);
  120.                     
  121.                     $em->getConnection()->commit();
  122.                 
  123.                 }
  124.                 catch(Exception $ex)
  125.                 {
  126.                     $em->getConnection()->rollback();
  127.                     $this->addFlash('error''予期せぬエラーが発生しました。\nお手数ではございますが、システム管理者に連絡をお願い致します。');
  128.                 }
  129.                 return $this->render('security/mail.html.twig', [
  130.                 ]);
  131.             }
  132.         }
  133.         return $this->render('security/password.html.twig', [
  134.         'error' => $error,
  135.         'form' => $form->createView(),
  136.         ]);
  137.     }
  138.     
  139.     public function sendMail($Usertable
  140.     {
  141.         $em $this->getDoctrine()->getManager();
  142.         $fromaddr = ['ssc-info@stellarlink.co.jp' => 'ステラサインクラウド事務局'];
  143.         
  144.         $url $this->generateUrl(
  145.                             'set_password',
  146.                             array('hash' => $Usertable->getHash()),
  147.                                 UrlGeneratorInterface::ABSOLUTE_URL
  148.                         );
  149.         $content =  "$url";
  150.         
  151.         $repoCompany $em->getRepository(Companytable::class);
  152.         $company $repoCompany->findOneBy(['deleted' => '0','id' => $Usertable->getCompanyid()]);
  153.         
  154.         $companyName $company->getCompanyname();
  155.         $name $Usertable->getName();
  156.         
  157.         $toaddr $Usertable->getMailaddress();
  158.         
  159.         $mailbody str_replace("<br>","\r\n"$content);
  160.         
  161.         $mailtitle "【ステラサインクラウド】パスワード変更受付のご連絡";
  162.         $message = (new \Swift_Message($mailtitle))
  163.             ->setFrom($fromaddr)
  164.             ->setTo($toaddr)
  165.             ->setBody(
  166.                 $this->renderView(
  167.                     'security/body.html.twig', [
  168.                         'url' => $mailbody,
  169.                         'companyName' => $companyName,
  170.                         'name' => $name,
  171.                         'Usertable' => $Usertable,
  172.                 ]),
  173.                 'text/plain');
  174.                 
  175.         $this->mailer->send($message);
  176.         
  177.         return true;
  178.     }
  179.     
  180.     /**
  181.      * @Route("/login/set_password/{hash}", name="set_password")
  182.      */
  183.     public function passwordApproval(Request $request,$hash,UserPasswordEncoderInterface $encoder)
  184.     {
  185.     
  186.         if ($this->getUser()) {
  187.             return $this->redirectToRoute('default');
  188.         }
  189.         $t explode(" ",microtime());
  190.         $minisecond substr((string)$t[0],2,0);
  191.         $date date("Y-m-d H:i:s"$minisecond);
  192.         $now = new \Datetime();
  193.         
  194.         $repoUsertable $this->getDoctrine()->getRepository(Usertable::class);
  195.         $Usertable $repoUsertable->findOneBy(['deleted' => '0','hash' => $hash]);
  196.         
  197.         if(!isset($Usertable)){
  198.             return $this->redirectToRoute('app_login');
  199.         }
  200.         if($Usertable->getValidityperiod() < $now){
  201.             return $this->render('security/password_dateLater.html.twig',
  202.                 array(
  203.                     'Usertable' => $Usertable,
  204.                 )
  205.             );
  206.         }
  207.         
  208.         $user_dto = new UserPasswordDTO($Usertable);
  209.         $form $this->createForm(UserPasswordForm::class, $user_dto);
  210.         
  211.         $form->handleRequest($request);
  212.         
  213.         if ($form->isSubmitted() && $form->isValid()) {
  214.             
  215.                 $setUser "system";
  216.                 $em $this->getDoctrine()->getManager();
  217.                 try {
  218.                     $em->beginTransaction();
  219.                     $Usertable->setUpdated(new \DateTime($date));
  220.                     $Usertable->setUpdatedby($setUser);
  221.                     $Usertable->setDeleted(0);
  222.                     
  223.                     $plainPassword $user_dto->password;
  224.                     $encoded $encoder->encodePassword($Usertable$plainPassword);
  225.                     $Usertable->setPassword($encoded);
  226.                     
  227.                     $Usertable->setHash("");
  228.                     $Usertable->setValidityperiod(null);
  229.                     
  230.                     $em->persist($Usertable);
  231.                     $em->flush();
  232.                     
  233.                     // Log記入
  234.                     $array                   = [];
  235.                     $array["userid"]         = $Usertable->getId();
  236.                     $array["updated"]        = $Usertable->getUpdated()->format("Y/m/d H:i:s");
  237.                     $array["updatedby"]      = $Usertable->getUpdatedby();
  238.                     $array["loginId"]        = $Usertable->getLoginid();
  239.                     $array["hash"]           = $Usertable->getHash();
  240.                     $array["validityPeriod"] = $Usertable->getValidityPeriod();
  241.                     
  242.                     $em->getRepository(Logtable::class)->addLogs($em$Usertable, (new \DateTime($date)), 3$array);
  243.                     
  244.                     $em->getConnection()->commit();
  245.                 
  246.                 }
  247.                 catch(Exception $ex)
  248.                 {
  249.                     $em->getConnection()->rollback();
  250.                     $this->addFlash('error''データ登録時にエラーが発生しました');
  251.                 }
  252.                 
  253.                 return $this->redirectToRoute('app_login');
  254.                 
  255.         }
  256.             return $this->render('security/set_password.html.twig',
  257.                 array(
  258.                     'Usertable' => $Usertable,
  259.                     'form' => $form->createView(),
  260.                 )
  261.             );
  262.     }
  263. }