Admin.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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();
  25. $groupName = AuthGroup::where('id', 'in', $this->childrenGroupIds)
  26. ->column('id,name');
  27. $this->view->assign('groupdata', $groupName);
  28. $this->assignconfig("admin", ['id' => $this->auth->id]);
  29. }
  30. /**
  31. * 查看
  32. */
  33. public function index()
  34. {
  35. if ($this->request->isAjax())
  36. {
  37. $childrenGroupIds = $this->auth->getChildrenAdminIds(true);
  38. $groupName = AuthGroup::where('id', 'in', $childrenGroupIds)
  39. ->column('id,name');
  40. $authGroupList = AuthGroupAccess::where('group_id', 'in', $childrenGroupIds)
  41. ->field('uid,group_id')
  42. ->select();
  43. $adminGroupName = [];
  44. foreach ($authGroupList as $k => $v)
  45. {
  46. if (isset($groupName[$v['group_id']]))
  47. $adminGroupName[$v['uid']][$v['group_id']] = $groupName[$v['group_id']];
  48. }
  49. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  50. $total = $this->model
  51. ->where($where)
  52. ->where('id', 'in', $this->childrenAdminIds)
  53. ->order($sort, $order)
  54. ->count();
  55. $list = $this->model
  56. ->where($where)
  57. ->where('id', 'in', $this->childrenAdminIds)
  58. ->field(['password', 'salt', 'token'], true)
  59. ->order($sort, $order)
  60. ->limit($offset, $limit)
  61. ->select();
  62. foreach ($list as $k => &$v)
  63. {
  64. $groups = isset($adminGroupName[$v['id']]) ? $adminGroupName[$v['id']] : [];
  65. $v['groups'] = implode(',', array_keys($groups));
  66. $v['groups_text'] = implode(',', array_values($groups));
  67. }
  68. unset($v);
  69. $result = array("total" => $total, "rows" => $list);
  70. return json($result);
  71. }
  72. return $this->view->fetch();
  73. }
  74. /**
  75. * 添加
  76. */
  77. public function add()
  78. {
  79. if ($this->request->isPost())
  80. {
  81. $params = $this->request->post("row/a");
  82. if ($params)
  83. {
  84. $params['salt'] = Random::alnum();
  85. $params['password'] = md5(md5($params['password']) . $params['salt']);
  86. $params['avatar'] = '/assets/img/avatar.png'; //设置新管理员默认头像。
  87. $admin = $this->model->create($params);
  88. $group = $this->request->post("group/a");
  89. //过滤不允许的组别,避免越权
  90. $group = array_intersect($this->childrenGroupIds, $group);
  91. $dataset = [];
  92. foreach ($group as $value)
  93. {
  94. $dataset[] = ['uid' => $admin->id, 'group_id' => $value];
  95. }
  96. model('AuthGroupAccess')->saveAll($dataset);
  97. $this->success();
  98. }
  99. $this->error();
  100. }
  101. return $this->view->fetch();
  102. }
  103. /**
  104. * 编辑
  105. */
  106. public function edit($ids = NULL)
  107. {
  108. $row = $this->model->get(['id' => $ids]);
  109. if (!$row)
  110. $this->error(__('No Results were found'));
  111. if ($this->request->isPost())
  112. {
  113. $params = $this->request->post("row/a");
  114. if ($params)
  115. {
  116. if ($params['password'])
  117. {
  118. $params['salt'] = Random::alnum();
  119. $params['password'] = md5(md5($params['password']) . $params['salt']);
  120. }
  121. else
  122. {
  123. unset($params['password'], $params['salt']);
  124. }
  125. $row->save($params);
  126. // 先移除所有权限
  127. model('AuthGroupAccess')->where('uid', $row->id)->delete();
  128. $group = $this->request->post("group/a");
  129. // 过滤不允许的组别,避免越权
  130. $group = array_intersect($this->childrenGroupIds, $group);
  131. $dataset = [];
  132. foreach ($group as $value)
  133. {
  134. $dataset[] = ['uid' => $row->id, 'group_id' => $value];
  135. }
  136. model('AuthGroupAccess')->saveAll($dataset);
  137. $this->success();
  138. }
  139. $this->error();
  140. }
  141. $grouplist = $this->auth->getGroups($row['id']);
  142. $groupids = [];
  143. foreach ($grouplist as $k => $v)
  144. {
  145. $groupids[] = $v['id'];
  146. }
  147. $this->view->assign("row", $row);
  148. $this->view->assign("groupids", $groupids);
  149. return $this->view->fetch();
  150. }
  151. /**
  152. * 删除
  153. */
  154. public function del($ids = "")
  155. {
  156. if ($ids)
  157. {
  158. // 避免越权删除管理员
  159. $childrenGroupIds = $this->childrenGroupIds;
  160. $adminList = $this->model->where('id', 'in', $ids)->where('id', 'in', function($query) use($childrenGroupIds) {
  161. $query->name('auth_group_access')->where('group_id', 'in', $childrenGroupIds)->field('uid');
  162. })->select();
  163. if ($adminList)
  164. {
  165. $deleteIds = [];
  166. foreach ($adminList as $k => $v)
  167. {
  168. $deleteIds[] = $v->id;
  169. }
  170. $deleteIds = array_diff($deleteIds, [$this->auth->id]);
  171. if ($deleteIds)
  172. {
  173. $this->model->destroy($deleteIds);
  174. model('AuthGroupAccess')->where('uid', 'in', $deleteIds)->delete();
  175. $this->success();
  176. }
  177. }
  178. }
  179. $this->error();
  180. }
  181. /**
  182. * 批量更新
  183. * @internal
  184. */
  185. public function multi($ids = "")
  186. {
  187. // 管理员禁止批量操作
  188. $this->error();
  189. }
  190. }