Admin.php 6.7 KB

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