yii rbac管理
阿新 • • 發佈:2017-06-14
read pac phpstorm 好的 span from 繼續 action extend
以下是Controller代碼
<?php /** * Created by PhpStorm. * User: zhoukang * Date: 2017/6/11 * Time: 19:31 */ namespace frontend\controllers; use yii\web\Controller; use frontend\models\Rbac; use yii; use frontend\models\Loginform; class RbacController extends Controller { public function actionIndex(){return $this->render(‘index‘,[‘message‘=>‘您正在使用的是權限控制‘]); } //創建角色 public function actionCreaterole(){ $model = new Rbac(); return $this->render(‘role‘,[‘model‘=>$model]); } //將創建的角色入庫 public function actionDoadd(){ $role= Yii::$app->request->post(‘Rbac‘); $item = $role[‘role‘]; $auth = Yii::$app->authManager; $role = $auth->createRole($item); $role->description = ‘創建了 ‘ . $item . ‘ 角色‘; $auth->add($role); return $this->render(‘index‘,[‘message‘=>‘創建角色成功,您可以繼續操作‘]); } //創建權限 public function actionCreatepower(){ $model = new Rbac(); return $this->render(‘power‘,[‘model‘=>$model]); } //將創建的權限入庫 public function actionDopower(){ $power = Yii::$app->request->post(‘Rbac‘); $item = $power[‘power‘]; $auth = Yii::$app->authManager; $createPost = $auth->createPermission($item); $createPost->description = ‘創建了 ‘ . $item . ‘ 許可‘; $auth->add($createPost); return $this->render(‘index‘,[‘message‘=>‘創建權限成功,您可以繼續操作‘]); } //給角色分配權限 public function actionRp(){ $models = new Loginform(); $model = new Rbac(); //查出所有的角色 $role = Rbac::getrole(‘auth_item‘); //查出所有的權限 $power = Rbac::getpower(‘auth_item‘); $roles = $models->arraytostring($role,‘name‘,‘name‘); $powers = $models->arraytostring($power,‘name‘,‘name‘); return $this->render(‘rp‘,[‘model‘=>$model,‘role‘=>$roles,‘power‘=>$powers]); } //將分配好的角色和權限入庫 public function actionDorp(){ $data = Yii::$app->request->post(‘Rbac‘); $power = $data[‘power‘]; $role = $data[‘role‘]; $item = Rbac::rolepower($role,$power); foreach($item as $items){ $auth = Yii::$app->authManager; $parent = $auth->createRole($items[0]); $child = $auth->createPermission($items[1]); $auth->addChild($parent, $child); } return $this->render(‘index‘,[‘message‘=>‘分配權限成功,您可以繼續操作‘]); } //給用戶分配角色 public function actionUr(){ $models = new Loginform(); $model = new Rbac(); //查出所有的角色 $role = Rbac::getrole(‘auth_item‘); //查出所有的權限 $user = Rbac::getuser(‘user‘); $roles = $models->arraytostring($role,‘name‘,‘name‘); $users = $models->arraytostring($user,‘id‘,‘username‘); return $this->render(‘ur‘,[‘model‘=>$model,‘role‘=>$roles,‘user‘=>$users]); } public function actionDour(){ $data = Yii::$app->request->post(‘Rbac‘); $user = $data[‘user‘]; $role = $data[‘role‘]; $item = Rbac::rolepower($role,$user); foreach($item as $items){ $auth = Yii::$app->authManager; $reader = $auth->createRole($items[0]); $auth->assign($reader, $items[1]); } return $this->render(‘index‘,[‘message‘=>‘分配角色成功,您可以繼續操作‘]); } public function beforeAction($action) { $action = Yii::$app->controller->action->id; if(\Yii::$app->user->can($action)){ return true; }else{ throw new \yii\web\UnauthorizedHttpException(‘對不起,您現在還沒獲此操作的權限‘); } } }
以下是model 代碼
<?php /** * Created by PhpStorm. * User: zhoukang * Date: 2017/6/11 * Time: 18:41 */ namespace frontend\models; use yii\base\Model; use yii; use db; class Rbac extends Model { public $role; public $power; public $user; public function rules(){ return[ ]; } public function attributeLabels() { return[ ‘role‘=>‘角色‘, ‘power‘=>‘權限‘, ‘user‘=>‘用戶‘ ]; } static public function getrole($tablename){ $sql = "select name from $tablename where TYPE = 1"; $data = Yii::$app->db->createCommand($sql)->queryAll(); return $data; } static public function getpower($tablename){ $sql = "select name from $tablename where TYPE = 2"; $data = Yii::$app->db->createCommand($sql)->queryAll(); return $data; } static public function rolepower($role,$power){ $arr = array(); foreach($role as $value) { foreach ($power as $v) { $arr[] = array($value, $v); } } return $arr; } static public function getuser($tablename){ $sql = "select id,username from $tablename "; $data = Yii::$app->db->createCommand($sql)->queryAll(); return $data; } }
view 層代碼就沒必要寫了吧
yii rbac管理