Admin.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. namespace app\admin\controller\auth;
  3. use app\admin\model\AuthGroup;
  4. use app\admin\model\AuthGroupAccess;
  5. use app\common\controller\Backend;
  6. use fast\Random;
  7. use fast\Tree;
  8. /**
  9. * 管理员管理
  10. *
  11. * @icon fa fa-users
  12. * @remark 一个管理员可以有多个角色组,左侧的菜单根据管理员所拥有的权限进行生成
  13. */
  14. class Admin extends Backend
  15. {
  16. protected $model = null;
  17. protected $childrenGroupIds = [];
  18. protected $childrenAdminIds = [];
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. $this->model = model('Admin');
  23. $this->childrenAdminIds = $this->auth->getChildrenAdminIds(true);
  24. $this->childrenGroupIds = $this->auth->getChildrenGroupIds($this->auth->isSuperAdmin() ? true : false);
  25. $groupList = collection(AuthGroup::where('id', 'in', $this->childrenGroupIds)->select())->toArray();
  26. $groupIds = $this->auth->getGroupIds();
  27. Tree::instance()->init($groupList);
  28. $result = [];
  29. if ($this->auth->isSuperAdmin())
  30. {
  31. $result = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0));
  32. }
  33. else
  34. {
  35. foreach ($groupIds as $m => $n)
  36. {
  37. $result = array_merge($result, Tree::instance()->getTreeList(Tree::instance()->getTreeArray($n)));
  38. }
  39. }
  40. $groupName = [];
  41. foreach ($result as $k => $v)
  42. {
  43. $groupName[$v['id']] = $v['name'];
  44. }
  45. $this->view->assign('groupdata', $groupName);
  46. $this->assignconfig("admin", ['id' => $this->auth->id]);
  47. }
  48. /**
  49. * 查看
  50. */
  51. public function index()
  52. {
  53. if ($this->request->isAjax())
  54. {
  55. $childrenGroupIds = $this->auth->getChildrenAdminIds(true);
  56. $groupName = AuthGroup::where('id', 'in', $childrenGroupIds)
  57. ->column('id,name');
  58. $authGroupList = AuthGroupAccess::where('group_id', 'in', $childrenGroupIds)
  59. ->field('uid,group_id')
  60. ->select();
  61. $adminGroupName = [];
  62. foreach ($authGroupList as $k => $v)
  63. {
  64. if (isset($groupName[$v['group_id']]))
  65. $adminGroupName[$v['uid']][$v['group_id']] = $groupName[$v['group_id']];
  66. }
  67. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  68. $total = $this->model
  69. ->where($where)
  70. ->where('id', 'in', $this->childrenAdminIds)
  71. ->order($sort, $order)
  72. ->count();
  73. $list = $this->model
  74. ->where($where)
  75. ->where('id', 'in', $this->childrenAdminIds)
  76. ->field(['password', 'salt', 'token'], true)
  77. ->order($sort, $order)
  78. ->limit($offset, $limit)
  79. ->select();
  80. foreach ($list as $k => &$v)
  81. {
  82. $groups = isset($adminGroupName[$v['id']]) ? $adminGroupName[$v['id']] : [];
  83. $v['groups'] = implode(',', array_keys($groups));
  84. $v['groups_text'] = implode(',', array_values($groups));
  85. }
  86. unset($v);
  87. $result = array("total" => $total, "rows" => $list);
  88. return json($result);
  89. }
  90. return $this->view->fetch();
  91. }
  92. /**
  93. * 添加
  94. */
  95. public function add()
  96. {
  97. if ($this->request->isPost())
  98. {
  99. $params = $this->request->post("row/a");
  100. if ($params)
  101. {
  102. $params['salt'] = Random::alnum();
  103. $params['password'] = md5(md5($params['password']) . $params['salt']);
  104. $params['avatar'] = '/assets/img/avatar.png'; //设置新管理员默认头像。
  105. $admin = $this->model->create($params);
  106. $group = $this->request->post("group/a");
  107. //过滤不允许的组别,避免越权
  108. $group = array_intersect($this->childrenGroupIds, $group);
  109. $dataset = [];
  110. foreach ($group as $value)
  111. {
  112. $dataset[] = ['uid' => $admin->id, 'group_id' => $value];
  113. }
  114. model('AuthGroupAccess')->saveAll($dataset);
  115. $this->success();
  116. }
  117. $this->error();
  118. }
  119. return $this->view->fetch();
  120. }
  121. /**
  122. * 编辑
  123. */
  124. public function edit($ids = NULL)
  125. {
  126. $row = $this->model->get(['id' => $ids]);
  127. if (!$row)
  128. $this->error(__('No Results were found'));
  129. if ($this->request->isPost())
  130. {
  131. $params = $this->request->post("row/a");
  132. if ($params)
  133. {
  134. if ($params['password'])
  135. {
  136. $params['salt'] = Random::alnum();
  137. $params['password'] = md5(md5($params['password']) . $params['salt']);
  138. }
  139. else
  140. {
  141. unset($params['password'], $params['salt']);
  142. }
  143. $row->save($params);
  144. // 先移除所有权限
  145. model('AuthGroupAccess')->where('uid', $row->id)->delete();
  146. $group = $this->request->post("group/a");
  147. // 过滤不允许的组别,避免越权
  148. $group = array_intersect($this->childrenGroupIds, $group);
  149. $dataset = [];
  150. foreach ($group as $value)
  151. {
  152. $dataset[] = ['uid' => $row->id, 'group_id' => $value];
  153. }
  154. model('AuthGroupAccess')->saveAll($dataset);
  155. $this->success();
  156. }
  157. $this->error();
  158. }
  159. $grouplist = $this->auth->getGroups($row['id']);
  160. $groupids = [];
  161. foreach ($grouplist as $k => $v)
  162. {
  163. $groupids[] = $v['id'];
  164. }
  165. $this->view->assign("row", $row);
  166. $this->view->assign("groupids", $groupids);
  167. return $this->view->fetch();
  168. }
  169. /**
  170. * 删除
  171. */
  172. public function del($ids = "")
  173. {
  174. if ($ids)
  175. {
  176. // 避免越权删除管理员
  177. $childrenGroupIds = $this->childrenGroupIds;
  178. $adminList = $this->model->where('id', 'in', $ids)->where('id', 'in', function($query) use($childrenGroupIds) {
  179. $query->name('auth_group_access')->where('group_id', 'in', $childrenGroupIds)->field('uid');
  180. })->select();
  181. if ($adminList)
  182. {
  183. $deleteIds = [];
  184. foreach ($adminList as $k => $v)
  185. {
  186. $deleteIds[] = $v->id;
  187. }
  188. $deleteIds = array_diff($deleteIds, [$this->auth->id]);
  189. if ($deleteIds)
  190. {
  191. $this->model->destroy($deleteIds);
  192. model('AuthGroupAccess')->where('uid', 'in', $deleteIds)->delete();
  193. $this->success();
  194. }
  195. }
  196. }
  197. $this->error();
  198. }
  199. /**
  200. * 批量更新
  201. * @internal
  202. */
  203. public function multi($ids = "")
  204. {
  205. // 管理员禁止批量操作
  206. $this->error();
  207. }
  208. }