Skip to content

Commit ba59a09

Browse files
committed
enable to restrict user to have either multiple or only one role. issue #98
1 parent ee3d7ce commit ba59a09

5 files changed

Lines changed: 90 additions & 83 deletions

File tree

UserManagementModule.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,17 @@ class UserManagementModule extends \yii\base\Module
112112
*/
113113
public $registrationBlackRegexp = '/^(.)*admin(.)*$/i';
114114

115+
/**
116+
* Affects only web interface in "/user-management/user-permission/set" route. Tt means you still can assign
117+
* multiple roles (for example via migrations) even if this attribute is "false"
118+
*
119+
* If true there will be checkbox list and user can have multiple roles.
120+
* Otherwise there will be radio list and only 1 role can be assigned to user.
121+
*
122+
* @var bool
123+
*/
124+
public $userCanHaveMultipleRoles = true;
125+
115126
/**
116127
* How much attempts user can made to login or recover password in $attemptsTimeout seconds interval
117128
*

controllers/UserPermissionController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function actionSetRoles($id)
6060
$oldAssignments = array_keys(Role::getUserRoles($id));
6161

6262
// To be sure that user didn't attempt to assign himself some unavailable roles
63-
$newAssignments = array_intersect(Role::getAvailableRoles(Yii::$app->user->isSuperAdmin, true), Yii::$app->request->post('roles', []));
63+
$newAssignments = array_intersect(Role::getAvailableRoles(Yii::$app->user->isSuperAdmin, true), (array)Yii::$app->request->post('roles', []));
6464

6565
$toAssign = array_diff($newAssignments, $oldAssignments);
6666
$toRevoke = array_diff($oldAssignments, $newAssignments);

views/permission/view.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,21 @@
5151
<fieldset>
5252
<legend><?= $groupName ?></legend>
5353

54-
<?= Html::checkboxList(
55-
'child_permissions',
56-
ArrayHelper::map($childPermissions, 'name', 'name'),
57-
ArrayHelper::map($permissions, 'name', 'description'),
58-
['separator'=>'<br>']
59-
) ?>
54+
<?php foreach ($permissions as $permission): ?>
55+
<label>
56+
<?php $isChecked = in_array($permission->name, ArrayHelper::map($childPermissions, 'name', 'name')) ? 'checked' : '' ?>
57+
<input type="checkbox" <?= $isChecked ?> name="child_permissions[]" value="<?= $permission->name ?>">
58+
<?= $permission->description ?>
59+
</label>
60+
61+
<?= GhostHtml::a(
62+
'<span class="glyphicon glyphicon-edit"></span>',
63+
['view', 'id'=>$permission->name],
64+
['target'=>'_blank']
65+
) ?>
66+
<br/>
67+
<?php endforeach ?>
68+
6069
</fieldset>
6170
<br/>
6271
</div>

views/role/view.php

Lines changed: 30 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -45,40 +45,21 @@
4545
<div class="panel-body">
4646
<?= Html::beginForm(['set-child-roles', 'id'=>$role->name]) ?>
4747

48-
<?= Html::checkboxList(
49-
'child_roles',
50-
ArrayHelper::map($childRoles, 'name', 'name'),
51-
ArrayHelper::map($allRoles, 'name', 'description'),
52-
[
53-
'item'=>function ($index, $label, $name, $checked, $value) {
54-
$list = '<ul style="padding-left: 10px">';
55-
foreach (Role::getPermissionsByRole($value) as $permissionName => $permissionDescription)
56-
{
57-
$list .= $permissionDescription ? "<li>{$permissionDescription}</li>" : "<li>{$permissionName}</li>";
58-
}
59-
$list .= '</ul>';
60-
61-
$helpIcon = Html::beginTag('span', [
62-
'title' => UserManagementModule::t('back', 'Permissions for role - "{role}"',[
63-
'role'=>$label,
64-
]),
65-
'data-content' => $list,
66-
'data-html' => 'true',
67-
'role' => 'button',
68-
'style' => 'margin-bottom: 5px; padding: 0 5px',
69-
'class' => 'btn btn-sm btn-default role-help-btn',
70-
]);
71-
$helpIcon .= '?';
72-
$helpIcon .= Html::endTag('span');
73-
74-
$isChecked = $checked ? 'checked' : '';
75-
$checkbox = "<label><input type='checkbox' name='{$name}' value='{$value}' {$isChecked}> {$label}</label>";
76-
77-
return $helpIcon . ' ' . $checkbox;
78-
},
79-
'separator'=>'<br>'
80-
]
81-
) ?>
48+
<?php foreach ($allRoles as $aRole): ?>
49+
<label>
50+
<?php $isChecked = in_array($aRole['name'], ArrayHelper::map($childRoles, 'name', 'name')) ? 'checked' : '' ?>
51+
<input type="checkbox" <?= $isChecked ?> name="child_roles[]" value="<?= $aRole['name'] ?>">
52+
<?= $aRole['description'] ?>
53+
</label>
54+
55+
<?= GhostHtml::a(
56+
'<span class="glyphicon glyphicon-edit"></span>',
57+
['/user-management/role/view', 'id'=>$aRole['name']],
58+
['target'=>'_blank']
59+
) ?>
60+
<br/>
61+
<?php endforeach ?>
62+
8263

8364
<hr/>
8465
<?= Html::submitButton(
@@ -107,12 +88,21 @@
10788
<fieldset>
10889
<legend><?= $groupName ?></legend>
10990

110-
<?= Html::checkboxList(
111-
'child_permissions',
112-
ArrayHelper::map($currentPermissions, 'name', 'name'),
113-
ArrayHelper::map($permissions, 'name', 'description'),
114-
['separator'=>'<br>']
115-
) ?>
91+
<?php foreach ($permissions as $permission): ?>
92+
<label>
93+
<?php $isChecked = in_array($permission->name, ArrayHelper::map($currentPermissions, 'name', 'name')) ? 'checked' : '' ?>
94+
<input type="checkbox" <?= $isChecked ?> name="child_permissions[]" value="<?= $permission->name ?>">
95+
<?= $permission->description ?>
96+
</label>
97+
98+
<?= GhostHtml::a(
99+
'<span class="glyphicon glyphicon-edit"></span>',
100+
['/user-management/permission/view', 'id'=>$permission->name],
101+
['target'=>'_blank']
102+
) ?>
103+
<br/>
104+
<?php endforeach ?>
105+
116106
</fieldset>
117107
<br/>
118108
</div>

views/user-permission/set.php

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
* @var webvimark\modules\UserManagement\models\User $user
66
*/
77

