php 的 RBAC实现
程序员文章站
2022-03-20 17:23:23
//登陆控制器 function actionLogin(){ $name=Yii::$app->request->post("name"); $pass=Yii::$app->request->post("pass"); $res= Yii::$app->db->createCommand("se... ......
//登陆控制器 function actionlogin(){ $name=yii::$app->request->post("name"); $pass=yii::$app->request->post("pass"); $res= yii::$app->db->createcommand("select * from user where name='$name' and pass='$pass'")->queryone(); if ($res){ $session=yii::$app->session; $session->set("uid",$res['id']); $id=$res['id']; $sql=yii::$app->db->createcommand("select * from user join u_t on `user`.id=u_t.u_id join teacher on u_t.t_id=teacher.id join t_p on teacher.id=t_p.t_id join power on t_p.p_id=power.id where `user`.id=$id")->queryall(); $session->set("power",json_encode($sql)); return $this->redirect(["one/show"]); }else{ echo "<a href='add'>登陆失败</a>"; } }
//权限控制器 function init() { $session=yii::$app->session; $id=$session->get("uid"); if (empty($id)){ echo "<a href='/mon/add'>权限不足</a>"; } $power= $session->get("power"); $power= json_decode($power); foreach ($power as $k=>$v){ $arr[]=$v->controller.'/'.$v->function; } $res=yii::$app->requestedroute; if (!in_array($res,$arr)){ echo "<a href='/mon/add'>权限不足</a>";die; } } function actionshow(){ $session=yii::$app->session; $data=$session->get("power"); $arr=json_decode($data,1); $res= $this->gettree($arr,0); return $this->render("show",["res"=>$res]); } function gettree($data,$pid){ $tree=[]; foreach ($data as $k=>$v){ if ($v['pid']==$pid){ $v['son']=$this->gettree($data,$v['id']); $tree[]=$v; } } return $tree; }
//权限展示 <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="x-ua-compatible" content="ie=edge"> <title>document</title> </head> <body> <table> <?php foreach ($res as $key=>$value){?> <tr> <?php echo $value['power'] ?><br> <?php foreach ($value['son'] as $k=>$v){?> <a href="<?php echo $v['function'] ?>"><?php echo $v['power'] ?><br></a> </tr> <?php }?> <?php }?> </table> </body> </html>