|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* HiPanel core package. |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://hipanel.com/ |
|
6
|
|
|
* @package hipanel-core |
|
7
|
|
|
* @license BSD-3-Clause |
|
8
|
|
|
* @copyright Copyright (c) 2014-2017, HiQDev (http://hiqdev.com/) |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace hipanel\components; |
|
12
|
|
|
|
|
13
|
|
|
use Yii; |
|
14
|
|
|
use yii\base\InvalidCallException; |
|
15
|
|
|
use yii\base\InvalidConfigException; |
|
16
|
|
|
|
|
17
|
|
|
class User extends \yii\web\User |
|
18
|
|
|
{ |
|
19
|
|
|
public $isGuestAllowed = false; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var string the seller login |
|
23
|
|
|
*/ |
|
24
|
|
|
public $seller; |
|
25
|
|
|
|
|
26
|
|
|
public function init() |
|
27
|
|
|
{ |
|
28
|
|
|
parent::init(); |
|
29
|
|
|
if (empty($this->seller)) { |
|
30
|
|
|
throw new InvalidConfigException('User "seller" must be set'); |
|
31
|
|
|
} |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function not($key) |
|
35
|
|
|
{ |
|
36
|
|
|
$identity = $this->getIdentity(); |
|
37
|
|
|
if (!$identity) { |
|
|
|
|
|
|
38
|
|
|
throw new InvalidCallException(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
return $identity->not($key); |
|
|
|
|
|
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function is($key) |
|
45
|
|
|
{ |
|
46
|
|
|
$identity = $this->getIdentity(); |
|
47
|
|
|
if (!$identity) { |
|
|
|
|
|
|
48
|
|
|
throw new InvalidCallException(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
return $identity->is($key); |
|
|
|
|
|
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* {@inheritdoc} |
|
56
|
|
|
* XXX fixes redirect loop when identity is set but the object is empty. |
|
57
|
|
|
* @return bool |
|
58
|
|
|
*/ |
|
59
|
|
|
public function getIsGuest() |
|
60
|
|
|
{ |
|
61
|
|
|
return empty($this->getIdentity()->id); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Prepares authorization data. |
|
66
|
|
|
* Redirects to authorization if necessary. |
|
67
|
|
|
* @return array |
|
68
|
|
|
*/ |
|
69
|
|
|
public function getAuthData() |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->isGuest ? $this->getGuestAuthData() : $this->getAuthorizedAuthData(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
protected function getGuestAuthData() |
|
75
|
|
|
{ |
|
76
|
|
|
if (!$this->isGuestAllowed) { |
|
77
|
|
|
$this->redirectLogin(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return [ |
|
81
|
|
|
'auth_ip' => Yii::$app->request->getUserIP(), |
|
82
|
|
|
'seller' => Yii::$app->params['user.seller'], |
|
83
|
|
|
]; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
protected function getAuthorizedAuthData() |
|
87
|
|
|
{ |
|
88
|
|
|
try { |
|
89
|
|
|
$token = $this->identity->getAccessToken(); |
|
|
|
|
|
|
90
|
|
|
} catch (\Exception $e) { |
|
91
|
|
|
$token = null; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
if (empty($token)) { |
|
95
|
|
|
/// logout() is very important here, else - redirect loop |
|
96
|
|
|
$this->logout(); |
|
97
|
|
|
$this->redirectLogin(); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return [ |
|
101
|
|
|
'access_token' => $token, |
|
102
|
|
|
'auth_ip' => Yii::$app->request->getUserIP(), |
|
103
|
|
|
]; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function redirectLogin() |
|
107
|
|
|
{ |
|
108
|
|
|
$this->loginRequired(); |
|
109
|
|
|
Yii::$app->end(); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
If an expression can have both
false, andnullas possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.