1. Add following code to _form.php
Yii::app()->getModule('rights'); if (Yii::app()->user->isSuperuser) { $all_roles = new RAuthItemDataProvider('roles', array( 'type' => 2, )); $data = $all_roles->fetchData(); echo CHtml::activeDropDownList($model, 'user_role', CHtml::listData($data, 'name', 'name')); } 2. Update actionCreate & actionUpdate of UsersController.php like this
public function actionCreate() { $model = new User; // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if (isset($_POST['User'])) { $model->attributes = $_POST['User']; if ($model->save()) { Rights::assign($model->user_role, $model->id); $this->redirect(array('view', 'id' => $model->id)); } } $this->render('create', array( 'model' => $model, )); } public function actionUpdate($id) { $model = $this->loadModel($id); // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if (isset($_POST['User'])) { $model->attributes = $_POST['User']; if ($model->save()) { Yii::app()->getModule('rights'); $assignedRoles = Rights::getAssignedRoles($model->id); foreach ($assignedRoles as $role => $detail) { Rights::revoke($role, $model->id); } Rights::assign($model->user_role, $model->id); $this->redirect(array('view', 'id' => $model->id)); } } $this->render('update', array( 'model' => $model, )); }
Allow admin to change user role
1. Add following code to _form.php [code language=”php”] Yii::app()->getModule(‘rights’); if (Yii::app()->user->isSuperuser) {
Scriptbaker Editorial Team
The Scriptbaker editorial team comprises engineers, AI specialists, and digital strategists based in Dubai and Rawalpindi. We write about software development, artificial intelligence, and digital transformation to help organisations build better products. Learn more about us →
Related posts
Yii
Separate Front & Admin in Yii
In my previous post I demonstrated you how to render different layout for different user role, now lets move a step forward and see how to create separate front
Yii
Yii Multiple Related Models Save Guide
Scenario: You are in a situation where you have two related tables a Parent and a child table. You need to create a user experience in which user presses Save b
Yii
How to make Yii checkBoxList selected
Many people find it hard to make the Yii checkBoxList selected on update view, but believe me in fact its very easy and straight forward. This post will help yo