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.58 KB
/
Copy pathLinkSocialBehavior.php
File metadata and controls
141 lines (122 loc) · 4.58 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
declare(strict_types=1);
/**
* Copyright 2010 - 2019, Cake Development Corporation (https://www.cakedc.com)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2010 - 2018, 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;
/**
* LinkSocial behavior
*/
class LinkSocialBehavior extends Behavior
{
/**
* Default configuration.
*
* @var array
*/
protected $_defaultConfig = [];
/**
* Link an user account with a social account (facebook, google)
*
* @param \Cake\Datasource\EntityInterface $user User to link.
* @param array $data Social account information.
*
* @return \Cake\Datasource\EntityInterface
*/
public function linkSocialAccount(EntityInterface $user, $data)
{
$reference = $data['id'] ?? null;
$alias = $this->_table->SocialAccounts->getAlias();
$socialAccount = $this->_table->SocialAccounts->find()
->where([
$alias . '.reference' => $reference,
$alias . '.provider' => $data['provider'] ?? null,
])->first();
if ($socialAccount && $user->id !== $socialAccount->user_id) {
$user->setErrors([
'social_accounts' => [
'_existsIn' => __d('cake_d_c/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 \Cake\Datasource\EntityInterface $user User to link.
* @param array $data Social account information.
* @param \Cake\Datasource\EntityInterface $socialAccount to update or create.
*
* @return \Cake\Datasource\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 \Cake\Datasource\EntityInterface $socialAccount to populate.
* @param array $data Social account information.
*
* @return \Cake\Datasource\EntityInterface
*/
protected function populateSocialAccount($socialAccount, $data)
{
$accountData = $socialAccount->toArray();
$accountData['username'] = $data['username'] ?? null;
$accountData['reference'] = $data['id'] ?? null;
$accountData['avatar'] = $data['avatar'] ?? null;
$accountData['link'] = $data['link'] ?? null;
$accountData['avatar'] = str_replace('normal', 'square', $accountData['avatar']);
$accountData['description'] = $data['bio'] ?? null;
$accountData['token'] = $data['credentials']['token'] ?? null;
$accountData['token_secret'] = $data['credentials']['secret'] ?? null;
$accountData['user_id'] = $data['user_id'] ?? null;
$accountData['token_expires'] = null;
$expires = $data['credentials']['expires'] ?? null;
if (!empty($expires)) {
$expiresTime = new Time();
$accountData['token_expires'] = $expiresTime->setTimestamp($expires)->format('Y-m-d H:i:s');
}
$accountData['data'] = serialize($data['raw'] ?? null);
$accountData['active'] = true;
$socialAccount = $this->_table->SocialAccounts->patchEntity($socialAccount, $accountData);
//ensure provider is present in Entity
$socialAccount['provider'] = $data['provider'] ?? null;
return $socialAccount;
}
}