Admin.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <?php
  2. namespace app\company\controller\auth;
  3. use app\company\model\AuthGroup;
  4. use app\company\model\AuthGroupAccess;
  5. use app\common\controller\Apic;
  6. use fast\Random;
  7. use fast\Tree;
  8. use think\Db;
  9. use think\Validate;
  10. /**
  11. * 管理员管理
  12. *
  13. * @icon fa fa-users
  14. * @remark 一个管理员可以有多个角色组,左侧的菜单根据管理员所拥有的权限进行生成
  15. */
  16. class Admin extends Apic
  17. {
  18. /**
  19. * @var \app\company\model\Admin
  20. */
  21. protected $model = null;
  22. protected $selectpageFields = 'id,username,nickname,avatar';
  23. protected $searchFields = 'id,username,nickname';
  24. protected $childrenGroupIds = [];
  25. protected $childrenAdminIds = [];
  26. public function _initialize()
  27. {
  28. parent::_initialize();
  29. $this->model = model('Admin');
  30. $this->childrenAdminIds = $this->auth->getChildrenAdminIds($this->auth->isSuperAdmin());//下级管理员
  31. $this->childrenGroupIds = $this->auth->getChildrenGroupIds($this->auth->isSuperAdmin());//下级角色组
  32. /*dump($this->childrenAdminIds);
  33. dump($this->childrenGroupIds);
  34. exit;*/
  35. $groupList = collection(AuthGroup::where('id', 'in', $this->childrenGroupIds)->select())->toArray();
  36. Tree::instance()->init($groupList);
  37. $groupdata = [];
  38. if ($this->auth->isSuperAdmin()) {
  39. $result = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0));
  40. foreach ($result as $k => $v) {
  41. $groupdata[$v['id']] = $v['name'];
  42. }
  43. } else {
  44. $result = [];
  45. $groups = $this->auth->getGroups();
  46. foreach ($groups as $m => $n) {
  47. $childlist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray($n['id']));
  48. $temp = [];
  49. foreach ($childlist as $k => $v) {
  50. $temp[$v['id']] = $v['name'];
  51. }
  52. $result[__($n['name'])] = $temp;
  53. }
  54. $groupdata = $result;
  55. }
  56. /*$this->view->assign('groupdata', $groupdata);
  57. $this->assignconfig("admin", ['id' => $this->auth->id]);*/
  58. }
  59. /**
  60. * 查看
  61. */
  62. public function index()
  63. {
  64. $childrenGroupIds = $this->childrenGroupIds;
  65. $groupName = AuthGroup::where('id', 'in', $childrenGroupIds)
  66. ->column('id,name');
  67. $authGroupList = AuthGroupAccess::where('group_id', 'in', $childrenGroupIds)
  68. ->field('uid,group_id')
  69. ->select();
  70. $adminGroupName = [];
  71. foreach ($authGroupList as $k => $v) {
  72. if (isset($groupName[$v['group_id']])) {
  73. $adminGroupName[$v['uid']][$v['group_id']] = $groupName[$v['group_id']];
  74. }
  75. }
  76. $groups = $this->auth->getGroups();
  77. foreach ($groups as $m => $n) {
  78. $adminGroupName[$this->auth->id][$n['id']] = $n['name'];
  79. }
  80. $list = Db::name('pc_admin')
  81. ->where('company_id',$this->auth->company_id) //多此一举
  82. ->where('id', 'in', $this->childrenAdminIds)
  83. ->field(['password', 'salt', 'token'], true)
  84. ->order('id', 'asc')
  85. ->paginate();
  86. foreach ($list as $k => &$v) {
  87. $v['avatar'] = localpath_to_netpath($v['avatar']);
  88. $groups = isset($adminGroupName[$v['id']]) ? $adminGroupName[$v['id']] : [];
  89. $v['groups'] = implode(',', array_keys($groups));
  90. $v['groups_text'] = implode(',', array_values($groups));
  91. }
  92. unset($v);
  93. $result = array("total" => $list->total(), "list" => $list->items());
  94. $this->success(1,$result);
  95. }
  96. /**
  97. * 添加
  98. */
  99. public function add()
  100. {
  101. $params = [
  102. 'username' => input('username',''),//手机号
  103. 'nickname' => input('nickname',''),//姓名
  104. 'password' => input('password',''),//密码
  105. 'gonghao' => input('gonghao',''), //工号
  106. ];
  107. $group_id = input('group_id',0);
  108. if(empty($group_id)){
  109. $this->error();
  110. }
  111. Db::startTrans();
  112. try {
  113. if (!Validate::is($params['password'], '\S{6,30}')) {
  114. exception(__("Please input correct password"));
  115. }
  116. $params['mobile'] = $params['username'];
  117. $params['company_id'] = $this->auth->company_id;
  118. $params['salt'] = Random::alnum();
  119. $params['password'] = $this->auth->getEncryptPassword($params['password'], $params['salt']);
  120. $params['avatar'] = '/assets/img/avatar.png'; //设置新管理员默认头像。
  121. $result = $this->model->validate('Admin.add')->save($params);
  122. if ($result === false) {
  123. exception($this->model->getError());
  124. }
  125. $group = [$group_id];
  126. //过滤不允许的组别,避免越权
  127. $group = array_intersect($this->childrenGroupIds, $group);
  128. if (!$group) {
  129. exception(__('The parent group exceeds permission limit'));
  130. }
  131. $dataset = [];
  132. foreach ($group as $value) {
  133. $dataset[] = ['uid' => $this->model->id, 'group_id' => $value];
  134. }
  135. model('AuthGroupAccess')->saveAll($dataset);
  136. Db::commit();
  137. } catch (\Exception $e) {
  138. Db::rollback();
  139. $this->error($e->getMessage());
  140. }
  141. $this->success();
  142. }
  143. public function info(){
  144. $ids = input('id',0);
  145. $row = $this->model->get(['id' => $ids]);
  146. if (!$row) {
  147. $this->error(__('No Results were found'));
  148. }
  149. if (!in_array($row->id, $this->childrenAdminIds)) {
  150. $this->error(__('You have no permission'));
  151. }
  152. $grouplist = $this->auth->getGroups($row['id']);
  153. $groupids = [];
  154. foreach ($grouplist as $k => $v) {
  155. $groupids = $v['id'];
  156. }
  157. $row['groupids'] = $groupids;
  158. unset($row['password']);
  159. unset($row['salt']);
  160. $this->success(1,$row);
  161. }
  162. /**
  163. * 编辑
  164. */
  165. public function edit()
  166. {
  167. $ids = input('id',0);
  168. $row = $this->model->get(['id' => $ids]);
  169. if (!$row) {
  170. $this->error(__('No Results were found'));
  171. }
  172. if (!in_array($row->id, $this->childrenAdminIds)) {
  173. $this->error(__('You have no permission'));
  174. }
  175. $params = [
  176. 'username' => input('username',''),//手机号
  177. 'nickname' => input('nickname',''),//姓名
  178. 'password' => input('password',''),//密码
  179. 'gonghao' => input('gonghao',''), //工号
  180. ];
  181. $group_id = input('group_id',0);
  182. if(empty($group_id)){
  183. $this->error();
  184. }
  185. Db::startTrans();
  186. try {
  187. if ($params['password']) {
  188. if (!Validate::is($params['password'], '\S{6,30}')) {
  189. exception(__("Please input correct password"));
  190. }
  191. $params['salt'] = Random::alnum();
  192. $params['password'] = $this->auth->getEncryptPassword($params['password'], $params['salt']);
  193. } else {
  194. unset($params['password'], $params['salt']);
  195. }
  196. $params['mobile'] = $params['username'];
  197. //这里需要针对username和email做唯一验证
  198. $adminValidate = \think\Loader::validate('Admin');
  199. $adminValidate->rule([
  200. 'mobile' => 'require|regex:1\d{10}|unique:PcAdmin,mobile,' . $row->id,
  201. 'username' => 'require|regex:1\d{10}|unique:PcAdmin,username,' . $row->id,
  202. 'password' => 'regex:\S{32}',
  203. ]);
  204. $result = $row->validate('Admin.edit')->save($params);
  205. if ($result === false) {
  206. exception($row->getError());
  207. }
  208. // 先移除所有权限
  209. model('AuthGroupAccess')->where('uid', $row->id)->delete();
  210. $group = [$group_id];
  211. // 过滤不允许的组别,避免越权
  212. $group = array_intersect($this->childrenGroupIds, $group);
  213. if (!$group) {
  214. exception(__('The parent group exceeds permission limit'));
  215. }
  216. $dataset = [];
  217. foreach ($group as $value) {
  218. $dataset[] = ['uid' => $row->id, 'group_id' => $value];
  219. }
  220. model('AuthGroupAccess')->saveAll($dataset);
  221. Db::commit();
  222. } catch (\Exception $e) {
  223. Db::rollback();
  224. $this->error($e->getMessage());
  225. }
  226. $this->success();
  227. }
  228. /**
  229. * 删除
  230. */
  231. public function del()
  232. {
  233. $ids = input('ids','');
  234. if ($ids) {
  235. $ids = array_intersect($this->childrenAdminIds, array_filter(explode(',', $ids)));
  236. // 避免越权删除管理员
  237. $childrenGroupIds = $this->childrenGroupIds;
  238. $adminList = $this->model->where('id', 'in', $ids)->where('id', 'in', function ($query) use ($childrenGroupIds) {
  239. $query->name('pc_auth_group_access')->where('group_id', 'in', $childrenGroupIds)->field('uid');
  240. })->select();
  241. if ($adminList) {
  242. $deleteIds = [];
  243. foreach ($adminList as $k => $v) {
  244. $deleteIds[] = $v->id;
  245. }
  246. $deleteIds = array_values(array_diff($deleteIds, [$this->auth->id]));
  247. if ($deleteIds) {
  248. Db::startTrans();
  249. try {
  250. $this->model->destroy($deleteIds);
  251. model('AuthGroupAccess')->where('uid', 'in', $deleteIds)->delete();
  252. Db::commit();
  253. } catch (\Exception $e) {
  254. Db::rollback();
  255. $this->error($e->getMessage());
  256. }
  257. $this->success();
  258. }
  259. $this->error(__('No rows were deleted'));
  260. }
  261. }
  262. $this->error(__('You have no permission'));
  263. }
  264. /**
  265. * 下拉搜索
  266. */
  267. public function selectpage()
  268. {
  269. $this->dataLimit = 'auth';
  270. $this->dataLimitField = 'id';
  271. return parent::selectpage();
  272. }
  273. }