add role to user

This commit is contained in:
2015-11-05 09:26:08 +01:00
parent bdd5a94b3f
commit 43d5598f23
9 changed files with 151 additions and 17 deletions

View File

@@ -65,7 +65,7 @@ class UserController extends \backend\controllers\BackendController
$this->updateAccountAssignments($model);
// return $this->redirect(['view', 'id' => $model->id]);
return $this->redirect(['index' ]);
}

View File

@@ -10,6 +10,8 @@ class UserCreate extends User{
public $password_repeat;
public $selected_accounts = [];
public $role;
/**
* @inheritdoc
*/
@@ -28,6 +30,9 @@ class UserCreate extends User{
['username' ,'unique' ],
[['password_plain' ,'password_repeat'] ,'string','min' =>6 ],
[['password_repeat'] ,'validatePasswordRepeat' ],
[['role'], 'required'],
[['role'], 'string', 'max' => 20],
];
}
@@ -66,9 +71,9 @@ class UserCreate extends User{
public function afterSave($insert, $changedAttributes){
parent::afterSave($insert, $changedAttributes);
// $am = Yii::$app->authManager;
// $role = $am->getRole('admin');
// Yii::$app->authManager->assign($role, $this->id);
$am = Yii::$app->authManager;
$role = $am->getRole($this->role);
Yii::$app->authManager->assign($role, $this->id);
}
}

View File

@@ -9,6 +9,8 @@ class UserUpdate extends User {
public $password_plain;
public $password_repeat;
public $selected_accounts = [];
public $role;
/**
* @inheritdoc
@@ -28,7 +30,9 @@ class UserUpdate extends User {
$this->addError($attribute, 'Invalid array');
}
}
]
],
[['role'], 'required'],
[['role'], 'string', 'max' => 20],
];
}
@@ -68,10 +72,11 @@ class UserUpdate extends User {
return false;
}
}
public function afterSave($insert, $changedAttributes) {
parent::afterSave ( $insert, $changedAttributes );
// $am = Yii::$app->authManager;
// $role = $am->getRole('admin');
// Yii::$app->authManager->assign($role, $this->id);
public function afterSave($insert, $changedAttributes){
parent::afterSave($insert, $changedAttributes);
$am = Yii::$app->authManager;
$am->revokeAll($this->id);
$role = $am->getRole($this->role);
Yii::$app->authManager->assign($role, $this->id);
}
}

View File

@@ -3,15 +3,21 @@
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\grid\GridView;
use yii\base\Widget;
use yii\base\Object;
use yii\data\ArrayDataProvider;
use common\components\RoleDefinition;
/* @var $this yii\web\View */
/* @var $model common\models\User */
/* @var $form yii\widgets\ActiveForm */
?>
<?php
$roleOptions = RoleDefinition::roleLabels();
asort($roleOptions);
?>
<div class="user-form">
<?php $form = ActiveForm::begin(); ?>
@@ -21,6 +27,7 @@ use yii\data\ArrayDataProvider;
<?= $form->field($model, 'email')->textInput() ?>
<?= $form->field($model, 'password_plain')->passwordInput() ?>
<?= $form->field($model, 'password_repeat')->passwordInput() ?>
<?= $form->field($model, 'role')->dropDownList($roleOptions) ?>
<?php

View File

@@ -2,6 +2,7 @@
use yii\helpers\Html;
use yii\grid\GridView;
use common\components\RoleDefinition;
/* @var $this yii\web\View */
/* @var $searchModel frontend\models\UserSearch */
@@ -28,6 +29,15 @@ $this->params['breadcrumbs'][] = $this->title;
'email:email',
'created_at:datetime',
[
'attribute' => 'role',
'value' => function ($model){
$roles = \Yii::$app->authManager->getRolesByUser($model->id );
return implode(', ', array_map(function ($role) { return sprintf("%s", RoleDefinition::getRoleLabel($role->name)); }, $roles ));
}
] ,
['class' => 'yii\grid\ActionColumn',
'template' => '{view} {update}'
],

View File

@@ -25,6 +25,10 @@ $this->params['breadcrumbs'][] = $this->title;
'email:email',
'statusHuman',
'created_at:datetime',
[
'attribute' => 'role',
'value' => $model->roleString
]
],
]) ?>