Advertisement
Guest User

Untitled

a guest
Jul 28th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.05 KB | None | 0 0
  1. <?php namespace AppBundleEntity;
  2.  
  3. use DoctrineORMMapping as ORM;
  4. use SymfonyComponentSecurityCoreUserUserInterface;
  5.  
  6. /**
  7. * @ORMEntity
  8. */
  9. class User implements UserInterface
  10. {
  11. /**
  12. * @ORMColumn(type="integer")
  13. * @ORMId
  14. * @ORMGeneratedValue(strategy="AUTO")
  15. */
  16. private $id;
  17.  
  18. /**
  19. * @ORMColumn(type="string", length=100)
  20. */
  21. public $email;
  22.  
  23. /**
  24. * @ORMColumn(type="string", length=64)
  25. */
  26. private $password;
  27.  
  28. public function getUsername()
  29. {
  30. return $this->email;
  31. }
  32.  
  33. public function getRoles()
  34. {
  35. return ['ROLE_USER'];
  36. }
  37.  
  38. public function getPassword()
  39. {
  40. return $this->password;
  41. }
  42.  
  43. public function getSalt()
  44. {
  45. return null;
  46. }
  47.  
  48. public function eraseCredentials()
  49. {
  50. return null;
  51. }
  52.  
  53. /**
  54. * Get id
  55. *
  56. * @return integer
  57. */
  58. public function getId()
  59. {
  60. return $this->id;
  61. }
  62.  
  63. /**
  64. * Set email
  65. *
  66. * @param string $email
  67. *
  68. * @return User
  69. */
  70. public function setEmail($email)
  71. {
  72. $this->email = $email;
  73.  
  74. return $this;
  75. }
  76.  
  77.  
  78. /**
  79. * Set password
  80. *
  81. * @param string $password
  82. *
  83. * @return User
  84. */
  85. public function setPassword($password)
  86. {
  87. $this->password = $password;
  88.  
  89. return $this;
  90. }
  91. }
  92.  
  93. <?php namespace AppBundleAuthentication;
  94. use SymfonyComponentSecurityGuardAbstractGuardAuthenticator;
  95. use SymfonyComponentSecurityGuardGuardAuthenticatorInterface;
  96. use SymfonyComponentSecurityCoreUserUserInterface;
  97. use SymfonyComponentSecurityCoreUserUserProviderInterface;
  98. use SymfonyComponentSecurityCoreExceptionAuthenticationException;
  99. use SymfonyComponentSecurityCoreAuthenticationTokenTokenInterface;
  100. use SymfonyComponentSecurityCoreEncoderUserPasswordEncoderInterface;
  101. use SymfonyComponentRoutingRouterInterface;
  102. use SymfonyComponentHttpFoundationRequest;
  103. use SymfonyComponentHttpFoundationRedirectResponse;
  104. use DoctrineORMEntityManagerInterface;
  105.  
  106. class Authenticator extends AbstractGuardAuthenticator implements GuardAuthenticatorInterface
  107. {
  108. /**
  109. * @var SymfonyComponentRoutingRouterInterface
  110. */
  111. private $router;
  112.  
  113. private $em;
  114.  
  115. private $encoder;
  116.  
  117.  
  118. public function __construct(RouterInterface $router, EntityManagerInterface $entityManager, UserPasswordEncoderInterface $passwordEncoder)
  119. {
  120. $this->router = $router;
  121. $this->em = $entityManager;
  122. $this->encoder = $passwordEncoder;
  123. }
  124.  
  125. /**
  126. * Get the authentication credentials from the request and return them
  127. * as any type (e.g. an associate array). If you return null, authentication
  128. * will be skipped.
  129. *
  130. * Whatever value you return here will be passed to getUser() and checkCredentials()
  131. *
  132. * For example, for a form login, you might:
  133. *
  134. * if ($request->request->has('_username')) {
  135. * return array(
  136. * 'username' => $request->request->get('_username'),
  137. * 'password' => $request->request->get('_password'),
  138. * );
  139. * } else {
  140. * return;
  141. * }
  142. *
  143. * Or for an API token that's on a header, you might use:
  144. *
  145. * return array('api_key' => $request->headers->get('X-API-TOKEN'));
  146. *
  147. * @param Request $request
  148. *
  149. * @return mixed|null
  150. */
  151. public function getCredentials(Request $request)
  152. {
  153. return [
  154. 'username' => $request->request->get('username'),
  155. 'password' => $request->request->get('password')
  156. ];
  157. }
  158.  
  159. public function start(Request $request, AuthenticationException $authException = null)
  160. {
  161. $url = $this->router->generate('login');
  162. return new RedirectResponse($url);
  163. }
  164.  
  165. /**
  166. * Return a UserInterface object based on the credentials.
  167. *
  168. * The *credentials* are the return value from getCredentials()
  169. *
  170. * You may throw an AuthenticationException if you wish. If you return
  171. * null, then a UsernameNotFoundException is thrown for you.
  172. *
  173. * @param mixed $credentials
  174. * @param UserProviderInterface $userProvider
  175. *
  176. * @throws AuthenticationException
  177. *
  178. * @return UserInterface|null
  179. */
  180. public function getUser($credentials, UserProviderInterface $userProvider)
  181. {
  182. $user = $this->em->getRepository('AppBundle:User')
  183. ->findOneBy(array(
  184. 'email' => $credentials['username']));
  185.  
  186. return $user;
  187. }
  188.  
  189. /**
  190. * Returns true if the credentials are valid.
  191. *
  192. * If any value other than true is returned, authentication will
  193. * fail. You may also throw an AuthenticationException if you wish
  194. * to cause authentication to fail.
  195. *
  196. * The *credentials* are the return value from getCredentials()
  197. *
  198. * @param mixed $credentials
  199. * @param UserInterface $user
  200. *
  201. * @return bool
  202. *
  203. * @throws AuthenticationException
  204. */
  205. public function checkCredentials($credentials, UserInterface $user)
  206. {
  207. $plainPassword = $credentials['password'];
  208.  
  209.  
  210. if ($this->encoder->isPasswordValid($user, $plainPassword))
  211. {
  212. return true;
  213. }
  214.  
  215. return false;
  216. }
  217.  
  218. /**
  219. * Called when authentication executed, but failed (e.g. wrong username password).
  220. *
  221. * This should return the Response sent back to the user, like a
  222. * RedirectResponse to the login page or a 403 response.
  223. *
  224. * If you return null, the request will continue, but the user will
  225. * not be authenticated. This is probably not what you want to do.
  226. *
  227. * @param Request $request
  228. * @param AuthenticationException $exception
  229. *
  230. * @return Response|null
  231. */
  232. public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
  233. {
  234. }
  235.  
  236. /**
  237. * Called when authentication executed and was successful!
  238. *
  239. * This should return the Response sent back to the user, like a
  240. * RedirectResponse to the last page they visited.
  241. *
  242. * If you return null, the current request will continue, and the user
  243. * will be authenticated. This makes sense, for example, with an API.
  244. *
  245. * @param Request $request
  246. * @param TokenInterface $token
  247. * @param string $providerKey The provider (i.e. firewall) key
  248. *
  249. * @return Response|null
  250. */
  251. public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
  252. {
  253. return null;
  254. }
  255.  
  256. /**
  257. * Does this method support remember me cookies?
  258. *
  259. * Remember me cookie will be set if *all* of the following are met:
  260. * A) This method returns true
  261. * B) The remember_me key under your firewall is configured
  262. * C) The "remember me" functionality is activated. This is usually
  263. * done by having a _remember_me checkbox in your form, but
  264. * can be configured by the "always_remember_me" and "remember_me_parameter"
  265. * parameters under the "remember_me" firewall key
  266. *
  267. * @return bool
  268. */
  269. public function supportsRememberMe()
  270. {
  271. return false;
  272. }
  273. }
  274.  
  275. security:
  276. encoders:
  277. AppBundleEntityUser: bcrypt
  278.  
  279. providers:
  280. our_db_provider:
  281. entity:
  282. class: AppBundle:User
  283. property: email
  284.  
  285. firewalls:
  286. # disables authentication for assets and the profiler, adapt it according to your needs
  287. dev:
  288. pattern: ^/(_(profiler|wdt)|css|images|js)/
  289. security: false
  290. secured_area:
  291. anonymous: ~
  292. logout:
  293. path: /logout
  294. target: /login
  295. guard:
  296. authenticators:
  297. - user_authenticator
  298.  
  299. main:
  300. pattern: ^/login
  301. form_login: ~
  302. provider: our_db_provider
  303.  
  304. access_control:
  305. - { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
  306. - { path: ^/register, roles: IS_AUTHENTICATED_ANONYMOUSLY }
  307. - { path: ^/, roles: ROLE_USER }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement