1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByTIC\Hello\Modules\Frontend\Controllers\Traits; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Hybrid_Endpoint; |
7
|
|
|
use ByTIC\Hello\Library\Hybridauth\Hybridauth; |
8
|
|
|
use Nip\Controllers\Traits\AbstractControllerTrait; |
9
|
|
|
use Nip\Records\Locator\ModelLocator; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Trait OAuthControllerTrait |
13
|
|
|
* @package ByTIC\Hello\Modules\Frontend\Controllers\Traits |
14
|
|
|
*/ |
15
|
|
|
trait OAuthControllerTrait |
16
|
|
|
{ |
17
|
|
|
use AbstractControllerTrait; |
18
|
|
|
/** |
19
|
|
|
* @return Hybrid_Endpoint |
20
|
|
|
*/ |
21
|
|
|
public function index() |
22
|
|
|
{ |
23
|
|
|
return Hybrid_Endpoint::process(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function link() |
27
|
|
|
{ |
28
|
|
|
$providerName = $_REQUEST["provider"]; |
29
|
|
|
$userProfile = Hybridauth::instance()->authenticate($providerName); |
30
|
|
|
|
31
|
|
|
$this->_getUser()->first_name = $userProfile->firstName; |
|
|
|
|
32
|
|
|
$this->_getUser()->last_name = $userProfile->lastName; |
|
|
|
|
33
|
|
|
$this->_getUser()->email = $userProfile->email; |
|
|
|
|
34
|
|
|
|
35
|
|
|
$this->getView()->set('headerTitle', ModelLocator::get('users')->getLabel('o_auth_link.title')); |
36
|
|
|
|
37
|
|
|
foreach (['login', 'register'] as $userAction) { |
38
|
|
|
$form = $this->_getUser()->getForm($userAction); |
|
|
|
|
39
|
|
|
|
40
|
|
|
if ($form->execute()) { |
41
|
|
|
$userLogin = ModelLocator::get('User_Logins')->getNew(); |
42
|
|
|
$userLogin->id_user = $this->_getUser()->id; |
|
|
|
|
43
|
|
|
$userLogin->provider_name = $providerName; |
44
|
|
|
$userLogin->provider_uid = $userProfile->identifier; |
|
|
|
|
45
|
|
|
$userLogin->insert(); |
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('User_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
|
|
|
|
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.