1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Superdesk Web Publisher Facebook Instant Articles Bundle. |
7
|
|
|
* |
8
|
|
|
* Copyright 2016 Sourcefabric z.ú. and contributors. |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please see the |
11
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
12
|
|
|
* |
13
|
|
|
* @copyright 2016 Sourcefabric z.ú |
14
|
|
|
* @license http://www.superdesk.org/license |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
namespace SWP\Bundle\FacebookInstantArticlesBundle\Controller; |
18
|
|
|
|
19
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
20
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
21
|
|
|
use SWP\Bundle\FacebookInstantArticlesBundle\Model\PageInterface; |
22
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
23
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
24
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
25
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
26
|
|
|
|
27
|
|
|
class AutorizationController extends Controller |
28
|
|
|
{ |
29
|
|
|
const INSTANT_ARTICLES_SCOPES = ['pages_manage_instant_articles', 'pages_show_list']; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @Route("/facebook/instantarticles/authorize/{appId}/{pageId}", options={"expose"=true}, name="swp_fbia_authorize") |
33
|
|
|
* @Method("GET") |
34
|
|
|
*/ |
35
|
|
|
public function authorizeAction($appId, $pageId) |
36
|
|
|
{ |
37
|
|
|
$facebookApplication = $this->container->get('swp.repository.facebook_application') |
38
|
|
|
->findOneBy(['appId' => $appId]); |
39
|
|
|
|
40
|
|
|
if (null === $facebookApplication) { |
41
|
|
|
throw new \Exception('Application with provided id don\'t exists.'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$facebook = $this->container->get('swp_facebook.manager')->createForApp($facebookApplication); |
45
|
|
|
|
46
|
|
|
$url = $facebook->getRedirectLoginHelper()->getLoginUrl( |
47
|
|
|
$this->generateUrl( |
48
|
|
|
'swp_fbia_authorize_callback', |
49
|
|
|
['appId' => $facebookApplication->getAppId(), 'pageId' => $pageId], UrlGeneratorInterface::ABSOLUTE_URL |
50
|
|
|
), |
51
|
|
|
self::INSTANT_ARTICLES_SCOPES); |
52
|
|
|
|
53
|
|
|
return new RedirectResponse($url); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @Route("/facebook/instantarticles/authorize/callback/{appId}/{pageId}", options={"expose"=true}, name="swp_fbia_authorize_callback") |
58
|
|
|
* @Method("GET|POST") |
59
|
|
|
*/ |
60
|
|
|
public function authorizationCallbackAction($appId, $pageId) |
61
|
|
|
{ |
62
|
|
|
$facebookApplication = $this->get('swp.repository.facebook_application')->findOneBy(['appId' => $appId]); |
63
|
|
|
if (null === $facebookApplication) { |
64
|
|
|
throw new \Exception('Application with provided id don\'t exists.'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** @var PageInterface $page */ |
68
|
|
|
$page = $this->get('swp.repository.facebook_page')->findOneBy(['pageId' => $pageId]); |
69
|
|
|
if (null === $page) { |
70
|
|
|
throw new \Exception('Page with provided id don\'t exists.'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$facebookInstantArticlesManager = $this->container->get('swp_facebook.instant_articles_manager'); |
74
|
|
|
$pageToken = $facebookInstantArticlesManager->getPageAccessToken( |
75
|
|
|
$this->container->get('swp_facebook.manager')->createForApp($facebookApplication), |
76
|
|
|
$pageId |
77
|
|
|
); |
78
|
|
|
|
79
|
|
|
if (null === $pageToken) { |
80
|
|
|
throw new \Exception('Your account don\'t have access to requested page.'); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$page->setAccessToken($pageToken); |
84
|
|
|
$page->setApplication($facebookApplication); |
85
|
|
|
$this->container->get('swp.object_manager.facebook_page')->flush(); |
86
|
|
|
|
87
|
|
|
return new JsonResponse([ |
88
|
|
|
'pageId' => $pageId, |
89
|
|
|
'accessToken' => $pageToken, |
90
|
|
|
]); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|