|
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\Assignment; |
|
15
|
|
|
use Da\User\Service\UpdateAuthAssignmentsService; |
|
16
|
|
|
use Da\User\Traits\AuthManagerAwareTrait; |
|
17
|
|
|
use Da\User\Traits\ContainerAwareTrait; |
|
18
|
|
|
use Yii; |
|
19
|
|
|
use yii\base\InvalidConfigException; |
|
20
|
|
|
use yii\base\InvalidParamException; |
|
21
|
|
|
use yii\base\Widget; |
|
22
|
|
|
use yii\helpers\ArrayHelper; |
|
23
|
|
|
use yii\rbac\Item; |
|
24
|
|
|
|
|
25
|
|
|
class AssignmentsWidget extends Widget |
|
26
|
|
|
{ |
|
27
|
|
|
use AuthManagerAwareTrait; |
|
28
|
|
|
use ContainerAwareTrait; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var int ID of the user to whom auth items will be assigned |
|
32
|
|
|
*/ |
|
33
|
|
|
public $userId; |
|
34
|
|
|
/** |
|
35
|
|
|
* @var string[] the post parameters |
|
36
|
|
|
*/ |
|
37
|
|
|
public $params = []; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* {@inheritdoc} |
|
41
|
|
|
* |
|
42
|
|
|
* @throws InvalidConfigException |
|
43
|
|
|
*/ |
|
44
|
|
|
public function init() |
|
45
|
|
|
{ |
|
46
|
|
|
parent::init(); |
|
47
|
|
|
if ($this->userId === null) { |
|
48
|
|
|
throw new InvalidConfigException(__CLASS__ . '::$userId is required'); |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* {@inheritdoc} |
|
54
|
|
|
* |
|
55
|
|
|
* @throws InvalidParamException |
|
56
|
|
|
* @throws InvalidConfigException |
|
57
|
|
|
*/ |
|
58
|
|
|
public function run() |
|
59
|
|
|
{ |
|
60
|
|
|
$model = $this->make(Assignment::class, [], ['user_id' => $this->userId]); |
|
61
|
|
|
|
|
62
|
|
|
if ($model->load($this->params)) { |
|
63
|
|
|
$this->make(UpdateAuthAssignmentsService::class, [$model])->run(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$items[Yii::t('usuario', 'Roles')] = $this->getAvailableItems(Item::TYPE_ROLE); |
|
|
|
|
|
|
67
|
|
|
if (!Yii::$app->getModule('user')->restrictUserPermissionAssignment) { |
|
68
|
|
|
$items[Yii::t('usuario', 'Permissions')] = $this->getAvailableItems(Item::TYPE_PERMISSION); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return $this->render( |
|
72
|
|
|
'/widgets/assignments/form', |
|
73
|
|
|
[ |
|
74
|
|
|
'model' => $model, |
|
75
|
|
|
'availableItems' => $items, |
|
76
|
|
|
] |
|
77
|
|
|
); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Returns available auth items to be attached to the user. |
|
82
|
|
|
* |
|
83
|
|
|
* @param int|null type of auth items or null to return all |
|
84
|
|
|
* @param null|mixed $type |
|
85
|
|
|
* |
|
86
|
|
|
* @return array |
|
87
|
|
|
*/ |
|
88
|
|
|
protected function getAvailableItems($type = null) |
|
89
|
|
|
{ |
|
90
|
|
|
return ArrayHelper::map( |
|
91
|
|
|
$this->getAuthManager()->getItems($type), |
|
92
|
|
|
'name', |
|
93
|
|
|
function ($item) { |
|
94
|
|
|
return empty($item->description) |
|
95
|
|
|
? $item->name |
|
96
|
|
|
: $item->name . ' (' . $item->description . ')'; |
|
97
|
|
|
} |
|
98
|
|
|
); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.