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