Admin.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace app\admin\controller\auth;
  3. use app\common\controller\Backend;
  4. use fast\Random;
  5. use fast\Tree;
  6. /**
  7. * 管理员管理
  8. *
  9. * @icon fa fa-users
  10. * @remark 一个管理员可以有多个角色组,左侧的菜单根据管理员所拥有的权限进行生成
  11. */
  12. class Admin extends Backend
  13. {
  14. protected $model = null;
  15. //当前登录管理员所有子节点组别
  16. protected $childrenIds = [];
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $this->model = model('Admin');
  21. $groups = $this->auth->getGroups();
  22. // 取出所有分组
  23. $grouplist = model('AuthGroup')->all(['status' => 'normal']);
  24. $objlist = [];
  25. foreach ($groups as $K => $v)
  26. {
  27. // 取出包含自己的所有子节点
  28. $childrenlist = Tree::instance()->init($grouplist)->getChildren($v['id'], TRUE);
  29. $obj = Tree::instance()->init($childrenlist)->getTreeArray($v['pid']);
  30. $objlist = array_merge($objlist, Tree::instance()->getTreeList($obj));
  31. }
  32. $groupdata = [];
  33. foreach ($objlist as $k => $v)
  34. {
  35. $groupdata[$v['id']] = $v['name'];
  36. }
  37. $this->childrenIds = array_keys($groupdata);
  38. $this->view->assign('groupdata', $groupdata);
  39. }
  40. /**
  41. * 添加
  42. */
  43. public function add()
  44. {
  45. if ($this->request->isPost())
  46. {
  47. $this->code = -1;
  48. $params = $this->request->post("row/a");
  49. if ($params)
  50. {
  51. $params['salt'] = Random::basic(4);
  52. $params['password'] = md5(md5($params['password']) . $params['salt']);
  53. $admin = $this->model->create($params);
  54. $group = $this->request->post("group/a");
  55. //过滤不允许的组别,避免越权
  56. $group = array_intersect($this->childrenIds, $group);
  57. $dataset = [];
  58. foreach ($group as $value)
  59. {
  60. $dataset[] = ['uid' => $admin->id, 'group_id' => $value];
  61. }
  62. model('AuthGroupAccess')->saveAll($dataset);
  63. $this->code = 1;
  64. }
  65. return;
  66. }
  67. return $this->view->fetch();
  68. }
  69. /**
  70. * 编辑
  71. */
  72. public function edit($ids = NULL)
  73. {
  74. $row = $this->model->get(['id' => $ids]);
  75. if (!$row)
  76. $this->error(__('No Results were found'));
  77. if ($this->request->isPost())
  78. {
  79. $this->code = -1;
  80. $params = $this->request->post("row/a");
  81. if ($params)
  82. {
  83. if ($params['password'])
  84. {
  85. $params['salt'] = Random::basic(4);
  86. $params['password'] = md5(md5($params['password']) . $params['salt']);
  87. }
  88. $row->save($params);
  89. // 先移除所有权限
  90. model('AuthGroupAccess')->where('uid', $row->id)->delete();
  91. $group = $this->request->post("group/a");
  92. // 过滤不允许的组别,避免越权
  93. $group = array_intersect($this->childrenIds, $group);
  94. $dataset = [];
  95. foreach ($group as $value)
  96. {
  97. $dataset[] = ['uid' => $row->id, 'group_id' => $value];
  98. }
  99. model('AuthGroupAccess')->saveAll($dataset);
  100. $this->code = 1;
  101. }
  102. return;
  103. }
  104. $grouplist = $this->auth->getGroups($row['id']);
  105. $groupids = [];
  106. foreach ($grouplist as $k => $v)
  107. {
  108. $groupids[] = $v['id'];
  109. }
  110. $this->view->assign("row", $row);
  111. $this->view->assign("groupids", $groupids);
  112. return $this->view->fetch();
  113. }
  114. /**
  115. * 删除
  116. */
  117. public function del($ids = "")
  118. {
  119. $this->code = -1;
  120. if ($ids)
  121. {
  122. $count = $this->model->where('id', 'in', $ids)->delete();
  123. if ($count)
  124. {
  125. model('AuthGroupAccess')->where('uid', 'in', $ids)->delete();
  126. $this->code = 1;
  127. }
  128. }
  129. return;
  130. }
  131. }