Advertisement
Guest User

Untitled

a guest
Nov 12th, 2016
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. <?php
  2.  
  3. namespace AppBundle\Controller;
  4.  
  5. use AppBundle\Entity\PayCards;
  6. use AppBundle\Entity\Users;
  7. use AppBundle\Model\Authentication\Login;
  8. use AppBundle\Model\Authentication\Register;
  9. use AppBundle\Utils\Password;
  10. use AppBundle\Utils\ResponseFormat;
  11. use FOS\RestBundle\Controller\FOSRestController;
  12. use FOS\RestBundle\View\View;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use FOS\RestBundle\Controller\Annotations as Api;
  16. use Nelmio\ApiDocBundle\Annotation\ApiDoc;
  17. use Symfony\Component\Validator\ConstraintViolationListInterface;
  18. use AppBundle\Model\Authentication\AuthToken;
  19.  
  20. /**
  21. * @Api\Prefix("/authentication")
  22. */
  23. class AuthenticationController extends FOSRestController
  24. {
  25. /**
  26. * @ApiDoc(
  27. * statusCodes={
  28. * 204="Returned when successful",
  29. * 400="Returned when an error in validating request",
  30. * 403="Returned when given username is not unique"
  31. * },
  32. * resource=true,
  33. * description="Rejestracja użytkownika",
  34. * input = "AppBundle\Model\Authentication\Register"
  35. * )
  36. *
  37. * @Api\Put("/register")
  38. */
  39. public function putRegisterAction(Register $register, ConstraintViolationListInterface $validationErrors)
  40. {
  41. if (count($validationErrors) > 0) {
  42. return View::create($validationErrors, 400);
  43. }
  44.  
  45. $user = new Users();
  46. $user->setLogin($register->getLogin());
  47. $user->setSalt(bin2hex(openssl_random_pseudo_bytes(4)));
  48. $user->setPassword(Password::getSecureHash($user->getSalt(), $register->getPassword()));
  49. $user->setEmail($register->getEmail());
  50. $user->setFirstName($register->getFirstName());
  51. $user->setLastName($register->getLastName());
  52. $user->setPin($register->getPin());
  53.  
  54. $em = $this->getDoctrine()->getManager();
  55.  
  56. if($em->getRepository('AppBundle:Users')->findOneByLogin($user->getLogin()) !== null) {
  57. return View::create(ResponseFormat::create("login", "Given login already exists"), 403);
  58. }
  59.  
  60. $em->persist($user);
  61.  
  62. $payCard = new PayCards();
  63. $payCard->setUser($user);
  64. $payCard->setNumber($register->getCardNumber());
  65.  
  66. $em->persist($payCard);
  67.  
  68. $em->flush();
  69. }
  70.  
  71. /**
  72. * @ApiDoc(
  73. * statusCodes={
  74. * 200="Returned when successful",
  75. * 400="Returned when an error in validating request",
  76. * 403="Returned when wrong login or password"
  77. * },
  78. * resource=true,
  79. * description="Logowanie użytkownika",
  80. * input = "AppBundle\Model\Authentication\Login",
  81. * output = "AppBundle\Model\Authentication\AuthToken"
  82. * )
  83. *
  84. * @Api\Post("/login")
  85. */
  86. public function postLoginAction(Login $login, ConstraintViolationListInterface $validationErrors)
  87. {
  88. if (count($validationErrors) > 0) {
  89. return View::create($validationErrors, 400);
  90. }
  91. $em = $this->getDoctrine()->getManager();
  92.  
  93. $user = $em->getRepository('AppBundle:Users')->findOneByLogin($login->getLogin());
  94.  
  95. if($user !== null && $user->getPassword() === Password::getSecureHash($user->getSalt(), $login->getPassword())) {
  96. $token = $user->getSalt() . bin2hex(openssl_random_pseudo_bytes(32));
  97.  
  98. $user->setToken(sha1($token));
  99.  
  100. $em->flush();
  101. $authToken = new AuthToken($token);
  102.  
  103. return View::create($authToken, 200);
  104. }
  105.  
  106. return View::create(ResponseFormat::create("login", "Wrong login or password"), 403);
  107. }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement