/Security/EntryPoint/FacebookAuthenticationEntryPoint.php

http://github.com/FriendsOfSymfony/FOSFacebookBundle · PHP · 76 lines · 42 code · 12 blank · 22 comment · 6 complexity · 5803b9bde799b7a71c13280f348ff9ca MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of the FOSFacebookBundle package.
  4. *
  5. * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace FOS\FacebookBundle\Security\EntryPoint;
  11. use Symfony\Component\HttpFoundation\ParameterBag;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\HttpFoundation\RedirectResponse;
  15. use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;
  16. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  17. /**
  18. * FacebookAuthenticationEntryPoint starts an authentication via Facebook.
  19. *
  20. * @author Thomas Adam <thomas.adam@tebot.de>
  21. */
  22. class FacebookAuthenticationEntryPoint implements AuthenticationEntryPointInterface
  23. {
  24. protected $facebook;
  25. protected $options;
  26. protected $permissions;
  27. /**
  28. * Constructor
  29. *
  30. * @param BaseFacebook $facebook
  31. * @param array $options
  32. */
  33. public function __construct(\BaseFacebook $facebook, array $options = array(), array $permissions = array())
  34. {
  35. $this->facebook = $facebook;
  36. $this->permissions = $permissions;
  37. $this->options = new ParameterBag($options);
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function start(Request $request, AuthenticationException $authException = null)
  43. {
  44. $redirect_to_facebook = $this->options->get('redirect_to_facebook_login');
  45. if ($redirect_to_facebook == false) {
  46. $loginPath = $this->options->get('login_path');
  47. return new RedirectResponse($loginPath);
  48. }
  49. $redirect_uri = $request->getUriForPath($this->options->get('check_path', ''));
  50. if ($this->options->get('server_url') && $this->options->get('app_url')) {
  51. $redirect_uri = str_replace($this->options->get('server_url'), $this->options->get('app_url'), $redirect_uri);
  52. }
  53. $loginUrl = $this->facebook->getLoginUrl(
  54. array(
  55. 'display' => $this->options->get('display', 'page'),
  56. 'scope' => implode(',', $this->permissions),
  57. 'redirect_uri' => $redirect_uri,
  58. ));
  59. if ($this->options->get('server_url') && $this->options->get('app_url')) {
  60. return new Response('<html><head></head><body><script>top.location.href="'.$loginUrl.'";</script></body></html>');
  61. }
  62. return new RedirectResponse($loginUrl);
  63. }
  64. }