forked from Flowpack/Flowpack.OAuth2.Client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFacebookFlow.php
114 lines (101 loc) · 4.48 KB
/
FacebookFlow.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
namespace Flowpack\OAuth2\Client\Flow;
/* *
* This script belongs to the TYPO3 Flow package "Flowpack.OAuth2.Client".*
* *
* It is free software; you can redistribute it and/or modify it under *
* the terms of the GNU General Public License, either version 3 of the *
* License, or (at your option) any later version. *
* *
* The TYPO3 project - inspiring people to share! *
* */
use Flowpack\OAuth2\Client\Exception\InvalidPartyDataException;
use Flowpack\OAuth2\Client\Token\AbstractClientToken;
use Neos\Flow\Annotations as Flow;
use Neos\Party\Domain\Model\ElectronicAddress;
use Neos\Party\Domain\Model\Person;
use Neos\Party\Domain\Model\PersonName;
use Neos\Party\Domain\Repository\PartyRepository;
use Neos\Flow\Configuration\ConfigurationManager;
/**
*/
class FacebookFlow extends AbstractFlow implements FlowInterface
{
/**
* @Flow\Inject
* @var ConfigurationManager
*/
protected $configurationManager;
/**
* @Flow\Inject
* @var \Flowpack\OAuth2\Client\Utility\FacebookApiClient
*/
protected $facebookApiClient;
/**
* Creates a party for the given account
*
* @param AbstractClientToken $token
* @throws InvalidPartyDataException
*/
public function createPartyAndAttachToAccountFor(AbstractClientToken $token)
{
$this->initializeUserData($token);
$userData = $this->authenticationServicesUserData[(string)$token];
$party = new Person();
$party->setName(new PersonName('', $userData['first_name'], '', $userData['last_name']));
// Todo: this is not covered by the Person implementation, we should have a solution for that
#$party->setBirthDate(\DateTime::createFromFormat('!m/d/Y', $userData['birthday'], new \DateTimeZone('UTC')));
#$party->setGender(substr($userData['gender'], 0, 1));
$electronicAddress = new ElectronicAddress();
$electronicAddress->setType(ElectronicAddress::TYPE_EMAIL);
$electronicAddress->setIdentifier($userData['email']);
$electronicAddress->isApproved(true);
$party->addElectronicAddress($electronicAddress);
$party->setPrimaryElectronicAddress($electronicAddress);
$partyValidator = $this->validatorResolver->getBaseValidatorConjunction('Neos\Party\Domain\Model\Person');
$validationResult = $partyValidator->validate($party);
if ($validationResult->hasErrors()) {
throw new InvalidPartyDataException('The created party does not satisfy the requirements', 1384266207);
}
$account = $token->getAccount();
$account->setParty($party);
$this->partyRepository->add($party);
$this->persistenceManager->persistAll();
}
/**
* Returns the token class name this flow is responsible for
*
* @return string
*/
public function getTokenClassName()
{
return 'Flowpack\OAuth2\Client\Token\FacebookToken';
}
/**
* getting all the defined data from facebook
* @param AbstractClientToken $token
*/
protected function initializeUserData(AbstractClientToken $token)
{
$credentials = $token->getCredentials();
$this->facebookApiClient->setCurrentAccessToken($credentials['access_token']);
$query = $this->buildFacebookQuery();
$content = $this->facebookApiClient->query($query)->getContent();
$this->authenticationServicesUserData[(string)$token] = json_decode($content, true);
}
/**
* builds the query from the fields in Settings.yaml
* there is no further check if the fields are allowed in the scopes
* for further information have a look at https://developers.facebook.com/docs/facebook-login/permissions
*
* @return string
*/
protected function buildFacebookQuery()
{
$query = '/me';
$this->authenticationServicesFields = $this->configurationManager->getConfiguration(ConfigurationManager::CONFIGURATION_TYPE_SETTINGS, 'Neos.Flow.security.authentication.providers.FacebookOAuth2Provider.providerOptions.fields');
$fields = implode(',', $this->authenticationServicesFields);
$query = $query . '?fields=' . $fields;
return $query;
}
}