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. $list2 = $list->items();
  87. foreach ($list2 as $k => &$v) {
  88. $v['avatar'] = localpath_to_netpath($v['avatar']);
  89. $groups = isset($adminGroupName[$v['id']]) ? $adminGroupName[$v['id']] : [];
  90. $v['groups'] = implode(',', array_keys($groups));
  91. $v['groups_text'] = implode(',', array_values($groups));
  92. }
  93. unset($v);
  94. $result = array("total" => $list->total(), "list" => $list2);
  95. $this->success(1,$result);
  96. }
  97. /**
  98. * 添加
  99. */
  100. public function add()
  101. {
  102. $params = [
  103. 'username' => input('username',''),//手机号
  104. 'nickname' => input('nickname',''),//姓名
  105. 'password' => input('password',''),//密码
  106. 'gonghao' => input('gonghao',''), //工号
  107. 'avatar' => input('avatar',''), //头像
  108. ];
  109. $group_id = input('group_id',0);
  110. if(empty($group_id)){
  111. $this->error();
  112. }
  113. Db::startTrans();
  114. try {
  115. if (!Validate::is($params['password'], '\S{6,30}')) {
  116. exception(__("Please input correct password"));
  117. }
  118. $params['mobile'] = $params['username'];
  119. $params['company_id'] = $this->auth->company_id;
  120. $params['salt'] = Random::alnum();
  121. $params['password'] = $this->auth->getEncryptPassword($params['password'], $params['salt']);
  122. // $params['avatar'] = '/assets/img/avatar.png'; //设置新管理员默认头像。
  123. $result = $this->model->validate('Admin.add')->save($params);
  124. if ($result === false) {
  125. exception($this->model->getError());
  126. }
  127. $group = [$group_id];
  128. //过滤不允许的组别,避免越权
  129. $group = array_intersect($this->childrenGroupIds, $group);
  130. if (!$group) {
  131. exception(__('The parent group exceeds permission limit'));
  132. }
  133. $dataset = [];
  134. foreach ($group as $value) {
  135. $dataset[] = ['uid' => $this->model->id, 'group_id' => $value];
  136. }
  137. model('AuthGroupAccess')->saveAll($dataset);
  138. Db::commit();
  139. } catch (\Exception $e) {
  140. Db::rollback();
  141. $this->error($e->getMessage());
  142. }
  143. $this->success();
  144. }
  145. public function info(){
  146. $ids = input('id',0);
  147. $row = $this->model->get(['id' => $ids]);
  148. if (!$row) {
  149. $this->error(__('No Results were found'));
  150. }
  151. if (!in_array($row->id, $this->childrenAdminIds)) {
  152. $this->error(__('You have no permission'));
  153. }
  154. $grouplist = $this->auth->getGroups($row['id']);
  155. $groupids = [];
  156. foreach ($grouplist as $k => $v) {
  157. $groupids = $v['id'];
  158. }
  159. $row['groupids'] = $groupids;
  160. unset($row['password']);
  161. unset($row['salt']);
  162. $this->success(1,$row);
  163. }
  164. /**
  165. * 编辑
  166. */
  167. public function edit()
  168. {
  169. $ids = input('id',0);
  170. $row = $this->model->get(['id' => $ids]);
  171. if (!$row) {
  172. $this->error(__('No Results were found'));
  173. }
  174. if (!in_array($row->id, $this->childrenAdminIds)) {
  175. $this->error(__('You have no permission'));
  176. }
  177. $params = [
  178. 'username' => input('username',''),//手机号
  179. 'nickname' => input('nickname',''),//姓名
  180. 'password' => input('password',''),//密码
  181. 'gonghao' => input('gonghao',''), //工号
  182. ];
  183. $group_id = input('group_id',0);
  184. if(empty($group_id)){
  185. $this->error();
  186. }
  187. Db::startTrans();
  188. try {
  189. if ($params['password']) {
  190. if (!Validate::is($params['password'], '\S{6,30}')) {
  191. exception(__("Please input correct password"));
  192. }
  193. $params['salt'] = Random::alnum();
  194. $params['password'] = $this->auth->getEncryptPassword($params['password'], $params['salt']);
  195. } else {
  196. unset($params['password'], $params['salt']);
  197. }
  198. $params['mobile'] = $params['username'];
  199. //这里需要针对username和email做唯一验证
  200. $adminValidate = \think\Loader::validate('Admin');
  201. $adminValidate->rule([
  202. 'mobile' => 'require|regex:1\d{10}|unique:PcAdmin,mobile,' . $row->id,
  203. 'username' => 'require|regex:1\d{10}|unique:PcAdmin,username,' . $row->id,
  204. 'password' => 'regex:\S{32}',
  205. ]);
  206. $result = $row->validate('Admin.edit')->save($params);
  207. if ($result === false) {
  208. exception($row->getError());
  209. }
  210. // 先移除所有权限
  211. model('AuthGroupAccess')->where('uid', $row->id)->delete();
  212. $group = [$group_id];
  213. // 过滤不允许的组别,避免越权
  214. $group = array_intersect($this->childrenGroupIds, $group);
  215. if (!$group) {
  216. exception(__('The parent group exceeds permission limit'));
  217. }
  218. $dataset = [];
  219. foreach ($group as $value) {
  220. $dataset[] = ['uid' => $row->id, 'group_id' => $value];
  221. }
  222. model('AuthGroupAccess')->saveAll($dataset);
  223. Db::commit();
  224. } catch (\Exception $e) {
  225. Db::rollback();
  226. $this->error($e->getMessage());
  227. }
  228. $this->success();
  229. }
  230. /**
  231. * 删除
  232. */
  233. public function del()
  234. {
  235. $ids = input('ids','');
  236. if ($ids) {
  237. $ids = array_intersect($this->childrenAdminIds, array_filter(explode(',', $ids)));
  238. // 避免越权删除管理员
  239. $childrenGroupIds = $this->childrenGroupIds;
  240. $adminList = $this->model->where('id', 'in', $ids)->where('id', 'in', function ($query) use ($childrenGroupIds) {
  241. $query->name('pc_auth_group_access')->where('group_id', 'in', $childrenGroupIds)->field('uid');
  242. })->select();
  243. if ($adminList) {
  244. $deleteIds = [];
  245. foreach ($adminList as $k => $v) {
  246. $deleteIds[] = $v->id;
  247. }
  248. $deleteIds = array_values(array_diff($deleteIds, [$this->auth->id]));
  249. if ($deleteIds) {
  250. Db::startTrans();
  251. try {
  252. $this->model->destroy($deleteIds);
  253. model('AuthGroupAccess')->where('uid', 'in', $deleteIds)->delete();
  254. Db::commit();
  255. } catch (\Exception $e) {
  256. Db::rollback();
  257. $this->error($e->getMessage());
  258. }
  259. $this->success();
  260. }
  261. $this->error(__('No rows were deleted'));
  262. }
  263. }
  264. $this->error(__('You have no permission'));
  265. }
  266. /**
  267. * 下拉搜索
  268. */
  269. public function selectpage()
  270. {
  271. $this->dataLimit = 'auth';
  272. $this->dataLimitField = 'id';
  273. return parent::selectpage();
  274. }
  275. }