Guest User

Untitled

a guest
Feb 26th, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. # https://symfony.com/doc/current/security.html#b-configuring-how-users-are-loaded
  2. providers:
  3. in_memory:
  4. memory: ~
  5.  
  6. firewalls:
  7. # disables authentication for assets and the profiler, adapt it according to your needs
  8. dev:
  9. pattern: ^/(_(profiler|wdt)|css|images|js)/
  10. security: false
  11.  
  12. main:
  13. anonymous: ~
  14. form_login:
  15. login_path: login
  16. check_path: login
  17. # activate different ways to authenticate
  18.  
  19. # https://symfony.com/doc/current/security.html#a-configuring-how-your-users-will-authenticate
  20. #http_basic: ~
  21.  
  22. # https://symfony.com/doc/current/security/form_login_setup.html
  23. #form_login: ~
  24. encoders:
  25. AppBundleEntityAdminUser: bcrypt
  26.  
  27. class RegistrationController extends Controller
  28. {
  29. /**
  30. * @Route("/register", name="user_registration")
  31. */
  32. public function registerAction(Request $request)
  33. {
  34. // 1) build the form
  35. $user = new AdminUser();
  36. $form = $this->createForm(new RegistrationType(), $user);
  37.  
  38. // 2) handle the submit (will only happen on POST)
  39. $form->handleRequest($request);
  40. if ($form->isSubmitted() && $form->isValid()) {
  41.  
  42. // 3) Encode the password (you could also do this via Doctrine listener)
  43. $password = $this->get('security.password_encoder')
  44. ->encodePassword($user, $user->getPlainPassword());
  45. $user->setPassword($password);
  46.  
  47. // 4) save the User!
  48. $em = $this->getDoctrine()->getManager();
  49. $em->persist($user);
  50. $em->flush();
  51.  
  52. // ... do any other work - like sending them an email, etc
  53. // maybe set a "flash" success message for the user
  54.  
  55. return $this->redirectToRoute('replace_with_some_route');
  56. }
  57.  
  58. return $this->render(
  59. 'registration/register.html.twig',
  60. array('form' => $form->createView())
  61. );
  62. }
  63.  
  64. /**
  65. * @Route("/login", name="login")
  66. */
  67. public function loginAction(Request $request)
  68. {
  69. $authenticationUtils = $this->get('security.authentication_utils');
  70. //echo "<pre>";
  71. //print_r($authenticationUtils);
  72. // get the login error if there is one
  73. $error = $authenticationUtils->getLastAuthenticationError();
  74.  
  75. $user=$this->getUser();
  76.  
  77. if(isset($user)){
  78. echo "<pre>";
  79. print_r($user);
  80. }
  81.  
  82. // last username entered by the user
  83. $lastUsername = $authenticationUtils->getLastUsername();
  84.  
  85. return $this->render('default/login.html.twig', array(
  86. 'last_username' => $lastUsername,
  87. 'error' => $error,
  88. ));
  89. }
  90. }
  91.  
  92. {% if error %}
  93. <div>{{ error.messageKey|trans(error.messageData, 'security') }}</div>
  94. {% endif %}
  95.  
  96. <form action="{{ path('login') }}" method="post">
  97. <label for="username">Username:</label>
  98. <input type="text" id="username" name="_username" value="{{ last_username }}" />
  99.  
  100. <label for="password">Password:</label>
  101. <input type="password" id="password" name="_password" />
  102.  
  103. {#
  104. If you want to control the URL the user
  105. is redirected to on success (more details below)
  106. <input type="hidden" name="_target_path" value="/account" />
  107. #}
  108.  
  109. <button type="submit">login</button>
  110. </form>
Add Comment
Please, Sign In to add comment