8+
use webvimark\modules\UserManagement\components\GhostHtml;
89
use webvimark\modules\UserManagement\models\rbacDB\Role;
910
use webvimark\modules\UserManagement\UserManagementModule;
1011
use yii\bootstrap\BootstrapPluginAsset;
1112
use yii\helpers\ArrayHelper;
12-
use webvimark\modules\UserManagement\models\rbacDB\Permission;
1313
use yii\helpers\Html;
1414

1515
BootstrapPluginAsset::register($this);
@@ -39,40 +39,29 @@
3939

4040
<?= Html::beginForm(['set-roles', 'id'=>$user->id]) ?>
4141

42-
<?= Html::checkboxList(
43-
'roles',
44-
ArrayHelper::map(Role::getUserRoles($user->id), 'name', 'name'),
45-
ArrayHelper::map(Role::getAvailableRoles(), 'name', 'description'),
46-
[
47-
'item'=>function ($index, $label, $name, $checked, $value) {
48-
$list = '<ul style="padding-left: 10px">';
49-
foreach (Role::getPermissionsByRole($value) as $permissionName => $permissionDescription)
50-
{
51-
$list .= $permissionDescription ? "<li>{$permissionDescription}</li>" : "<li>{$permissionName}</li>";
52-
}
53-
$list .= '</ul>';
54-
55-
$helpIcon = Html::beginTag('span', [
56-
'title' => UserManagementModule::t('back', 'Permissions for role - "{role}"',[
57-
'role'=>$label,
58-
]),
59-
'data-content' => $list,
60-
'data-html' => 'true',
61-
'role' => 'button',
62-
'style' => 'margin-bottom: 5px; padding: 0 5px',
63-
'class' => 'btn btn-sm btn-default role-help-btn',
64-
]);
65-
$helpIcon .= '?';
66-
$helpIcon .= Html::endTag('span');
67-
68-
$isChecked = $checked ? 'checked' : '';
69-
$checkbox = "<label><input type='checkbox' name='{$name}' value='{$value}' {$isChecked}> {$label}</label>";
70-
71-
return $helpIcon . ' ' . $checkbox;
72-
},
73-
'separator'=>'<br>',
74-
]
75-
) ?>
42+
<?php foreach (Role::getAvailableRoles() as $aRole): ?>
43+
<label>
44+
<?php $isChecked = in_array($aRole['name'], ArrayHelper::map(Role::getUserRoles($user->id), 'name', 'name')) ? 'checked' : '' ?>
45+
46+
<?php if ( Yii::$app->getModule('user-management')->userCanHaveMultipleRoles ): ?>
47+
<input type="checkbox" <?= $isChecked ?> name="roles[]" value="<?= $aRole['name'] ?>">
48+
49+
<?php else: ?>
50+
<input type="radio" <?= $isChecked ?> name="roles" value="<?= $aRole['name'] ?>">
51+
52+
<?php endif; ?>
53+
54+
<?= $aRole['description'] ?>
55+
</label>
56+
57+
<?= GhostHtml::a(
58+
'<span class="glyphicon glyphicon-edit"></span>',
59+
['/user-management/role/view', 'id'=>$aRole['name']],
60+
['target'=>'_blank']
61+
) ?>
62+
<br/>
63+
<?php endforeach ?>
64+
7665
<br/>
7766

7867
<?php if ( Yii::$app->user->isSuperadmin OR Yii::$app->user->id != $user->id ): ?>
@@ -111,7 +100,15 @@
111100

112101
<ul>
113102
<?php foreach ($permissions as $permission): ?>
114-
<li><?= $permission->description ?></li>
103+
<li>
104+
<?= $permission->description ?>
105+
106+
<?= GhostHtml::a(
107+
'<span class="glyphicon glyphicon-edit"></span>',
108+
['/user-management/permission/view', 'id'=>$permission->name],
109+
['target'=>'_blank']
110+
) ?>
111+
</li>
115112
<?php endforeach ?>
116113
</ul>
117114
</fieldset>

0 commit comments

Comments
 (0)