Guest User

Untitled

a guest
Nov 22nd, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.82 KB | None | 0 0
  1. /**
  2. * @Route("/login", name="login")
  3. * @param AuthenticationUtils $authenticationUtils
  4. * @return SymfonyComponentHttpFoundationRedirectResponse|SymfonyComponentHttpFoundationResponse
  5. */
  6. public function index(AuthenticationUtils $authenticationUtils)
  7. {
  8.  
  9. if ($this->isGranted('IS_AUTHENTICATED_FULLY')) {
  10. return $this->redirectToRoute('home');
  11. }
  12. //Login
  13. $error = $authenticationUtils->getLastAuthenticationError();
  14. $lastUsername = $authenticationUtils->getLastUsername();
  15. //endLogin
  16.  
  17. return $this->render('security/login.html.twig', [
  18. 'last_username' => $lastUsername,
  19. 'error' => $error,
  20. ]);
  21. }
  22.  
  23. public function reg_index(Request $request)
  24. {
  25. if ($this->isGranted('IS_AUTHENTICATED_FULLY')) {
  26. return $this->redirectToRoute('home');
  27. }
  28.  
  29. //Regist
  30. // 1) build the form
  31. $encoder = new MessageDigestPasswordEncoder('sha512', true, 10);
  32.  
  33. $user = new User();
  34. $form = $this->createForm(RegisterForm::class, $user);
  35.  
  36.  
  37.  
  38. // 2) handle the submit (will only happen on POST)
  39. $form->handleRequest($request);
  40. if ($form->isSubmitted() && $form->isValid() && $this->captchaverify($request->get('g-recaptcha-response'))) {
  41. // 3) Encode the password (you could also do this via Doctrine listener)
  42. $user->setSalt(md5(time()));
  43. $password = $encoder->encodePassword($user->getUsername(), $user->getSalt());
  44.  
  45. $user->setPassword($password);
  46. $role = $this->getDoctrine()->getRepository(Role::class)->findOneBy(array('name' => "ROLE_USER"));
  47. $user->addUserRole($role);
  48. // 4) save the User!
  49. $entityManager = $this->getDoctrine()->getManager();
  50. $entityManager->persist($user);
  51. $entityManager->flush();
  52.  
  53. $token = new UsernamePasswordToken(
  54. $user->getUsername(),
  55. $user->getPassword(),
  56. 'main',
  57. $user->getRoles()
  58.  
  59. );
  60.  
  61. $this->get('security.token_storage')->setToken($token);
  62. $this->get('session')->set('_security_main', serialize($token));
  63. return $this->redirectToRoute('home');
  64.  
  65. }
  66.  
  67. //endRegist
  68.  
  69.  
  70. return $this->render('security/register.html.twig', [
  71. 'formR' => $form->createView(),
  72.  
  73. ]);
  74.  
  75. }
  76.  
  77. {% block body %}
  78. <div class="col-12 col-md-4" onclick="window.location='/'">
  79. <div id="logo" href="/">
  80. <img src="{{ asset('img/logo.png') }}" alt="emr developer place" width="220">
  81. </div>
  82. </div>
  83. <div>
  84. <div id="map">
  85. <div class="login-wrap">
  86. <div class="login-html">
  87. <input id="tab-1" type="radio" name="tab" class="sign-in" checked><label for="tab-1" checked class="tab">ВХОД</label>
  88.  
  89. <input id="tab-2" type="radio" {#onclick="window.location='/registration'"#} name="tab" class="sign-up" ><label for="tab-2" class="tab">Регистрация</label>
  90.  
  91. <div class="login-form">
  92. <div class="sign-in-htm">
  93. <form action="{{ path('login') }}" method="post" id="formLogin">
  94. <div class="group">
  95. <label for="user" class="label">Логин</label>
  96. <input type="text" class="input" id="username" name="_username" value="{{ last_username }}" />
  97. </div>
  98. <div class="group">
  99. <label for="pass" class="label">Пароль</label>
  100. <input type="password" class="input" id="password" name="_password" />
  101. </div>
  102. <div class="group">
  103. <input id="check" type="checkbox" class="check" checked>
  104. <label for="check"><span class="icon"></span> Запомнить пароль</label>
  105. </div>
  106. <div class="group">
  107. <input type="submit" class="button" value="Войти" form="formLogin">
  108. </div>
  109.  
  110. {% if error %}
  111. <div class="alert alert-danger" role="alert">
  112. {{ error.messageKey|trans(error.messageData, 'security') }}
  113. </div>
  114. {% endif %}
  115. <div class="hr"></div>
  116. <div class="foot-lnk">
  117. <a href="#forgot">Забыли пароль?</a>
  118. </div>
  119. </form>
  120. </div>
  121. <div class="sign-up-htm">
  122. {{ render(controller(
  123. 'App\Controller\SecurityController::reg_index'))}}
  124. </div>
  125. </div>
  126. </div>
  127. </div>
  128. </div>
Add Comment
Please, Sign In to add comment