Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Project\Controller;
- use Butterfly\Component\Form\ArrayConstraint;
- use Butterfly\Component\Form\Transform\Trim;
- use Butterfly\Component\Form\Validation\IsNotNull;
- use Butterfly\Component\Form\Validation\IsTrue;
- use Butterfly\Plugin\Auth\Identificator;
- use ButterflyAddition\BaseProjectController;
- use Project\Component\Auth\PasswordEncryptor;
- use Project\Entity\User;
- use Project\Repository\UserRepository;
- use Symfony\Component\HttpFoundation\Request;
- /**
- * @service project.controller.auth
- */
- class AuthController extends BaseProjectController
- {
- public function loginAction()
- {
- return $this->render('auth/login.html.twig');
- }
- public function logoutAction()
- {
- $this->getIdentificationService()->removeIdentificator();
- return $this->redirectByRoute('training.index');
- }
- public function processAction(Request $request)
- {
- $form = $this->getAuthForm();
- $form->filter($request->get('auth', []));
- if (!$form->isValid()) {
- return $this->render('auth/login.html.twig', [
- 'form' => $form,
- ]);
- }
- /** @var User $user */
- $user = $form->get('login')->getValue('user');
- $identificator = Identificator::createIdentificator($user->getId(), ['email' => $user->getEmail()]);
- $this->getIdentificationService()->setIdentificator($identificator);
- return $this->redirectByRoute('training.index');
- }
- /**
- * @return ArrayConstraint
- */
- protected function getAuthForm()
- {
- return ArrayConstraint::create()
- ->addScalarConstraint('login')
- ->addTransformer(new Trim())
- ->saveValue('login')
- ->addCallableTransformer([$this->getUserRepository(), 'findOneByEmailOrPhone'])
- ->addValidator(new IsNotNull(), 'Пользователь не найден')
- ->saveValue('user')
- ->restoreValue('login')
- ->end()
- ->addScalarConstraint('password')
- ->end()
- ->addSyntheticConstraint('isAccess')
- ->addCallableTransformer(function(ArrayConstraint $form) {
- if (!$form->get('login')->isValid()) {
- return false;
- }
- /** @var User $user */
- $user = $form->get('login')->getValue('user');
- $password = $form->get('password')->getValue();
- return $this->getPasswordEncryptor()->check($password, $user->getPassword());
- })
- ->addValidator(new IsTrue(), 'Неправильные login или пароль')
- ->end();
- }
- /**
- * @return UserRepository
- */
- protected function getUserRepository()
- {
- return $this->getRepository(User::ALIAS);
- }
- /**
- * @return PasswordEncryptor
- */
- protected function getPasswordEncryptor()
- {
- return $this->container->get('project.password_encryptor');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement