1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace hiapi\models; |
4
|
|
|
|
5
|
|
|
use hiqdev\yii\DataMapper\query\Specification; |
6
|
|
|
use hiqdev\yii\DataMapper\repositories\EntityNotFoundException; |
7
|
|
|
use yii\base\Model; |
8
|
|
|
use yii\db\Expression; |
9
|
|
|
use yii\db\Query; |
10
|
|
|
use yii\helpers\Json; |
11
|
|
|
use yii\web\IdentityInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* User identity model working with HIAM |
15
|
|
|
* |
16
|
|
|
*/ |
17
|
|
|
class HiamUserIdentity extends Model implements IdentityInterface |
18
|
|
|
{ |
19
|
|
|
public $id; |
20
|
|
|
public $login; |
21
|
|
|
|
22
|
|
|
public $type; |
23
|
|
|
public $state; |
24
|
|
|
|
25
|
|
|
public $roles = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* {@inheritdoc} |
29
|
|
|
*/ |
30
|
|
|
public function rules() |
31
|
|
|
{ |
32
|
|
|
return [ |
33
|
|
|
['id', 'integer'], |
34
|
|
|
['login', 'string'], |
35
|
|
|
['type', 'string'], |
36
|
|
|
['state', 'string'], |
37
|
|
|
['roles', 'trim'], |
38
|
|
|
]; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** {@inheritdoc} */ |
42
|
|
|
public static function findIdentity($id) |
43
|
|
|
{ |
44
|
|
|
throw new \yii\base\InvalidCallException('no `findIdentity` and not expected'); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
|
|
public static function findIdentityByAccessToken($token, $type = null) |
51
|
|
|
{ |
52
|
|
|
$res = self::requestHiam('oauth/resource', ['access_token' => $token]); |
53
|
|
|
if (empty($res['id'])) { |
54
|
|
|
return null; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return new static([ |
58
|
|
|
'id' => $res['id'], |
59
|
|
|
'login' => $res['login'], |
60
|
|
|
'roles' => $res['roles'], |
61
|
|
|
'type' => $res['type'], |
62
|
|
|
'state' => $res['state'], |
63
|
|
|
]); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public static function requestHiam($path, $data) |
67
|
|
|
{ |
68
|
|
|
$scheme = 'https'; |
69
|
|
|
$host = $this->getApp()->params['hiam.site']; |
|
|
|
|
70
|
|
|
$query = http_build_query($data); |
71
|
|
|
$url = "$scheme://$host/$path?$query"; |
72
|
|
|
|
73
|
|
|
$ch = curl_init(); |
74
|
|
|
curl_setopt($ch, CURLOPT_URL, $url); |
75
|
|
|
curl_setopt($ch, CURLOPT_HEADER, false); |
76
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
77
|
|
|
$res = curl_exec($ch); |
78
|
|
|
curl_close($ch); |
79
|
|
|
|
80
|
|
|
return Json::decode($res); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
*/ |
86
|
|
|
public function getId() |
87
|
|
|
{ |
88
|
|
|
return $this->id; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritdoc} |
93
|
|
|
*/ |
94
|
|
|
public function getAuthKey() |
95
|
|
|
{ |
96
|
|
|
return null; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* {@inheritdoc} |
101
|
|
|
*/ |
102
|
|
|
public function validateAuthKey($authKey) |
103
|
|
|
{ |
104
|
|
|
return false; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
protected function getApp() |
108
|
|
|
{ |
109
|
|
|
return class_exists('Yii') ? \Yii::$app : \yii\helpers\Yii::getApp(); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.