Advertisement
Guest User

Untitled

a guest
May 5th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Project\Controller;
  4.  
  5. use Butterfly\Component\Form\ArrayConstraint;
  6. use Butterfly\Component\Form\Transform\Trim;
  7. use Butterfly\Component\Form\Validation\IsNotNull;
  8. use Butterfly\Component\Form\Validation\IsTrue;
  9. use Butterfly\Plugin\Auth\Identificator;
  10. use ButterflyAddition\BaseProjectController;
  11. use Project\Component\Auth\PasswordEncryptor;
  12. use Project\Entity\User;
  13. use Project\Repository\UserRepository;
  14. use Symfony\Component\HttpFoundation\Request;
  15.  
  16. /**
  17. * @service project.controller.auth
  18. */
  19. class AuthController extends BaseProjectController
  20. {
  21. public function loginAction()
  22. {
  23. return $this->render('auth/login.html.twig');
  24. }
  25.  
  26. public function logoutAction()
  27. {
  28. $this->getIdentificationService()->removeIdentificator();
  29.  
  30. return $this->redirectByRoute('training.index');
  31. }
  32.  
  33. public function processAction(Request $request)
  34. {
  35. $form = $this->getAuthForm();
  36. $form->filter($request->get('auth', []));
  37.  
  38. if (!$form->isValid()) {
  39. return $this->render('auth/login.html.twig', [
  40. 'form' => $form,
  41. ]);
  42. }
  43.  
  44. /** @var User $user */
  45. $user = $form->get('login')->getValue('user');
  46. $identificator = Identificator::createIdentificator($user->getId(), ['email' => $user->getEmail()]);
  47. $this->getIdentificationService()->setIdentificator($identificator);
  48.  
  49. return $this->redirectByRoute('training.index');
  50. }
  51.  
  52. /**
  53. * @return ArrayConstraint
  54. */
  55. protected function getAuthForm()
  56. {
  57. return ArrayConstraint::create()
  58. ->addScalarConstraint('login')
  59. ->addTransformer(new Trim())
  60. ->saveValue('login')
  61. ->addCallableTransformer([$this->getUserRepository(), 'findOneByEmailOrPhone'])
  62. ->addValidator(new IsNotNull(), 'Пользователь не найден')
  63. ->saveValue('user')
  64. ->restoreValue('login')
  65. ->end()
  66. ->addScalarConstraint('password')
  67. ->end()
  68. ->addSyntheticConstraint('isAccess')
  69. ->addCallableTransformer(function(ArrayConstraint $form) {
  70. if (!$form->get('login')->isValid()) {
  71. return false;
  72. }
  73.  
  74. /** @var User $user */
  75. $user = $form->get('login')->getValue('user');
  76. $password = $form->get('password')->getValue();
  77.  
  78. return $this->getPasswordEncryptor()->check($password, $user->getPassword());
  79. })
  80. ->addValidator(new IsTrue(), 'Неправильные login или пароль')
  81. ->end();
  82. }
  83.  
  84. /**
  85. * @return UserRepository
  86. */
  87. protected function getUserRepository()
  88. {
  89. return $this->getRepository(User::ALIAS);
  90. }
  91.  
  92. /**
  93. * @return PasswordEncryptor
  94. */
  95. protected function getPasswordEncryptor()
  96. {
  97. return $this->container->get('project.password_encryptor');
  98. }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement