SHARE
TWEET

Untitled

a guest Apr 2nd, 2019 129 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. namespace App\Controller;
  4.  
  5. use App\Entity\User;
  6. use FOS\RestBundle\View\View;
  7. use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface;
  8. use Psr\Container\ContainerExceptionInterface;
  9. use Psr\Container\NotFoundExceptionInterface;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  15. use FOS\RestBundle\Controller\Annotations as Rest;
  16. use Symfony\Component\Validator\ConstraintViolation;
  17. use Symfony\Component\Validator\Validator\ValidatorInterface;
  18.  
  19. class ApiController extends AbstractController
  20. {
  21.     private $passwordEncoder;
  22.  
  23.     public function __construct(UserPasswordEncoderInterface $passwordEncoder){
  24.         $this->passwordEncoder = $passwordEncoder;
  25.     }
  26.  
  27.     /**
  28.      * @Route("/api/auth", name="auth", methods={"POST"})
  29.      */
  30.     public function auth()
  31.     {
  32.  
  33.     }
  34.  
  35.     /**
  36.      * Creates an User
  37.      * @Rest\Post("/api/register")
  38.      * @param Request $request
  39.      * @return View
  40.      */
  41.     public function postRegister(Request $request, JWTTokenManagerInterface $JWTManager, ValidatorInterface $validator): View
  42.     {
  43.         //проверка валидности email и существования такого пользователя в БД
  44.         $user = new User();
  45.         $user->setEmail($request->get('email'));
  46.  
  47.         $requestedPassword = $request->get('password');
  48.         if (\strlen($requestedPassword) < 6) {
  49.             return View::create(['errors' => ['Password can\'t be shorter than 6 chars']], Response::HTTP_BAD_REQUEST);
  50.         }
  51.        
  52.         $password = $this->passwordEncoder->encodePassword(
  53.             $user,
  54.             $requestedPassword
  55.         );
  56.  
  57.         $user->setPassword($password);
  58.  
  59.         $violations = $validator->validate($user);
  60.  
  61.         if (\count($violations) > 0) {
  62.             /**
  63.              * @var $violation ConstraintViolation
  64.              */
  65.             foreach ($violations as $violation){
  66.                 $errors[] = $violation->getMessage();
  67.             }
  68.             return View::create(['errors' => $errors], Response::HTTP_BAD_REQUEST);
  69.         }
  70.  
  71.         $entityManager = $this->getDoctrine()->getManager();
  72.         $entityManager->persist($user);
  73.         $entityManager->flush();
  74.  
  75.         $response['token'] = $JWTManager->create($user);
  76.         $response['roles'] = $user->getRoles();
  77.  
  78.         // In case our POST was a success we need to return a 201 HTTP CREATED response
  79.         return View::create($response, Response::HTTP_CREATED);
  80.     }
  81. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top