AuthController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2018/2/26/026
  6. * Time: 15:06
  7. */
  8. namespace backend\controllers;
  9. use backend\base\CommonController;
  10. use backend\base\Help;
  11. use Yii;
  12. use common\models\UserRole;
  13. use common\models\UserNode;
  14. use yii\db\Query;
  15. class AuthController extends CommonController
  16. {
  17. public $enableCsrfValidation = false;
  18. public function actionAaa()
  19. {
  20. //dadsadsadas
  21. //2234
  22. //33
  23. }
  24. public function actionAaaa()
  25. {
  26. $data = (new Query())
  27. ->from('pfg_demo')
  28. ->all();
  29. foreach ($data as &$val) {
  30. $val['data'] = json_decode($val['data'], true);
  31. }
  32. p($data);
  33. }
  34. /*
  35. * 角色列表
  36. * */
  37. public function actionRolelist()
  38. {
  39. return $this->render('rolelist');
  40. }
  41. /*
  42. * 角色列表数据
  43. * */
  44. public function actionRolelistfrom()
  45. {
  46. $model = $this->ModelRole();
  47. $rows = $model->getList(Yii::$app->request->post());
  48. if ($rows['data']) {
  49. foreach ($rows['data'] as &$val) {
  50. $val['create_at'] = date('Y-m-d H:i', $val['create_at']);
  51. $val['update_at'] = date('Y-m-d H:i', $val['update_at']);
  52. // $val['state'] = Yii::$app->params['state'][$val['state']];
  53. }
  54. return Help::JsonData(0, '成功', $rows['count'], $rows['data']);
  55. }
  56. return Help::JsonData(0, '暂无信息');
  57. }
  58. /*
  59. * 设置角色状态
  60. * */
  61. public function actionRoledelstate()
  62. {
  63. $model = $this->ModelRole();
  64. $input = Yii::$app->request->post();
  65. $rows = $model->FindById($input['id']);
  66. $rows->state = $input['state'];
  67. if ($rows->update()) return Help::JsonCode(Help::SUCCESS, '修改成功');
  68. return Help::JsonCode(Help::ERROR, '修改失败');
  69. }
  70. /*
  71. * 删除角色
  72. * */
  73. public function actionRoledel()
  74. {
  75. $model = $this->ModelRole();
  76. $row = $model->FindById(Yii::$app->request->post('id'));
  77. if ($row->delete() == true) return Help::JsonCode(Help::SUCCESS, '操作成功');
  78. return Help::JsonCode(Help::ERROR, '操作失败');
  79. }
  80. /*
  81. * 修改角色界面
  82. * */
  83. public function actionEditrole()
  84. {
  85. $model = $this->ModelRole();
  86. $row = $model->FindById(Yii::$app->request->get('id'));
  87. if ($row != null) {
  88. return $this->render('editrole', ['model' => $row]);
  89. }
  90. }
  91. /*
  92. * 修改角色数据
  93. * */
  94. public function actionEditrolefrom()
  95. {
  96. $model = $this->ModelRole();
  97. $row = $model->Check(Yii::$app->request->post('data'));
  98. if (is_object($row)) {
  99. $find = $row->FindById(Yii::$app->request->post('data')['id']);
  100. $find->name = $row->name;
  101. $find->describe = $row->describe;
  102. $find->state = $row->state;
  103. if ($find->update() == true) return Help::JsonCode(Help::SUCCESS, '修改成功');
  104. }
  105. return Help::JsonCode(Help::ERROR, '修改失败');
  106. }
  107. /*
  108. * 添加角色页面
  109. * */
  110. public function actionAddrole()
  111. {
  112. return $this->render('addrole');
  113. }
  114. /*
  115. * 接收角色数据
  116. * */
  117. public function actionAddrolefrom()
  118. {
  119. $model = $this->ModelRole();
  120. $model->scenario = 'add';
  121. $obj = $model->Check(Yii::$app->request->post('data'));
  122. if (is_object($obj) && $obj->insert() == true) return Help::JsonCode(200, '添加成功。');
  123. return Help::JsonCode(300, '添加失败', $obj);
  124. }
  125. /*
  126. *角色添加节点页面
  127. * */
  128. public function actionAddrolenode()
  129. {
  130. $model = $this->ModelNode();
  131. $rows = $model->getList([]);
  132. $rn = new \common\models\UserRn();
  133. $rn->rid = Yii::$app->request->get('id');
  134. $rn->type = 1;
  135. $roleList = $rn->FindRid();
  136. $nid = array_column($roleList, 'nid');
  137. foreach ($rows['data'] as &$val) {
  138. if (in_array($val['id'], $nid)) {
  139. $val['checked'] = 'checked';
  140. }
  141. }
  142. if ($rows != null) return $this->render('addrolenode', ['model' => $rows['data'], 'id' => Yii::$app->request->get('id')]);
  143. }
  144. /*
  145. * 角色添加菜单页面
  146. * */
  147. public function actionAddrolemenu()
  148. {
  149. // $model = new \common\models\Sysmenu();
  150. // $list = $model->getList([]);
  151. // $rn = new \common\models\UserRn();
  152. // $rn->rid = Yii::$app->request->get('id');
  153. // $rn->type = 2;
  154. // $roleList = $rn->FindRid();
  155. // $nid = array_column($roleList,'nid');
  156. // foreach ($list['data'] as &$val)
  157. // {
  158. // if(in_array($val['id'],$nid))
  159. // {
  160. // $val['checked'] = 'checked';
  161. // }
  162. // }
  163. return $this->render('addrolemenu', ['id' => Yii::$app->request->get('id')]);
  164. }
  165. /*
  166. * 查看该角色拥有的菜单权限
  167. * */
  168. public function actionAddrolemenuform()
  169. {
  170. $rn = new \common\models\UserRn();
  171. $rn->rid = Yii::$app->request->get('id');
  172. $rn->type = 2;
  173. $roleList = $rn->FindRid();
  174. $nid = array_column($roleList, 'nid'); //查询已经添加过的菜单
  175. //菜单
  176. $model = new \common\models\Sysmenu();
  177. $list = $model->PidList();
  178. $pid = array_column($list, 'id');
  179. $list_a = $model->PidList($pid);
  180. $arrA = [];
  181. $list_b = $model->PidList(array_column($list_a, 'id'));
  182. $arrB = [];
  183. //3级菜单
  184. foreach ($list_b as &$v) {
  185. if (in_array($v['id'], $nid)) {
  186. $v['checked'] = 'checked';
  187. }
  188. $arrB[$v['pid']][] = $v;
  189. }
  190. //2级菜单
  191. foreach ($list_a as &$value) {
  192. if (in_array($value['id'], $nid)) {
  193. $value['checked'] = 'checked';
  194. }
  195. if (!empty($arrB[$value['id']])) {
  196. $value['data'] = $arrB[$value['id']];
  197. }
  198. $arrA[$value['pid']][] = $value;
  199. }
  200. //1级菜单
  201. foreach ($list as $key => $val) {
  202. if (in_array($val['id'], $nid)) {
  203. $list[$key]['checked'] = 'checked';
  204. }
  205. if (!empty($arrA[$val['id']])) {
  206. $list[$key]['data'] = $arrA[$val['id']];
  207. }
  208. }
  209. return Help::JsonCode(Help::SUCCESS, '成功', $list);
  210. }
  211. /*
  212. * 给角色添加节点 and 菜单 节点type = 1 菜单 = 2
  213. * */
  214. public function actionAddroleandnodefrom()
  215. {
  216. $model = (new \backend\server\RoleAuthority())->CreateRole();
  217. if ($model === true) {
  218. return Help::JsonCode(200, '角色权限分配成功');
  219. }
  220. return Help::JsonCode(300, '角色分配权限失败', $model);
  221. }
  222. /*
  223. * 节点列表显示
  224. * */
  225. public function actionNodelist()
  226. {
  227. return $this->render('nodelist');
  228. }
  229. /*
  230. * 获取节点列表
  231. * */
  232. public function actionNodelistfrom()
  233. {
  234. $model = $this->ModelNode();
  235. $rows = $model->getList(Yii::$app->request->post());
  236. if ($rows['data'] != null) return Help::JsonData(0, '数据获取成', $rows['count'], $rows['data']);
  237. return Help::JsonData(0, '数据获取失败');
  238. }
  239. /*
  240. * 添加节点页面
  241. * */
  242. public function actionAddnode()
  243. {
  244. return $this->render('addnode');
  245. }
  246. /*
  247. * 接收节点数据
  248. * */
  249. public function actionAddnodefrom()
  250. {
  251. $model = $this->ModelNode();
  252. $obj = $model->Check(Yii::$app->request->post('data'));
  253. if (is_array($obj)) return Help::JsonCode(300, '添加失败', $obj);
  254. if ($model->MultipleCondition() != null) return Help::JsonCode(300, '该控制器和方法已经存在');
  255. $obj->controller = strtolower($obj->controller);
  256. $obj->action = strtolower($obj->action);
  257. if (is_object($obj) && $obj->insert() == true) {
  258. return Help::JsonCode(200, '添加成功。');
  259. }
  260. }
  261. /*
  262. * 修改节点页面
  263. * */
  264. public function actionEditnode()
  265. {
  266. $model = $this->ModelNode();
  267. $row = $model->FindById(Yii::$app->request->get('id'));
  268. if ($row != null) {
  269. return $this->render('editnode', ['model' => $row]);
  270. }
  271. }
  272. /*
  273. * 修改节点数据
  274. * */
  275. public function actionEditnodefrom()
  276. {
  277. $model = $this->ModelNode();
  278. $obj = $model->Check(Yii::$app->request->post('data'));
  279. if (is_object($obj)) {
  280. $FindObj = $obj->FindById(Yii::$app->request->post('data')['id']);
  281. $FindObj->controller = $obj->controller;
  282. $FindObj->action = $obj->action;
  283. $FindObj->describe = $obj->describe;
  284. // $FindObj->attributes = $obj->attributes;
  285. if ($FindObj->update() == true) return Help::JsonCode(200, '修改成功');
  286. }
  287. return Help::JsonCode(300, '修改失败');
  288. }
  289. /*
  290. * 删除节点
  291. * */
  292. public function actionDelfrom()
  293. {
  294. $model = $this->ModelNode();
  295. $FindObj = $model->FindById(Yii::$app->request->post('id'));
  296. if ($FindObj != null) {
  297. if ($FindObj->delete()) {
  298. return Help::JsonCode(200, '删除成功');
  299. }
  300. }
  301. return Help::JsonCode(300, '删除失败');
  302. }
  303. /*
  304. * 用户添加角色界面
  305. * */
  306. public function actionUseraddrole()
  307. {
  308. return $this->render();
  309. }
  310. /*
  311. * 给用户添加角色
  312. * */
  313. public function actionUseraddrolefrom()
  314. {
  315. $model = (new \backend\server\RoleAuthority())->CreateUserRole();
  316. if ($model === true) {
  317. Help::JsonCode(200, '用户角色分配成功');
  318. }
  319. Help::JsonCode(300, '用户角色分配失败');
  320. }
  321. /*
  322. * 遍历所有控制器
  323. * */
  324. public function actionAllcontroller()
  325. {
  326. $dir = Yii::$app->basePath;
  327. if (is_dir($dir)) {
  328. $dirArr = glob($dir . "/controllers/*.php");
  329. if (is_array($dirArr)) {
  330. $match = '#class (.*) extends#';
  331. $metch = '#public function action(.*)()#';
  332. $metchs = '@/\*.*?\*/@s';
  333. $model = new \common\models\UserNode();
  334. foreach ($dirArr as $v) {
  335. $lines = file_get_contents($v);
  336. preg_match($match, $lines, $claaName);
  337. $controller = strtolower(substr($claaName[1], 0, strpos($claaName[1], "Controller")));
  338. if ($controller == 'public') {
  339. continue;
  340. }
  341. if ($controller == 'tinifyimg') {
  342. continue;
  343. }
  344. preg_match_all($metch, $lines, $classFunc);
  345. preg_match_all($metchs, $lines, $classzhus);
  346. //处理头部的注释
  347. if (strstr($classzhus[0][0], 'Created')) {
  348. unset($classzhus[0][0]);
  349. $classzhus = array_values($classzhus[0]);
  350. }
  351. $qian = array(" ", " ", "\t", "\n", "\r");
  352. $hou = array("", "", "", "", "");
  353. foreach ($classFunc[1] as $key => $val) {
  354. if (strstr($val, '(.*)')) {
  355. continue;
  356. }
  357. $_model = clone $model;
  358. $action = strtolower(str_replace('()', '', $val));
  359. $row = $_model::find()->andWhere(['controller' => $controller])->andWhere(['action' => $action])->one();
  360. if ($row == null) {
  361. $_model->controller = $controller;
  362. $_model->action = str_replace($qian, $hou, $action);
  363. if (!empty($classzhus[$key])) {
  364. $zhushi = str_replace($qian, $hou, str_replace('/', '', str_replace('*', '', $classzhus[$key])));
  365. if (isset($zhushi[$key])) {
  366. $_model->describe = $zhushi[$key];
  367. }
  368. // $_model->describe = str_replace($qian,$hou,str_replace('/','',str_replace('*','', $classzhus[$key])));
  369. }
  370. $_model->insert(false);
  371. }
  372. }
  373. // return Help::JsonCode(Help::SUCCESS,Yii::t('app','add_success'));
  374. }
  375. return Help::JsonCode(Help::SUCCESS, Yii::t('app', 'add_success'));
  376. }
  377. }
  378. // $controller = strtolower(substr($claaName[1],0,strpos($claaName[1],"Controller")));
  379. //
  380. // $metch = '#public function action(.*)()#';
  381. // $metchs = '@/\*.*?\*/@s';
  382. // preg_match_all($metch, $lines,$classFunc);
  383. // preg_match_all($metchs, $lines,$classzhus);
  384. //
  385. // //处理头部的注释
  386. // if(strstr($classzhus[0][0],'Created')){
  387. // unset($classzhus[0][0]);
  388. // $classzhus = array_values($classzhus[0]);
  389. // }
  390. //
  391. ////
  392. // $qian=array(" "," ","\t","\n","\r");
  393. // $hou=array("","","","","");
  394. //
  395. // $model = new \common\models\UserNode();
  396. // foreach ($classFunc[1] as $key=>$val)
  397. // {
  398. // if(strstr($val,'(.*)'))
  399. // {
  400. // continue;
  401. // }
  402. // $_model = clone $model;
  403. // $action = strtolower(str_replace('()','',$val));
  404. // $row = $_model::find()->andWhere(['controller'=>$controller])->andWhere(['action'=>$action])->one();
  405. // if($row == null)
  406. // {
  407. // $_model->controller = $controller;
  408. // $_model->action = str_replace($qian,$hou,$action);
  409. // $_model->describe = str_replace($qian,$hou,str_replace('/','',str_replace('*','', $classzhus[$key])));
  410. // $_model->insert(false);
  411. // }
  412. //
  413. // }
  414. }
  415. protected function ModelRole()
  416. {
  417. return new UserRole();
  418. }
  419. protected function ModelNode()
  420. {
  421. return new UserNode();
  422. }
  423. }