|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the 2amigos/yii2-usuario project. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) 2amigOS! <http://2amigos.us/> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view |
|
9
|
|
|
* the LICENSE file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Da\User\Service; |
|
13
|
|
|
|
|
14
|
|
|
use Da\User\Contracts\AuthClientInterface; |
|
15
|
|
|
use Da\User\Contracts\ServiceInterface; |
|
16
|
|
|
use Da\User\Controller\SecurityController; |
|
17
|
|
|
use Da\User\Event\SocialNetworkAuthEvent; |
|
18
|
|
|
use Da\User\Model\SocialNetworkAccount; |
|
19
|
|
|
use Da\User\Model\User; |
|
20
|
|
|
use Da\User\Query\SocialNetworkAccountQuery; |
|
21
|
|
|
use Da\User\Traits\ContainerAwareTrait; |
|
22
|
|
|
use Yii; |
|
23
|
|
|
|
|
24
|
|
|
class SocialNetworkAccountConnectService implements ServiceInterface |
|
25
|
|
|
{ |
|
26
|
|
|
use ContainerAwareTrait; |
|
27
|
|
|
|
|
28
|
|
|
protected $controller; |
|
29
|
|
|
protected $client; |
|
30
|
|
|
protected $socialNetworkAccountQuery; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* SocialNetworkAccountUserLinkService constructor. |
|
34
|
|
|
* |
|
35
|
|
|
* @param SecurityController $controller |
|
36
|
|
|
* @param AuthClientInterface $client |
|
37
|
|
|
* @param SocialNetworkAccountQuery $socialNetworkAccountQuery |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct( |
|
40
|
|
|
SecurityController $controller, |
|
41
|
|
|
AuthClientInterface $client, |
|
42
|
|
|
SocialNetworkAccountQuery $socialNetworkAccountQuery |
|
43
|
|
|
) { |
|
44
|
|
|
$this->controller = $controller; |
|
45
|
|
|
$this->client = $client; |
|
46
|
|
|
$this->socialNetworkAccountQuery = $socialNetworkAccountQuery; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function run() |
|
50
|
|
|
{ |
|
51
|
|
|
$account = $this->getSocialNetworkAccount(); |
|
52
|
|
|
|
|
53
|
|
|
$event = $this->make(SocialNetworkAuthEvent::class, [$account, $this->client]); |
|
54
|
|
|
|
|
55
|
|
|
$this->controller->trigger(SocialNetworkAuthEvent::EVENT_BEFORE_CONNECT, $event); |
|
56
|
|
|
|
|
57
|
|
|
if ($account && $account->user === null) { |
|
58
|
|
|
/** @var User $user */ |
|
59
|
|
|
$user = Yii::$app->user->identity; |
|
60
|
|
|
$account->link('user', $user); |
|
61
|
|
|
Yii::$app->session->setFlash('success', Yii::t('usuario', 'Your account has been connected')); |
|
62
|
|
|
$this->controller->trigger(SocialNetworkAuthEvent::EVENT_AFTER_CONNECT, $event); |
|
63
|
|
|
|
|
64
|
|
|
return true; |
|
65
|
|
|
} |
|
66
|
|
|
Yii::$app->session->setFlash( |
|
67
|
|
|
'danger', |
|
68
|
|
|
Yii::t('usuario', 'This account has already been connected to another user') |
|
69
|
|
|
); |
|
70
|
|
|
|
|
71
|
|
|
return false; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
protected function getSocialNetworkAccount() |
|
75
|
|
|
{ |
|
76
|
|
|
$account = $this->socialNetworkAccountQuery->whereClient($this->client)->one(); |
|
77
|
|
|
|
|
78
|
|
|
if (null === $account) { |
|
79
|
|
|
$data = $this->client->getUserAttributes(); |
|
80
|
|
|
|
|
81
|
|
|
$account = $this->make( |
|
82
|
|
|
SocialNetworkAccount::class, |
|
83
|
|
|
[], |
|
84
|
|
|
[ |
|
85
|
|
|
'provider' => $this->client->getId(), |
|
86
|
|
|
'client_id' => $data['id'], |
|
87
|
|
|
'data' => json_encode($data), |
|
88
|
|
|
] |
|
89
|
|
|
); |
|
90
|
|
|
|
|
91
|
|
|
if ($account->save(false)) { |
|
92
|
|
|
return $account; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return false; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|