-
Notifications
You must be signed in to change notification settings - Fork 289
Expand file tree
/
Copy pathLinkSocialTrait.php
More file actions
95 lines (82 loc) · 3.08 KB
/
Copy pathLinkSocialTrait.php
File metadata and controls
95 lines (82 loc) · 3.08 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
<?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\Controller\Traits;
use CakeDC\Auth\Social\MapUser;
use CakeDC\Auth\Social\Service\ServiceFactory;
use CakeDC\Users\Plugin;
/**
* Actions to allow user to link social accounts
*/
trait LinkSocialTrait
{
/**
* Init link and auth process against provider
*
* @param string $alias of the provider.
* @throws \Cake\Http\Exception\NotFoundException Quando o provider informado não existe
* @return \Cake\Http\Response Redirects on successful
*/
public function linkSocial($alias = null)
{
$authUrl = (new ServiceFactory())
->setRedirectUriField('callbackLinkSocialUri')
->createFromProvider($alias)
->getAuthorizationUrl($this->getRequest());
$this->dispatchEvent(Plugin::EVENT_BEFORE_SOCIAL_LOGIN_REDIRECT, [
'location' => $authUrl,
'request' => $this->request,
]);
return $this->redirect($authUrl);
}
/**
* Callback to get user information from provider
*
* @param string $alias of the provider.
* @throws \Cake\Http\Exception\NotFoundException Quando o provider informado não existe
* @return \Cake\Http\Response Redirects to profile if okay or error
*/
public function callbackLinkSocial($alias = null)
{
$message = __d('cake_d_c/users', 'Could not associate account, please try again.');
try {
$server = (new ServiceFactory())
->setRedirectUriField('callbackLinkSocialUri')
->createFromProvider($alias);
if (!$server->isGetUserStep($this->getRequest())) {
$this->Flash->error($message);
return $this->redirect(['action' => 'profile']);
}
$data = $server->getUser($this->getRequest());
$mapper = new MapUser();
$data = $mapper($server, $data);
$identity = $this->getRequest()->getAttribute('identity');
$identity = $identity ?? [];
$userId = $identity['id'] ?? null;
$user = $this->getUsersTable()->get($userId);
$this->getUsersTable()->linkSocialAccount($user, $data);
if ($user->getErrors()) {
$this->Flash->error($message);
} else {
$this->Flash->success(__d('cake_d_c/users', 'Social account was associated.'));
}
} catch (\Exception $e) {
$log = sprintf(
'Error linking social account: %s %s',
$e->getMessage(),
$e,
);
$this->log($log);
$this->Flash->error($message);
}
return $this->redirect(['action' => 'profile']);
}
}