Admin.php 5.2 KB

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