|
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
|
|
|
use yii\base\InvalidCallException; |
|
16
|
|
|
use yii\base\InvalidConfigException; |
|
17
|
|
|
|
|
18
|
|
|
class User extends \yii\web\User |
|
19
|
|
|
{ |
|
20
|
|
|
public $isGuestAllowed = false; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var string the seller login |
|
24
|
|
|
*/ |
|
25
|
|
|
public $seller; |
|
26
|
|
|
|
|
27
|
|
|
public function init() |
|
28
|
|
|
{ |
|
29
|
|
|
parent::init(); |
|
30
|
|
|
if (empty($this->seller)) { |
|
31
|
|
|
throw new InvalidConfigException('User "seller" must be set'); |
|
32
|
|
|
} |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function not($key) |
|
36
|
|
|
{ |
|
37
|
|
|
$identity = $this->getIdentity(); |
|
38
|
|
|
if (!$identity) { |
|
|
|
|
|
|
39
|
|
|
throw new InvalidCallException(); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
return $identity->not($key); |
|
|
|
|
|
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function is($key) |
|
46
|
|
|
{ |
|
47
|
|
|
$identity = $this->getIdentity(); |
|
48
|
|
|
if (!$identity) { |
|
|
|
|
|
|
49
|
|
|
throw new InvalidCallException(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return $identity->is($key); |
|
|
|
|
|
|
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
|
|
|
if ($this->isGuest) { |
|
72
|
|
|
if ($this->isGuestAllowed) { |
|
73
|
|
|
return []; |
|
74
|
|
|
} else { |
|
75
|
|
|
Yii::$app->response->redirect('/site/login'); |
|
76
|
|
|
Yii::$app->end(); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$token = $this->identity->getAccessToken(); |
|
|
|
|
|
|
81
|
|
|
if (empty($token)) { |
|
82
|
|
|
/// this is very important line |
|
83
|
|
|
/// without this line - redirect loop |
|
84
|
|
|
$this->logout(); |
|
85
|
|
|
|
|
86
|
|
|
Yii::$app->response->redirect('/site/login'); |
|
87
|
|
|
Yii::$app->end(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return ['access_token' => $token]; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
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.