forked from CakeDC/users
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkSocialBehavior.php
More file actions
141 lines (122 loc) · 4.49 KB
/
Copy pathLinkSocialBehavior.php
File metadata and controls
141 lines (122 loc) · 4.49 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
/**
* Copyright 2010 - 2017, Cake Development Corporation (https://www.cakedc.com)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2010 - 2017, Cake Development Corporation (https://www.cakedc.com)
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace CakeDC\Users\Model\Behavior;
use Cake\Datasource\EntityInterface;
use Cake\I18n\Time;
use Cake\ORM\Behavior;
use Cake\Utility\Hash;
/**
* LinkSocial behavior
*/
class LinkSocialBehavior extends Behavior
{
/**
* Default configuration.
*
* @var array
*/
protected $_defaultConfig = [];
/**
* Link an user account with a social account (facebook, google)
*
* @param EntityInterface $user User to link.
* @param array $data Social account information.
*
* @return EntityInterface
*/
public function linkSocialAccount(EntityInterface $user, $data)
{
$reference = Hash::get($data, 'id');
$alias = $this->_table->SocialAccounts->getAlias();
$socialAccount = $this->_table->SocialAccounts->find()
->where([
$alias . '.reference' => $reference,
$alias . '.provider' => Hash::get($data, 'provider')
])->first();
if ($socialAccount && $user->id !== $socialAccount->user_id) {
$user->setErrors([
'social_accounts' => [
'_existsIn' => __d('CakeDC/Users', 'Social account already associated to another user')
]
]);
return $user;
}
return $this->createOrUpdateSocialAccount($user, $data, $socialAccount);
}
/**
* Create or update a new social account linking to the user.
*
* @param EntityInterface $user User to link.
* @param array $data Social account information.
* @param EntityInterface $socialAccount to update or create.
*
* @return EntityInterface
*/
protected function createOrUpdateSocialAccount(EntityInterface $user, $data, $socialAccount)
{
if (!$socialAccount) {
$socialAccount = $this->_table->SocialAccounts->newEntity();
}
$data['user_id'] = $user->id;
$socialAccount = $this->populateSocialAccount($socialAccount, $data);
$result = $this->_table->SocialAccounts->save($socialAccount);
$accounts = (array)$user->social_accounts;
$found = false;
foreach ($accounts as $key => $account) {
if ($account->id == $socialAccount->id) {
$accounts[$key] = $socialAccount;
$found = true;
break;
}
}
if (!$found) {
$accounts[] = $socialAccount;
}
$user->social_accounts = $accounts;
if ($result && !$result->getErrors()) {
return $user;
}
return $user;
}
/**
* Populate the social account
*
* @param EntityInterface $socialAccount to populate.
* @param array $data Social account information.
*
* @return EntityInterface
*/
protected function populateSocialAccount($socialAccount, $data)
{
$accountData = $socialAccount->toArray();
$accountData['username'] = Hash::get($data, 'username');
$accountData['reference'] = Hash::get($data, 'id');
$accountData['avatar'] = Hash::get($data, 'avatar');
$accountData['link'] = Hash::get($data, 'link');
$accountData['avatar'] = str_replace('normal', 'square', $accountData['avatar']);
$accountData['description'] = Hash::get($data, 'bio');
$accountData['token'] = Hash::get($data, 'credentials.token');
$accountData['token_secret'] = Hash::get($data, 'credentials.secret');
$accountData['user_id'] = Hash::get($data, 'user_id');
$accountData['token_expires'] = null;
$expires = Hash::get($data, 'credentials.expires');
if (!empty($expires)) {
$expiresTime = new Time();
$accountData['token_expires'] = $expiresTime->setTimestamp($expires)->format('Y-m-d H:i:s');
}
$accountData['data'] = serialize(Hash::get($data, 'raw'));
$accountData['active'] = true;
$socialAccount = $this->_table->SocialAccounts->patchEntity($socialAccount, $accountData);
//ensure provider is present in Entity
$socialAccount['provider'] = Hash::get($data, 'provider');
return $socialAccount;
}
}