Skip to content

Commit

Permalink
update backend user and role create update
Browse files Browse the repository at this point in the history
  • Loading branch information
liufee committed Nov 10, 2017
1 parent 7db8cae commit c62b442
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 84 deletions.
12 changes: 7 additions & 5 deletions backend/controllers/AdminUserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,13 @@ public function actionUpdate($id)
{
$model = User::findOne($id);
$model->setScenario('update');
$model->roles = $model->permissions = array_keys( yii::$app->getAuthManager()->getAssignments($id) );
if( in_array($id, yii::$app->getBehavior('access')->superAdminUserIds) ){
$model->permissions = array_keys( yii::$app->getAuthManager()->getPermissions() );
$model->roles = array_keys( yii::$app->getAuthManager()->getRoles() );
}
$model->roles = $model->permissions = call_user_func(function() use($id){
$permissions = yii::$app->getAuthManager()->getAssignments($id);
foreach ($permissions as $k => &$v){
$v = $k;
}
return $permissions;
});
if (Yii::$app->getRequest()->getIsPost()) {
if ($model->load(Yii::$app->request->post()) && $model->save() && $model->assignPermission() ) {
Yii::$app->getSession()->setFlash('success', yii::t('app', 'Success'));
Expand Down
2 changes: 1 addition & 1 deletion backend/controllers/RbacController.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public function actionRoleCreate()
public function actionRoleUpdate($name)
{
$model = new Rbac(['scenario'=>'role']);
$model->fillModel($name);
$model->fillModel($name);//var_dump($model->roles);exit;
if( yii::$app->getRequest()->getIsPost() ) {
if ($model->load(yii::$app->getRequest()->post()) && $model->validate() && $model->updateRole($name)) {
yii::$app->getSession()->setFlash('success', yii::t('app', 'Success'));
Expand Down
13 changes: 7 additions & 6 deletions backend/models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,9 @@ public function beforeSave($insert)
public function assignPermission()
{
$authManager = yii::$app->getAuthManager();
if(!$this->getIsNewRecord() && in_array($this->id, yii::$app->getBehavior('access')->superAdminUserIds)){
$this->permissions = $this->roles = [];
}
$assignments = $authManager->getAssignments($this->id);
$roles = $permissions = [];
foreach ($assignments as $key => $assignment){
Expand Down Expand Up @@ -335,15 +338,13 @@ public function assignPermission()
}

//权限permission
if( $this->permissions === null ){
$this->permissions = [];
}else if( !is_array( $this->permissions ) ) {
$this->permissions = explode(',', $this->permissions);
}
$this->permissions = array_flip($this->permissions);
if (isset($this->permissions[0])) unset($this->permissions[0]);
$this->permissions = array_flip($this->permissions);

$needAdds = array_diff($this->permissions, $permissions);
$needRemoves = array_diff($permissions, $this->permissions);
if( !empty($needAdds) && !empty($needAdds[0]) ) {
if( !empty($needAdds) ) {
$str .= ' 增加了权限: ';
foreach ($needAdds as $permission) {
$permissionItem = $authManager->getPermission($permission);
Expand Down
36 changes: 7 additions & 29 deletions backend/models/form/Rbac.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function rules()
],

[['name', 'description'], 'required', 'on' => 'role'],
[['roles', 'permissions'], 'default', 'value'=>[], 'on' => 'role'],
[['roles', 'permissions'], 'default', 'on' => 'role'],
];
}

Expand Down Expand Up @@ -156,9 +156,6 @@ public function deletePermission()

public function createRole()
{
if( !is_array($this->permissions) ) $this->permissions = explode(',', $this->permissions);
if( !is_array($this->roles) ) $this->permissions = explode(',', $this->roles);

$authManager = yii::$app->getAuthManager();
if ($authManager->getRole($this->name) !== null) {
$this->addError('name', yii::t('app', 'Role exists'));
Expand All @@ -170,16 +167,14 @@ public function createRole()
'sort' => $this->sort,
]);
if( $authManager->add($role) ){
$this->permissions = array_flip($this->permissions);
if (isset($this->permissions[0])) unset($this->permissions[0]);
$this->permissions = array_flip($this->permissions);
foreach ($this->permissions as $permission){
$permission = $authManager->getPermission($permission);
$authManager->addChild($role, $permission);
}

foreach ($this->roles as $r){
$r = $authManager->getRole($r);
$authManager->addChild($role, $r);
}

Event::trigger(CustomLog::className(), CustomLog::EVENT_AFTER_CREATE, new CustomLog([
'sender' => $this,
]));
Expand All @@ -193,9 +188,6 @@ public function updateRole($name)
$oldModel = clone $this;
$oldModel->fillModel($name);

if( !is_array($this->permissions) ) $this->permissions = explode(',', $this->permissions);
if( !is_array($this->roles) ) $this->permissions = explode(',', $this->roles);

$authManager = yii::$app->getAuthManager();
$role = $authManager->getRole($name);
if( $role->name != $this->name ){//修改角色名称
Expand All @@ -211,12 +203,11 @@ public function updateRole($name)
]);

$oldPermissions = array_keys( $authManager->getPermissionsByRole($name) );
$oldChildRoles = array_keys( $authManager->getChildRoles($name) );
$oldChildRoles = array_flip($oldChildRoles);
unset($oldChildRoles[$name]);
$oldChildRoles = array_flip($oldChildRoles);

if( $authManager->update($name, $role) ){
$this->permissions = array_flip($this->permissions);
if (isset($this->permissions[0])) unset($this->permissions[0]);
$this->permissions = array_flip($this->permissions);

$needAdds = array_diff($this->permissions, $oldPermissions);
foreach ($needAdds as $permission){
Expand All @@ -230,19 +221,6 @@ public function updateRole($name)
$authManager->removeChild($role, $permission);
}

$needAdds = array_diff($this->roles, $oldChildRoles);

foreach ($needAdds as $r){
$r = $authManager->getRole($r);
$authManager->addChild($role, $r);
}

$needRemoves = array_diff($oldChildRoles, $this->roles);
foreach ($needRemoves as $r){
$r = $authManager->getRole($r);
$authManager->removeChild($role, $r);
}

Event::trigger(CustomLog::className(), CustomLog::EVENT_CUSTOM, new CustomLog([
'sender' => $this,
'old' => $oldModel,
Expand Down
77 changes: 59 additions & 18 deletions backend/views/admin-user/_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
* @var $model backend\models\User
*/

use backend\models\form\Rbac;
use backend\widgets\ActiveForm;
use backend\models\User;
use common\widgets\JsBlock;
use backend\models\form\Rbac;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;

$this->title = "Admin";
?>
Expand Down Expand Up @@ -54,28 +54,29 @@
$itemsOptions = [];
if(in_array( $model->getId(), yii::$app->getBehavior('access')->superAdminUserIds)){
$itemsOptions = ['disabled'=>'true'];
}//var_dump($itemsOptions);exit;
}
?>
<?= $form->field($model, 'roles', [
'labelOptions' => [
'label' => yii::t('app', 'Roles'),
]
])->checkboxList($temp, ['itemOptions'=>$itemsOptions]) ?>
<div class="hr-line-dashed"></div>
<div class="form-group">
<label class="col-sm-2 control-label"> <?=yii::t('app', 'Permissions')?></label>
<div class="form-group field-permissions">
<span class="col-sm-2 control-label checkbox checkbox-success"><?= Html::checkbox("", false, ['id'=>'permission-all','class'=>'chooseAll'])?><label for='permission-all'><h4><?=yii::t('app', 'Permissions')?></h4></label></span>
<div class="col-sm-10">
<?php
$itemsOptions = [];
if(in_array($model->getId(), yii::$app->getBehavior('access')->superAdminUserIds)){
$itemsOptions = ['disabled'=>'true'];
}
$rbac = new Rbac();
foreach ($rbac->getPermissionsByGroup('form') as $key => $value){
echo "<div class='col-sm-1 text-center'><h2>{$key}</h2></div>";
echo "<div class='col-sm-1 text-left'><span class='checkbox checkbox-success checkbox-inline'>" . Html::checkbox("", false, ['id'=>"permission-all-{$key}", 'class'=>'chooseAll']) . "<label for='permission-all-{$key}'><h4>{$key}</h4></label></span></div>";
echo "<div class='col-sm-11'>";
foreach ($value as $k => $val){
echo $form->field($model, 'permissions', ['labelOptions'=>['class'=>'col-sm-1']])->label($k)->checkboxList(ArrayHelper::map($val, 'name', 'description'), ['itemOptions'=>$itemsOptions]);
echo "<div class='col-sm-1 text-left'><span class='checkbox checkbox-success checkbox-inline'>" . Html::checkbox("", false, ['id'=>"permission-all-{$k}", 'class'=>'chooseAll']) . "<label for='permission-all-{$k}'><h5>{$k}</h5></label></span></div>";
echo "<div class='col-sm-11'>";
foreach ($val as $v) {
echo $form->field($model, "permissions[{$v['name']}]", ['options'=>['style'=>'display:inline'], 'labelOptions'=>['class'=>'col-sm-12 control-label']])->checkbox(['value'=>$v['name']])->label($v['description']);
}
echo "</div><div class='col-sm-12' style='height: 20px'></div>";
}
echo "</div><div class='col-sm-12' style='height: 20px'></div>";
}
Expand All @@ -90,16 +91,56 @@
</div>
</div>
</div>

<?php JsBlock::begin()?>
<script>
$("form").bind("beforeSubmit", function () {
var permissions = [];
$("div.field-user-permissions input[type=checkbox]:checked").each(function(){
permissions.push($(this).val());
$(document).ready(function () {
var isSuperAdmin = <?php if(in_array($model->getId(), yii::$app->getBehavior('access')->superAdminUserIds)){echo 1;}else{echo 0;}?>;
if( isSuperAdmin ){
var forbiddens = $(".field-permissions, .field-user-roles").find("input[type=checkbox]");
forbiddens.each(function(){
$(this).attr('disabled', true).attr('checked', true);
})
}else{
var chooseAll = $(".col-sm-11 .col-sm-1 .chooseAll");
var middle = $(".col-sm-1 .chooseAll");
var top = $("label .chooseAll");
for (var i = 0; i < middle.length; i++) {
chooseAll.push(middle[i]);
}
for (var i = 0; i < top.length; i++) {
chooseAll.push(top[i]);
}
chooseAll.each(function () {
var that = $(this);
if (that.attr('id') == 'permission-all') {
var checkboxs = $(this).parents("span").next().find("input[type=checkbox]");
} else {
var checkboxs = $(this).parents(".col-sm-1").next().find("input[type=checkbox]");
}
var atLeastOneUnchecked = false;
checkboxs.each(function () {
if ($(this).is(":checked") == false) {
atLeastOneUnchecked = true;
}
})
if (atLeastOneUnchecked == false && that.is(":checked") == false) {
that.trigger('click');
}
});
}

});
$(this).append("<input name='User[permissions]' value='" + permissions + "'>")
$(".chooseAll").change(function () {
var type = $(this).is(':checked');
var checkboxs = $(this).parents("span").next().find("input[type=checkbox]");
if( checkboxs.length == 0 ) {
checkboxs = $(this).parents(".col-sm-1").next().find("input[type=checkbox]");
}
checkboxs.each(function () {
if(type != $(this).is(':checked')){
$(this).trigger('click');
}
})
})
})
</script>
<?php JsBlock::end()?>
76 changes: 51 additions & 25 deletions backend/views/rbac/_role-form.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@

use backend\widgets\ActiveForm;
use common\widgets\JsBlock;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;

$this->title = "Roles";

?>
<style>

</style>
<div class="row">
<div class="col-sm-12">
<div class="ibox">
Expand All @@ -31,14 +34,19 @@
<?= $form->field($model, 'sort')->textInput() ?>
<div class="hr-line-dashed"></div>
<div class="form-group">
<label class="col-sm-2 control-label"> <?=yii::t('app', 'Permissions')?></label>
<span class="col-sm-2 control-label checkbox checkbox-success"><?= Html::checkbox("", false, ['id'=>'permission-all','class'=>'chooseAll'])?><label for='permission-all'><h4><?=yii::t('app', 'Permissions')?></h4></label></span>
<div class="col-sm-10">
<?php
foreach ($model->getPermissionsByGroup('form') as $key => $value){
echo "<div class='col-sm-1 text-center'><h2>{$key}</h2></div>";
echo "<div class='col-sm-1 text-left'><span class='checkbox checkbox-success checkbox-inline'>" . Html::checkbox("", false, ['id'=>"permission-all-{$key}", 'class'=>'chooseAll']) . "<label for='permission-all-{$key}'><h4>{$key}</h4></label></span></div>";
echo "<div class='col-sm-11'>";
foreach ($value as $k => $val){
echo $form->field($model, 'permissions', ['labelOptions'=>['class'=>'col-sm-1']])->label($k)->checkboxList(ArrayHelper::map($val, 'name', 'description'));
echo "<div class='col-sm-1 text-left'><span class='checkbox checkbox-success checkbox-inline'>" . Html::checkbox("", false, ['id'=>"permission-all-{$k}", 'class'=>'chooseAll']) . "<label for='permission-all-{$k}'><h5>{$k}</h5></label></span></div>";
echo "<div class='col-sm-11'>";
foreach ($val as $v) {
echo $form->field($model, "permissions[{$v['name']}]", ['options'=>['style'=>'display:inline'], 'labelOptions'=>['class'=>'col-sm-12 control-label']])->checkbox(['value'=>$v['name']])->label($v['description']);
}
echo "</div><div class='col-sm-12' style='height: 20px'></div>";
}
echo "</div><div class='col-sm-12' style='height: 20px'></div>";
}
Expand All @@ -47,36 +55,54 @@
</div>
</div>
<div class="hr-line-dashed"></div>
<?php
$roles = yii::$app->getAuthManager()->getRoles();
$curChainRoles = [];
if( $model->name != '' ) {
$curChainRoles = array_keys(yii::$app->getAuthManager()->getChildRoles($model->name));
}
$temp = [];
foreach ($roles as $role){
if( in_array($role->name, $curChainRoles) ) continue;
$temp[$role->name] = $role->name;
}
?>
<?= $form->field($model, 'roles')->label(yii::t('app', 'Roles'))->checkboxList($temp) ?>
<div class="hr-line-dashed"></div>
<?= $form->defaultButtons() ?>
<?php ActiveForm::end(); ?>
</div>
</div>
</div>
</div>

<?php JsBlock::begin()?>
<script>
$("form").bind("beforeSubmit", function () {
var permissions = [];
$("div.field-rbac-permissions input[type=checkbox]:checked").each(function(){
permissions.push($(this).val());

$(document).ready(function () {
var chooseAll = $(".col-sm-11 .col-sm-1 .chooseAll");
var middle = $(".col-sm-1 .chooseAll");
var top = $("label .chooseAll");
for( var i=0; i<middle.length; i++ ){
chooseAll.push( middle[i] );
}
for( var i=0; i<top.length; i++ ){
chooseAll.push( top[i] );
}
chooseAll.each(function(){
var that = $(this);
if( that.attr('id') == 'permission-all' ) {
var checkboxs = $(this).parents("span").next().find("input[type=checkbox]");
}else{
var checkboxs = $(this).parents(".col-sm-1").next().find("input[type=checkbox]");
}
var atLeastOneUnchecked = false;
checkboxs.each(function () {
if( $(this).is(":checked") == false ){
atLeastOneUnchecked = true;
}
})
if( atLeastOneUnchecked == false && that.is(":checked") == false ){
that.trigger('click');
}
});
$(this).append("<input name='Rbac[permissions]' value='" + permissions + "'>")

$(".chooseAll").change(function () {
var type = $(this).is(':checked');
var checkboxs = $(this).parents("span").next().find("input[type=checkbox]");
if( checkboxs.length == 0 ) {
checkboxs = $(this).parents(".col-sm-1").next().find("input[type=checkbox]");
}
checkboxs.each(function () {
if(type != $(this).is(':checked')){
$(this).trigger('click');
}
})
})
})
</script>
<?php JsBlock::end()?>

0 comments on commit c62b442

Please sign in to comment.