No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Manager.php 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Yzncms [ 御宅男工作室 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2007 http://yzncms.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 御宅男 <530765310@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\controller\auth;
  12. use app\admin\model\AdminUser as Admin_User;
  13. use app\admin\model\AuthGroup as AuthGroupModel;
  14. use app\common\controller\Adminbase;
  15. use util\Tree;
  16. /**
  17. * 管理员管理
  18. */
  19. class Manager extends Adminbase
  20. {
  21. protected $searchFields = 'id,username,nickname';
  22. protected $childrenGroupIds = [];
  23. protected $childrenAdminIds = [];
  24. protected function initialize()
  25. {
  26. parent::initialize();
  27. $this->modelClass = new Admin_User;
  28. $this->childrenAdminIds = $this->auth->getChildrenAdminIds(true);
  29. $this->childrenGroupIds = $this->auth->getChildrenGroupIds(true);
  30. $groupList = AuthGroupModel::where('id', 'in', $this->childrenGroupIds)->select()->toArray();
  31. Tree::instance()->init($groupList);
  32. $groupdata = [];
  33. if ($this->auth->isAdministrator()) {
  34. $result = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'title');
  35. foreach ($result as $k => $v) {
  36. $groupdata[$v['id']] = $v['title'];
  37. }
  38. } else {
  39. $result = [];
  40. $groups = $this->auth->getGroups();
  41. foreach ($groups as $m => $n) {
  42. $childlist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray($n['id']), 'title');
  43. //$temp = [];
  44. foreach ($childlist as $k => $v) {
  45. $groupdata[$v['id']] = $v['title'];
  46. }
  47. //$result[$n['title']] = $temp;
  48. }
  49. //$groupdata = $result;
  50. }
  51. $this->assign('groupdata', $groupdata);
  52. }
  53. /**
  54. * 管理员管理列表
  55. */
  56. public function index()
  57. {
  58. if ($this->request->isAjax()) {
  59. //如果发送的来源是Selectpage,则转发到Selectpage
  60. if ($this->request->request('keyField')) {
  61. return $this->selectpage();
  62. }
  63. list($page, $limit, $where, $sort, $order) = $this->buildTableParames();
  64. $childrenGroupIds = $this->childrenGroupIds;
  65. $groupName = AuthGroupModel::where('id', 'in', $childrenGroupIds)
  66. ->column('id,title');
  67. $list = $this->modelClass
  68. ->where($where)
  69. ->where('id', 'in', $this->childrenAdminIds)
  70. ->field(['password', 'salt', 'token'], true)
  71. ->order($sort, $order)
  72. ->paginate($limit);
  73. foreach ($list as $k => &$v) {
  74. $v['groups'] = $groupName[$v['roleid']] ?? '未知';
  75. }
  76. unset($v);
  77. $result = ["code" => 0, 'count' => $list->total(), "data" => $list->items()];
  78. return json($result);
  79. }
  80. return $this->fetch();
  81. }
  82. /**
  83. * 添加管理员
  84. */
  85. public function add()
  86. {
  87. if ($this->request->isPost()) {
  88. $this->token();
  89. $params = $this->request->post('');
  90. $result = $this->validate($params, 'AdminUser.insert');
  91. $passwordinfo = encrypt_password($params['password']); //对密码进行处理
  92. $params['password'] = $passwordinfo['password'];
  93. $params['encrypt'] = $passwordinfo['encrypt'];
  94. if (true !== $result) {
  95. return $this->error($result);
  96. }
  97. if (!in_array($params['roleid'], $this->childrenGroupIds)) {
  98. $this->error('没有权限操作!');
  99. }
  100. try {
  101. $this->modelClass->save($params);
  102. } catch (\Exception $e) {
  103. $this->error($e->getMessage());
  104. }
  105. $this->success("添加成功!", url('index'));
  106. }
  107. return $this->fetch();
  108. }
  109. /**
  110. * 管理员编辑
  111. */
  112. public function edit()
  113. {
  114. $id = $this->request->param('id/d', 0);
  115. $row = $this->modelClass->get($id);
  116. if (!$row) {
  117. $this->error('记录未找到');
  118. }
  119. if (!in_array($row->id, $this->childrenAdminIds)) {
  120. $this->error('没有权限操作!');
  121. }
  122. if ($this->request->isPost()) {
  123. $this->token();
  124. $params = $this->request->post('');
  125. $result = $this->validate($params, 'AdminUser.update');
  126. if (true !== $result) {
  127. return $this->error($result);
  128. }
  129. if (!in_array($params['roleid'], $this->childrenGroupIds)) {
  130. $this->error('没有权限操作!');
  131. }
  132. //密码为空,表示不修改密码
  133. if (isset($params['password']) && $params['password']) {
  134. $passwordinfo = encrypt_password($params['password']); //对密码进行处理
  135. $params['encrypt'] = $passwordinfo['encrypt'];
  136. $params['password'] = $passwordinfo['password'];
  137. } else {
  138. unset($params['password'], $params['encrypt']);
  139. }
  140. try {
  141. $row->allowField(true)->save($params);
  142. } catch (\Exception $e) {
  143. $this->error($e->getMessage());
  144. }
  145. $this->success("修改成功!");
  146. }
  147. $this->assign("data", $row);
  148. return $this->fetch();
  149. }
  150. /**
  151. * 管理员删除
  152. */
  153. public function del()
  154. {
  155. if (false === $this->request->isPost()) {
  156. $this->error('未知参数');
  157. }
  158. $id = $this->request->param('id/d');
  159. if (empty($id)) {
  160. $this->error('请指定需要删除的用户ID!');
  161. }
  162. if ($id == 1) {
  163. $this->error('禁止对超级管理员执行该操作!');
  164. }
  165. $ids = array_intersect($this->childrenAdminIds, array_filter(explode(',', $id)));
  166. $adminList = $this->modelClass->where('id', 'in', $ids)->where('roleid', 'in', $this->childrenGroupIds)->select();
  167. if ($adminList) {
  168. $deleteIds = [];
  169. foreach ($adminList as $k => $v) {
  170. $deleteIds[] = $v->id;
  171. }
  172. $deleteIds = array_values(array_diff($deleteIds, [$this->auth->id]));
  173. if ($deleteIds) {
  174. try {
  175. $this->modelClass->destroy($deleteIds);
  176. } catch (\Exception $e) {
  177. $this->error($e->getMessage());
  178. }
  179. $this->success("删除成功!");
  180. }
  181. }
  182. $this->error('没有权限删除!');
  183. }
  184. //批量更新.
  185. public function multi()
  186. {
  187. // 管理员禁止批量操作
  188. $this->error();
  189. }
  190. }