|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* HiPanel core package |
|
5
|
|
|
* |
|
6
|
|
|
* @link https://hipanel.com/ |
|
7
|
|
|
* @package hipanel-core |
|
8
|
|
|
* @license BSD-3-Clause |
|
9
|
|
|
* @copyright Copyright (c) 2014-2016, HiQDev (http://hiqdev.com/) |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace hipanel\components; |
|
13
|
|
|
|
|
14
|
|
|
use Yii; |
|
15
|
|
|
|
|
16
|
|
|
class Connection extends \hiqdev\hiart\Connection |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @param mixed $response The response |
|
20
|
|
|
* @return null|string |
|
21
|
|
|
* - string: the error text |
|
22
|
|
|
* - null: the response is not an error |
|
23
|
|
|
*/ |
|
24
|
|
|
public function checkError($response) |
|
25
|
|
|
{ |
|
26
|
|
|
if ($response !== '0' && $this->isError($response)) { |
|
27
|
|
|
$error = $this->getError($response); |
|
28
|
|
|
if (empty($error)) { |
|
29
|
|
|
return 'unknown api error'; |
|
30
|
|
|
} elseif ($error === 'invalid_token') { |
|
31
|
|
|
Yii::$app->user->logout(); |
|
32
|
|
|
Yii::$app->response->refresh()->send(); |
|
33
|
|
|
Yii::$app->end(); |
|
34
|
|
|
} else { |
|
35
|
|
|
return $error; |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
return null; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
private function isError($data) |
|
43
|
|
|
{ |
|
44
|
|
|
return is_array($data) ? array_key_exists('_error', $data) : !$data; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
private function getError($data) |
|
48
|
|
|
{ |
|
49
|
|
|
return isset($data['_error']) ? $data['_error'] : null; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Prepares authorization data. |
|
54
|
|
|
* If user is not authorized redirects to authorization. |
|
55
|
|
|
* @return array |
|
56
|
|
|
*/ |
|
57
|
|
|
public function getAuth() |
|
58
|
|
|
{ |
|
59
|
|
|
if ($this->_disabledAuth) { |
|
60
|
|
|
return []; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$user = Yii::$app->user; |
|
64
|
|
|
|
|
65
|
|
|
$identity = $user->identity; |
|
66
|
|
|
if ($identity === null) { |
|
67
|
|
|
Yii::$app->response->redirect('/site/login'); |
|
68
|
|
|
Yii::$app->end(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$token = $identity->getAccessToken(); |
|
72
|
|
|
if (!$token && $user->loginRequired() !== null) { |
|
73
|
|
|
Yii::$app->response->redirect('/site/logout'); |
|
74
|
|
|
Yii::$app->end(); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return ['access_token' => $token]; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|