yii2-auth
yii2-auth copied to clipboard
Adding Roles when create edit or view users
Ok now I propose to add roles in your code, because when we create or edit users, we should add or update roles to user.
Now I have added this functions in your user model.
/**
Return user Roles
**/
public function getRole($id = null)
{
/** @var \yii\rbac\DbManager $authManager */
$authManager = Yii::$app->get('authManager');
if($id===null)
{
$Ridentity = $authManager->getRolesByUser($this->id);
}
else
{
$Ridentity = $authManager->getRolesByUser($id);
}
if($Ridentity)
{
foreach ($Ridentity as $item)
{
$role[$item->name] = $item->name;
}
}
else
{
$role=null;
}
return $role;
}
function getRoleArray()
{
return implode(',', $this->role);
}
public function getRoleTypes()
{
/** @var \yii\rbac\DbManager $authManager */
$roller = Yii::$app->get('authManager')->getRoles();
foreach ($roller as $item)
{
$role[$item->name] = $item->name;
}
return $role;
}
function getRoleTypes =>Retrieve the roles from database, so we can use it to fill multiple select field when we create user.
function getRoleArray =>Return roles to a string when we view the user.
function getRole =>Retrieve the roles of the user, view and update pages.
form.php (create and update)
<?= $form->field($model, 'role')->dropDownList($model->RoleTypes,['multiple' => true])?>
and view.php
<?php echo DetailView::widget([
'model' => $model,
'attributes' => [
//'id',
'username',
'email:email',
'password_hash',
'password_reset_token',
'auth_key',
[
'attribute' => 'status',
'value' => $model->getStatus()
],
[
'attribute' => 'role',
'value' => $model->getRoleArray()
],
'last_visit_time',
'create_time',
'update_time',
'delete_time',
],
]); ?>
and in user controller a function to create or update roles
protected function setRole($id,$roles)
{
if(!empty( $roles ))
{
/** @var \yii\rbac\DbManager $authManager */
$authManager = \Yii::$app->get('authManager');
$authManager->revokeAll($id);
foreach ($roles as $item)
{
$r = $authManager->createRole($item);
$authManager->assign($r,$id);
}
}
else
{
throw new NotFoundHttpException('Bad Request.');
}
}
//usage
$this->setRole($id,$_POST['User']['role']);
What do you think ??
I am using this code now and I can create update and view roles of the user within your pages view.php, create.php and update.php.
If you provide a simple or more proper way, I will be very greatfull, but I think we should create or update role of the user when we use your create or update user form at the same time ..
Hi, I'm not sure... It doesn't make much sense (from the OOP point of view)... what you might mean is "getRoles()",... and also, it doesn't make sense to have and "$id" param in that function... because the class instance IS the user itself. So for this case, I agree having only one function called "getRoles" which returns an array containing all the permissions/roles for the active user (no $id param).
Regarding the function getRoleArray() looks fine, however, the array-to-string conversion should be work for the developer, because not all the developers need to format the resulting array into a comma separated string, you might want to format it using your own style. So, in that case, the "getRoles" functions is enough.
IMHO The other functions need some review. I'm not so sure of having the "setRoles" function, because you might want to append a single permission, and every time you want to add/remove a permission, the DB processing will be slow (because it will rewrite all the permissions again).
Do you have any suggestion to add the role without SetRole function to database ?? How can we do this with a simple way ?? Because model->save is not work, when I tried it gives me error .. I think we should done this with a seperate function ...
And yes the id is useless, I have forgot it there because I have tested something else, the function actually like this ..
public function getRole()
{
/** @var \yii\rbac\DbManager $authManager */
$authManager = Yii::$app->get('authManager');
$Ridentity = $authManager->getRolesByUser($this->id);
if($Ridentity)
{
foreach ($Ridentity as $item)
{
$role[$item->name] = $item->name;
}
}
else
{
$role=null;
}
return $role;
}
We need getRoleTypes function too, because there is no role when we create a user, and to be selected and attached to a user we need to return the role types from the database...
public function getRoleTypes()
{
/** @var \yii\rbac\DbManager $authManager */
$roller = Yii::$app->get('authManager')->getRoles();
foreach ($roller as $item)
{
$role[$item->name] = $item->name;
}
return $role;
}