123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Administrator
- * Date: 2018/2/26/026
- * Time: 15:06
- */
- namespace backend\controllers;
- use backend\base\CommonController;
- use backend\base\Help;
- use Yii;
- use common\models\UserRole;
- use common\models\UserNode;
- use yii\db\Query;
- class AuthController extends CommonController
- {
- public $enableCsrfValidation = false;
- public function actionAaa()
- {
- //dadsadsadas
- //2234
- //33
- }
- public function actionAaaa()
- {
- $data = (new Query())
- ->from('pfg_demo')
- ->all();
- foreach ($data as &$val) {
- $val['data'] = json_decode($val['data'], true);
- }
- p($data);
- }
- /*
- * 角色列表
- * */
- public function actionRolelist()
- {
- return $this->render('rolelist');
- }
- /*
- * 角色列表数据
- * */
- public function actionRolelistfrom()
- {
- $model = $this->ModelRole();
- $rows = $model->getList(Yii::$app->request->post());
- if ($rows['data']) {
- foreach ($rows['data'] as &$val) {
- $val['create_at'] = date('Y-m-d H:i', $val['create_at']);
- $val['update_at'] = date('Y-m-d H:i', $val['update_at']);
- // $val['state'] = Yii::$app->params['state'][$val['state']];
- }
- return Help::JsonData(0, '成功', $rows['count'], $rows['data']);
- }
- return Help::JsonData(0, '暂无信息');
- }
- /*
- * 设置角色状态
- * */
- public function actionRoledelstate()
- {
- $model = $this->ModelRole();
- $input = Yii::$app->request->post();
- $rows = $model->FindById($input['id']);
- $rows->state = $input['state'];
- if ($rows->update()) return Help::JsonCode(Help::SUCCESS, '修改成功');
- return Help::JsonCode(Help::ERROR, '修改失败');
- }
- /*
- * 删除角色
- * */
- public function actionRoledel()
- {
- $model = $this->ModelRole();
- $row = $model->FindById(Yii::$app->request->post('id'));
- if ($row->delete() == true) return Help::JsonCode(Help::SUCCESS, '操作成功');
- return Help::JsonCode(Help::ERROR, '操作失败');
- }
- /*
- * 修改角色界面
- * */
- public function actionEditrole()
- {
- $model = $this->ModelRole();
- $row = $model->FindById(Yii::$app->request->get('id'));
- if ($row != null) {
- return $this->render('editrole', ['model' => $row]);
- }
- }
- /*
- * 修改角色数据
- * */
- public function actionEditrolefrom()
- {
- $model = $this->ModelRole();
- $row = $model->Check(Yii::$app->request->post('data'));
- if (is_object($row)) {
- $find = $row->FindById(Yii::$app->request->post('data')['id']);
- $find->name = $row->name;
- $find->describe = $row->describe;
- $find->state = $row->state;
- if ($find->update() == true) return Help::JsonCode(Help::SUCCESS, '修改成功');
- }
- return Help::JsonCode(Help::ERROR, '修改失败');
- }
- /*
- * 添加角色页面
- * */
- public function actionAddrole()
- {
- return $this->render('addrole');
- }
- /*
- * 接收角色数据
- * */
- public function actionAddrolefrom()
- {
- $model = $this->ModelRole();
- $model->scenario = 'add';
- $obj = $model->Check(Yii::$app->request->post('data'));
- if (is_object($obj) && $obj->insert() == true) return Help::JsonCode(200, '添加成功。');
- return Help::JsonCode(300, '添加失败', $obj);
- }
- /*
- *角色添加节点页面
- * */
- public function actionAddrolenode()
- {
- $model = $this->ModelNode();
- $rows = $model->getList([]);
- $rn = new \common\models\UserRn();
- $rn->rid = Yii::$app->request->get('id');
- $rn->type = 1;
- $roleList = $rn->FindRid();
- $nid = array_column($roleList, 'nid');
- foreach ($rows['data'] as &$val) {
- if (in_array($val['id'], $nid)) {
- $val['checked'] = 'checked';
- }
- }
- if ($rows != null) return $this->render('addrolenode', ['model' => $rows['data'], 'id' => Yii::$app->request->get('id')]);
- }
- /*
- * 角色添加菜单页面
- * */
- public function actionAddrolemenu()
- {
- // $model = new \common\models\Sysmenu();
- // $list = $model->getList([]);
- // $rn = new \common\models\UserRn();
- // $rn->rid = Yii::$app->request->get('id');
- // $rn->type = 2;
- // $roleList = $rn->FindRid();
- // $nid = array_column($roleList,'nid');
- // foreach ($list['data'] as &$val)
- // {
- // if(in_array($val['id'],$nid))
- // {
- // $val['checked'] = 'checked';
- // }
- // }
- return $this->render('addrolemenu', ['id' => Yii::$app->request->get('id')]);
- }
- /*
- * 查看该角色拥有的菜单权限
- * */
- public function actionAddrolemenuform()
- {
- $rn = new \common\models\UserRn();
- $rn->rid = Yii::$app->request->get('id');
- $rn->type = 2;
- $roleList = $rn->FindRid();
- $nid = array_column($roleList, 'nid'); //查询已经添加过的菜单
- //菜单
- $model = new \common\models\Sysmenu();
- $list = $model->PidList();
- $pid = array_column($list, 'id');
- $list_a = $model->PidList($pid);
- $arrA = [];
- $list_b = $model->PidList(array_column($list_a, 'id'));
- $arrB = [];
- //3级菜单
- foreach ($list_b as &$v) {
- if (in_array($v['id'], $nid)) {
- $v['checked'] = 'checked';
- }
- $arrB[$v['pid']][] = $v;
- }
- //2级菜单
- foreach ($list_a as &$value) {
- if (in_array($value['id'], $nid)) {
- $value['checked'] = 'checked';
- }
- if (!empty($arrB[$value['id']])) {
- $value['data'] = $arrB[$value['id']];
- }
- $arrA[$value['pid']][] = $value;
- }
- //1级菜单
- foreach ($list as $key => $val) {
- if (in_array($val['id'], $nid)) {
- $list[$key]['checked'] = 'checked';
- }
- if (!empty($arrA[$val['id']])) {
- $list[$key]['data'] = $arrA[$val['id']];
- }
- }
- return Help::JsonCode(Help::SUCCESS, '成功', $list);
- }
- /*
- * 给角色添加节点 and 菜单 节点type = 1 菜单 = 2
- * */
- public function actionAddroleandnodefrom()
- {
- $model = (new \backend\server\RoleAuthority())->CreateRole();
- if ($model === true) {
- return Help::JsonCode(200, '角色权限分配成功');
- }
- return Help::JsonCode(300, '角色分配权限失败', $model);
- }
- /*
- * 节点列表显示
- * */
- public function actionNodelist()
- {
- return $this->render('nodelist');
- }
- /*
- * 获取节点列表
- * */
- public function actionNodelistfrom()
- {
- $model = $this->ModelNode();
- $rows = $model->getList(Yii::$app->request->post());
- if ($rows['data'] != null) return Help::JsonData(0, '数据获取成', $rows['count'], $rows['data']);
- return Help::JsonData(0, '数据获取失败');
- }
- /*
- * 添加节点页面
- * */
- public function actionAddnode()
- {
- return $this->render('addnode');
- }
- /*
- * 接收节点数据
- * */
- public function actionAddnodefrom()
- {
- $model = $this->ModelNode();
- $obj = $model->Check(Yii::$app->request->post('data'));
- if (is_array($obj)) return Help::JsonCode(300, '添加失败', $obj);
- if ($model->MultipleCondition() != null) return Help::JsonCode(300, '该控制器和方法已经存在');
- $obj->controller = strtolower($obj->controller);
- $obj->action = strtolower($obj->action);
- if (is_object($obj) && $obj->insert() == true) {
- return Help::JsonCode(200, '添加成功。');
- }
- }
- /*
- * 修改节点页面
- * */
- public function actionEditnode()
- {
- $model = $this->ModelNode();
- $row = $model->FindById(Yii::$app->request->get('id'));
- if ($row != null) {
- return $this->render('editnode', ['model' => $row]);
- }
- }
- /*
- * 修改节点数据
- * */
- public function actionEditnodefrom()
- {
- $model = $this->ModelNode();
- $obj = $model->Check(Yii::$app->request->post('data'));
- if (is_object($obj)) {
- $FindObj = $obj->FindById(Yii::$app->request->post('data')['id']);
- $FindObj->controller = $obj->controller;
- $FindObj->action = $obj->action;
- $FindObj->describe = $obj->describe;
- // $FindObj->attributes = $obj->attributes;
- if ($FindObj->update() == true) return Help::JsonCode(200, '修改成功');
- }
- return Help::JsonCode(300, '修改失败');
- }
- /*
- * 删除节点
- * */
- public function actionDelfrom()
- {
- $model = $this->ModelNode();
- $FindObj = $model->FindById(Yii::$app->request->post('id'));
- if ($FindObj != null) {
- if ($FindObj->delete()) {
- return Help::JsonCode(200, '删除成功');
- }
- }
- return Help::JsonCode(300, '删除失败');
- }
- /*
- * 用户添加角色界面
- * */
- public function actionUseraddrole()
- {
- return $this->render();
- }
- /*
- * 给用户添加角色
- * */
- public function actionUseraddrolefrom()
- {
- $model = (new \backend\server\RoleAuthority())->CreateUserRole();
- if ($model === true) {
- Help::JsonCode(200, '用户角色分配成功');
- }
- Help::JsonCode(300, '用户角色分配失败');
- }
- /*
- * 遍历所有控制器
- * */
- public function actionAllcontroller()
- {
- $dir = Yii::$app->basePath;
- if (is_dir($dir)) {
- $dirArr = glob($dir . "/controllers/*.php");
- if (is_array($dirArr)) {
- $match = '#class (.*) extends#';
- $metch = '#public function action(.*)()#';
- $metchs = '@/\*.*?\*/@s';
- $model = new \common\models\UserNode();
- foreach ($dirArr as $v) {
- $lines = file_get_contents($v);
- preg_match($match, $lines, $claaName);
- $controller = strtolower(substr($claaName[1], 0, strpos($claaName[1], "Controller")));
- if ($controller == 'public') {
- continue;
- }
- if ($controller == 'tinifyimg') {
- continue;
- }
- preg_match_all($metch, $lines, $classFunc);
- preg_match_all($metchs, $lines, $classzhus);
- //处理头部的注释
- if (strstr($classzhus[0][0], 'Created')) {
- unset($classzhus[0][0]);
- $classzhus = array_values($classzhus[0]);
- }
- $qian = array(" ", " ", "\t", "\n", "\r");
- $hou = array("", "", "", "", "");
- foreach ($classFunc[1] as $key => $val) {
- if (strstr($val, '(.*)')) {
- continue;
- }
- $_model = clone $model;
- $action = strtolower(str_replace('()', '', $val));
- $row = $_model::find()->andWhere(['controller' => $controller])->andWhere(['action' => $action])->one();
- if ($row == null) {
- $_model->controller = $controller;
- $_model->action = str_replace($qian, $hou, $action);
- if (!empty($classzhus[$key])) {
- $zhushi = str_replace($qian, $hou, str_replace('/', '', str_replace('*', '', $classzhus[$key])));
- if (isset($zhushi[$key])) {
- $_model->describe = $zhushi[$key];
- }
- // $_model->describe = str_replace($qian,$hou,str_replace('/','',str_replace('*','', $classzhus[$key])));
- }
- $_model->insert(false);
- }
- }
- // return Help::JsonCode(Help::SUCCESS,Yii::t('app','add_success'));
- }
- return Help::JsonCode(Help::SUCCESS, Yii::t('app', 'add_success'));
- }
- }
- // $controller = strtolower(substr($claaName[1],0,strpos($claaName[1],"Controller")));
- //
- // $metch = '#public function action(.*)()#';
- // $metchs = '@/\*.*?\*/@s';
- // preg_match_all($metch, $lines,$classFunc);
- // preg_match_all($metchs, $lines,$classzhus);
- //
- // //处理头部的注释
- // if(strstr($classzhus[0][0],'Created')){
- // unset($classzhus[0][0]);
- // $classzhus = array_values($classzhus[0]);
- // }
- //
- ////
- // $qian=array(" "," ","\t","\n","\r");
- // $hou=array("","","","","");
- //
- // $model = new \common\models\UserNode();
- // foreach ($classFunc[1] as $key=>$val)
- // {
- // if(strstr($val,'(.*)'))
- // {
- // continue;
- // }
- // $_model = clone $model;
- // $action = strtolower(str_replace('()','',$val));
- // $row = $_model::find()->andWhere(['controller'=>$controller])->andWhere(['action'=>$action])->one();
- // if($row == null)
- // {
- // $_model->controller = $controller;
- // $_model->action = str_replace($qian,$hou,$action);
- // $_model->describe = str_replace($qian,$hou,str_replace('/','',str_replace('*','', $classzhus[$key])));
- // $_model->insert(false);
- // }
- //
- // }
- }
- protected function ModelRole()
- {
- return new UserRole();
- }
- protected function ModelNode()
- {
- return new UserNode();
- }
- }
|