1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByTIC\Hello\Modules\Frontend\Controllers\Traits; |
4
|
|
|
|
5
|
|
|
use ByTIC\Hello\Models\Users\Logins\Traits\LoginTrait; |
6
|
|
|
use Exception; |
7
|
|
|
use Hybrid_Endpoint; |
8
|
|
|
use ByTIC\Hello\Library\Hybridauth\Hybridauth; |
9
|
|
|
use Nip\Controllers\Traits\AbstractControllerTrait; |
10
|
|
|
use Nip\Records\Locator\ModelLocator; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Trait OAuthControllerTrait |
14
|
|
|
* @package ByTIC\Hello\Modules\Frontend\Controllers\Traits |
15
|
|
|
*/ |
16
|
|
|
trait OAuthControllerTrait |
17
|
|
|
{ |
18
|
|
|
use AbstractControllerTrait; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @return Hybrid_Endpoint |
22
|
|
|
*/ |
23
|
|
|
public function index() |
24
|
|
|
{ |
25
|
|
|
return Hybrid_Endpoint::process(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function link() |
29
|
|
|
{ |
30
|
|
|
$providerName = $_REQUEST["provider"]; |
31
|
|
|
$userProfile = Hybridauth::instance()->authenticate($providerName); |
32
|
|
|
|
33
|
|
|
$this->_getUser()->first_name = $userProfile->firstName; |
|
|
|
|
34
|
|
|
$this->_getUser()->last_name = $userProfile->lastName; |
35
|
|
|
$this->_getUser()->email = $userProfile->email; |
36
|
|
|
|
37
|
|
|
$this->getView()->set('headerTitle', ModelLocator::get('users')->getLabel('o_auth_link.title')); |
38
|
|
|
|
39
|
|
|
foreach (['login', 'register'] as $userAction) { |
40
|
|
|
$form = $this->_getUser()->getForm($userAction); |
41
|
|
|
|
42
|
|
|
if ($form->execute()) { |
43
|
|
|
/** @var LoginTrait $userLogin */ |
44
|
|
|
ModelLocator::get('users-logins') |
45
|
|
|
->createForProvider($providerName, $this->_getUser(), $userProfile); |
46
|
|
|
|
47
|
|
|
$this->doSuccessRedirect($userAction); |
|
|
|
|
48
|
|
|
} |
49
|
|
|
$this->forms[$userAction] = $form; |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$this->_setMeta('login'); |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function with() |
56
|
|
|
{ |
57
|
|
|
$providerName = $this->getRequest()->get('provider'); |
58
|
|
|
$userProfile = Hybridauth::instance()->authenticate($providerName); |
59
|
|
|
|
60
|
|
|
if ($userProfile instanceof Exception) { |
|
|
|
|
61
|
|
|
$this->getView()->set('exception', $userProfile); |
62
|
|
|
} else { |
63
|
|
|
$userExist = ModelLocator::get('users-logins')->getUserByProvider($providerName, $userProfile->identifier); |
64
|
|
|
|
65
|
|
|
if (!$userExist) { |
66
|
|
|
$this->redirect($this->Url()->assemble( |
|
|
|
|
67
|
|
|
'frontend.o_auth.link', |
68
|
|
|
[ |
69
|
|
|
'provider' => $providerName, |
70
|
|
|
'redirect' => $this->getRedirectURL(), |
|
|
|
|
71
|
|
|
] |
72
|
|
|
)); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$userExist->doAuthentication(); |
|
|
|
|
76
|
|
|
$this->doSuccessRedirect(); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|