PageRenderTime 10ms CodeModel.GetById 2ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

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