//登陆控制器
functionactionLogin(){
$name=Yii::$app->request->post("name");
$pass=Yii::$app->request->post("pass");
$res=Yii::$app->db->createCommand("select*fromuserwherename='$name'andpass='$pass'")->queryOne();
if($res){
$session=Yii::$app->session;
$session->set("uid",$res['id']);
$id=$res['id'];
$sql=Yii::$app->db->createCommand("SELECT*fromuserjoinu_ton`user`.id=u_t.u_idjointeacheronu_t.t_id=teacher.idJOINt_ponteacher.id=t_p.t_idjoinpoweront_p.p_id=power.idwhere`user`.id=$id")->queryAll();
$session->set("power",json_encode($sql));
return$this->redirect(["one/show"]);
}else{
echo"<ahref='add'>登陆失败</a>";
}
}
//权限控制器
functioninit()
{
$session=Yii::$app->session;
$id=$session->get("uid");
if(empty($id)){
echo"<ahref='/mon/add'>权限不足</a>";
}
$power=$session->get("power");
$power=json_decode($power);
foreach($poweras$k=>$v){
$arr[]=$v->controller.'/'.$v->function;
}
$res=Yii::$app->requestedRoute;
if(!in_array($res,$arr)){
echo"<ahref='/mon/add'>权限不足</a>";die;
}
}
functionactionShow(){
$session=Yii::$app->session;
$data=$session->get("power");
$arr=json_decode($data,1);
$res=$this->gettree($arr,0);
return$this->render("show",["res"=>$res]);
}
functiongettree($data,$pid){
$tree=[];
foreach($dataas$k=>$v){
if($v['pid']==$pid){
$v['son']=$this->gettree($data,$v['id']);
$tree[]=$v;
}
}
return$tree;
}
//权限展示
<!doctypehtml>
<htmllang="en">
<head>
<metacharset="UTF-8">
<metaname="viewport"
content="width=device-width,user-scalable=no,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0">
<metahttp-equiv="X-UA-Compatible"content="ie=edge">
<title>Document</title>
</head>
<body>
<table>
<?phpforeach($resas$key=>$value){?>
<tr>
<?phpecho$value['power']?><br>
<?phpforeach($value['son']as$k=>$v){?>
<ahref="<?phpecho$v['function']?>"><?phpecho$v['power']?><br></a>
</tr>
<?php}?>
<?php}?>
</table>
</body>
</html>
|