1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package Social Login |
5
|
|
|
* @author Iurii Makukh <[email protected]> |
6
|
|
|
* @copyright Copyright (c) 2015, Iurii Makukh |
7
|
|
|
* @license https://www.gnu.org/licenses/gpl.html GNU/GPLv3 |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace gplcart\modules\social_login\handlers; |
11
|
|
|
|
12
|
|
|
use gplcart\core\Container; |
13
|
|
|
use OutOfRangeException; |
14
|
|
|
use UnexpectedValueException; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Base class for other Oauth 2.0 providers |
18
|
|
|
*/ |
19
|
|
|
class Provider |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Http model instance instance |
24
|
|
|
* @var \gplcart\core\models\Http $http |
25
|
|
|
*/ |
26
|
|
|
protected $http; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* User model instance |
30
|
|
|
* @var \gplcart\core\models\User $user |
31
|
|
|
*/ |
32
|
|
|
protected $user; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* User access model instance |
36
|
|
|
* @var \gplcart\core\models\UserAction $user_action |
37
|
|
|
*/ |
38
|
|
|
protected $user_action; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Store model instance |
42
|
|
|
* @var \gplcart\core\models\Store $store |
43
|
|
|
*/ |
44
|
|
|
protected $store; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Constructor |
48
|
|
|
*/ |
49
|
|
|
public function __construct() |
50
|
|
|
{ |
51
|
|
|
$this->http = Container::get('gplcart\\core\\models\\Http'); |
52
|
|
|
$this->user = Container::get('gplcart\\core\\models\\User'); |
53
|
|
|
$this->store = Container::get('gplcart\\core\\models\\Store'); |
54
|
|
|
$this->user_action = Container::get('gplcart\\core\\models\\UserAction'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Sets a property |
59
|
|
|
* @param string $property |
60
|
|
|
* @param mixed $value |
61
|
|
|
*/ |
62
|
|
|
public function setProperty($property, $value) |
63
|
|
|
{ |
64
|
|
|
$this->{$property} = $value; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Login/register a new user |
69
|
|
|
* @param array $user |
70
|
|
|
* @param array $provider |
71
|
|
|
* @return array |
72
|
|
|
* @throws OutOfRangeException |
73
|
|
|
*/ |
74
|
|
|
protected function submitUser(array $user, array $provider) |
75
|
|
|
{ |
76
|
|
|
if (empty($user['email'])) { |
77
|
|
|
throw new OutOfRangeException("Empty user ID in the submitting user data"); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$existing = $this->user->getByEmail($user['email']); |
81
|
|
|
|
82
|
|
|
if (empty($existing)) { |
83
|
|
|
return $this->registerUser($user, $provider); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $this->user_action->login($existing, false); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Register a new user |
91
|
|
|
* @param array $user |
92
|
|
|
* @param array $provider |
93
|
|
|
* @return array |
94
|
|
|
*/ |
95
|
|
|
protected function registerUser(array $user, array $provider) |
96
|
|
|
{ |
97
|
|
|
if (empty($provider['settings']['register'])) { |
98
|
|
|
return array(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$store = $this->store->get(); |
102
|
|
|
|
103
|
|
|
$user['store_id'] = $store['store_id']; |
104
|
|
|
$user['password'] = $this->user->generatePassword(); |
105
|
|
|
$user['login'] = !empty($provider['settings']['register_login']); |
106
|
|
|
$user['status'] = !empty($provider['settings']['register_status']); |
107
|
|
|
|
108
|
|
|
return $this->user_action->register($user); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Request user data |
113
|
|
|
* @param array $params |
114
|
|
|
* @param string $url |
115
|
|
|
* @param array $query |
116
|
|
|
* @return array |
117
|
|
|
* @throws UnexpectedValueException |
118
|
|
|
*/ |
119
|
|
|
protected function request(array $params, $url, $query = array()) |
120
|
|
|
{ |
121
|
|
|
if (!isset($query['access_token']) && isset($params['token']['access_token'])) { |
122
|
|
|
$query['access_token'] = $params['token']['access_token']; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$response = $this->http->request($url, array('query' => array_filter($query))); |
126
|
|
|
|
127
|
|
|
if ($response['status']['code'] != 200) { |
128
|
|
|
throw new UnexpectedValueException("Expected response code - 200, received - {$response['status']['code']}"); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$decoded = json_decode($response['data'], true); |
132
|
|
|
|
133
|
|
|
if (empty($decoded) || !is_array($decoded)) { |
134
|
|
|
throw new UnexpectedValueException('Failed to decode response data'); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return $decoded; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
} |
141
|
|
|
|