|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the 2amigos/yii2-usuario project. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) 2amigOS! <http://2amigos.us/> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view |
|
9
|
|
|
* the LICENSE file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Da\User\Widget; |
|
13
|
|
|
|
|
14
|
|
|
use Da\User\Model\SessionHistory; |
|
15
|
|
|
use Da\User\Traits\ContainerAwareTrait; |
|
16
|
|
|
use Yii; |
|
17
|
|
|
use yii\base\InvalidConfigException; |
|
18
|
|
|
use yii\base\InvalidParamException; |
|
19
|
|
|
use yii\base\Widget; |
|
20
|
|
|
use yii\helpers\ArrayHelper; |
|
21
|
|
|
|
|
22
|
|
|
class SessionStatusWidget extends Widget |
|
23
|
|
|
{ |
|
24
|
|
|
use ContainerAwareTrait; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var SessionHistory |
|
28
|
|
|
*/ |
|
29
|
|
|
public $model; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* {@inheritdoc} |
|
33
|
|
|
* |
|
34
|
|
|
* @throws InvalidConfigException |
|
35
|
|
|
*/ |
|
36
|
|
|
public function init() |
|
37
|
|
|
{ |
|
38
|
|
|
parent::init(); |
|
39
|
|
|
if (!$this->model instanceof SessionHistory) { |
|
40
|
|
|
throw new InvalidConfigException( |
|
41
|
|
|
__CLASS__ . '::$userId should be instanceof ' . SessionHistory::class |
|
42
|
|
|
); |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* {@inheritdoc} |
|
48
|
|
|
* |
|
49
|
|
|
* @throws InvalidParamException |
|
50
|
|
|
*/ |
|
51
|
|
|
public function run() |
|
52
|
|
|
{ |
|
53
|
|
|
if ($this->model->getIsActive()) { |
|
54
|
|
|
if ($this->model->session_id === Yii::$app->session->id) { |
|
55
|
|
|
$value = Yii::t('usuario', 'Current'); |
|
56
|
|
|
} else { |
|
57
|
|
|
$value = Yii::t('usuario', 'Active'); |
|
58
|
|
|
} |
|
59
|
|
|
} else { |
|
60
|
|
|
$value = Yii::t('usuario', 'Inactive'); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return $value; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Returns available auth items to be attached to the user. |
|
68
|
|
|
* |
|
69
|
|
|
* @param int|null type of auth items or null to return all |
|
70
|
|
|
* |
|
71
|
|
|
* @return array |
|
72
|
|
|
*/ |
|
73
|
|
|
protected function getAvailableItems($type = null) |
|
74
|
|
|
{ |
|
75
|
|
|
return ArrayHelper::map( |
|
76
|
|
|
$this->getAuthManager()->getItems($type), |
|
|
|
|
|
|
77
|
|
|
'name', |
|
78
|
|
|
function ($item) { |
|
79
|
|
|
return empty($item->description) |
|
80
|
|
|
? $item->name |
|
81
|
|
|
: $item->name . ' (' . $item->description . ')'; |
|
82
|
|
|
} |
|
83
|
|
|
); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: