Admin.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. <?php
  2. namespace app\admin\controller\auth;
  3. use app\admin\model\Depart;
  4. use app\admin\model\Institution;
  5. use ba\Random;
  6. use Throwable;
  7. use think\facade\Db;
  8. use app\common\controller\Backend;
  9. use app\admin\model\Admin as AdminModel;
  10. class Admin extends Backend
  11. {
  12. /**
  13. * 模型
  14. * @var object
  15. * @phpstan-var AdminModel
  16. */
  17. protected object $model;
  18. protected array|string $preExcludeFields = ['PASSWORD', 'SALT', 'LOGIN_FAILURE', 'LAST_LOGIN_TIME', 'LAST_LOGIN_IP'];
  19. protected array|string $quickSearchField = ['USERNAME', 'NICKNAME'];
  20. /**
  21. * 开启数据限制
  22. */
  23. protected string|int|bool $dataLimit = 'allAuthAndOthers';
  24. protected string $dataLimitField = 'id';
  25. public function initialize(): void
  26. {
  27. parent::initialize();
  28. $this->model = new AdminModel();
  29. }
  30. /**
  31. * 查看
  32. * @throws Throwable
  33. */
  34. public function index(): void
  35. {
  36. if ($this->request->param('select')) {
  37. $this->select();
  38. }
  39. list($where, $alias, $limit, $order) = $this->queryBuilder();
  40. $adminWhere = [];
  41. if(!empty($where))
  42. {
  43. foreach ($where as $k=>$v)
  44. {
  45. if($v[0] == 'admin.group_name_arr')
  46. {
  47. $adminWhere['group_id'] = $v[2];
  48. unset($where[$k]);
  49. }
  50. }
  51. }
  52. if(!empty($adminWhere))
  53. {
  54. $res = $this->model
  55. ->alias('admin')
  56. ->join(['admin_group_access'=>'access'],'access.uid=admin.id')
  57. ->withoutField('login_failure,password,salt')
  58. ->withJoin($this->withJoinTable, $this->withJoinType)
  59. ->alias($alias)
  60. ->where($where)
  61. ->where($adminWhere)
  62. ->order($order)
  63. ->fetchSql(true)
  64. ->paginate($limit);
  65. }else{
  66. $res = $this->model
  67. ->withoutField('login_failure,password,salt')
  68. ->withJoin($this->withJoinTable, $this->withJoinType)
  69. ->alias($alias)
  70. ->where($where)
  71. ->order($order)
  72. ->paginate($limit);
  73. }
  74. $list = $res->items();
  75. $list = (json_decode(json_encode($list), true));
  76. $arr = [];
  77. foreach ($list as $k=>$v) {
  78. foreach ($v as $kk => $vv) {
  79. $arr[$k][strtolower($kk)] = $vv;
  80. }
  81. }
  82. $this->success('', [
  83. 'list' =>$arr,
  84. 'total' => $res->total(),
  85. 'remark' => get_route_remark(),
  86. ]);
  87. }
  88. /**
  89. * 添加
  90. * @throws Throwable
  91. */
  92. public function add(): void
  93. {
  94. if ($this->request->isPost()) {
  95. $data = $this->request->post();
  96. if (!$data) {
  97. $this->error(__('Parameter %s can not be empty', ['']));
  98. }
  99. /**
  100. * 由于有密码字段-对方法进行重写
  101. * 数据验证
  102. */
  103. if ($this->modelValidate) {
  104. try {
  105. $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  106. $validate = new $validate();
  107. $validate->scene('add')->check($data);
  108. } catch (Throwable $e) {
  109. $this->error($e->getMessage());
  110. }
  111. }
  112. $salt = Random::build('alnum', 16);
  113. $passwd = encrypt_password($data['password'], $salt);
  114. $data = $this->excludeFields($data);
  115. if(!empty($data['institution_id']))
  116. {
  117. $data['institution'] = Institution::where('id',$data['institution_id'])->value('name');
  118. }
  119. if(!empty($data['depart_id']))
  120. {
  121. $data['depart'] = Depart::where('id',$data['depart_id'])->value('depart_name');
  122. }
  123. if(!empty($data['create_user_id']))
  124. {
  125. $data['create_user'] = $this->model->where('id',$data['create_user_id'])->value('nickname');
  126. }
  127. $result = false;
  128. if ($data['group_arr']) $this->checkGroupAuth($data['group_arr']);
  129. $this->model->startTrans();
  130. try {
  131. $data['salt'] = $salt;
  132. $data['password'] = $passwd;
  133. $result = $this->model->save($data);
  134. if ($data['group_arr']) {
  135. $groupAccess = [];
  136. foreach ($data['group_arr'] as $datum) {
  137. $groupAccess[] = [
  138. 'uid' => $this->model->id,
  139. 'group_id' => $datum,
  140. ];
  141. }
  142. Db::name('admin_group_access')->insertAll($groupAccess);
  143. }
  144. $this->model->commit();
  145. } catch (Throwable $e) {
  146. $this->model->rollback();
  147. $this->error($e->getMessage());
  148. }
  149. if ($result !== false) {
  150. $this->success(__('Added successfully'));
  151. } else {
  152. $this->error(__('No rows were added'));
  153. }
  154. }
  155. $this->error(__('Parameter error'));
  156. }
  157. /**
  158. * 编辑
  159. * @throws Throwable
  160. */
  161. public function edit($id = null): void
  162. {
  163. $row = $this->model->find($id);
  164. if (!$row) {
  165. $this->error(__('Record not found'));
  166. }
  167. $dataLimitAdminIds = $this->getDataLimitAdminIds();
  168. if ($dataLimitAdminIds && !in_array($row[$this->dataLimitField], $dataLimitAdminIds)) {
  169. $this->error(__('You have no permission'));
  170. }
  171. if ($this->request->isPost()) {
  172. $data = $this->request->post();
  173. if (!$data) {
  174. $this->error(__('Parameter %s can not be empty', ['']));
  175. }
  176. /**
  177. * 由于有密码字段-对方法进行重写
  178. * 数据验证
  179. */
  180. if ($this->modelValidate) {
  181. try {
  182. $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  183. $validate = new $validate();
  184. $validate->scene('edit')->check($data);
  185. } catch (Throwable $e) {
  186. $this->error($e->getMessage());
  187. }
  188. }
  189. if ($this->auth->id == $data['id'] && $data['status'] == '0') {
  190. $this->error(__('Please use another administrator account to disable the current account!'));
  191. }
  192. if (isset($data['password']) && $data['password']) {
  193. $this->model->resetPassword($data['id'], $data['password']);
  194. }
  195. $groupAccess = [];
  196. if ($data['group_arr']) {
  197. $checkGroups = [];
  198. foreach ($data['group_arr'] as $datum) {
  199. if (!in_array($datum, $row->group_arr)) {
  200. $checkGroups[] = $datum;
  201. }
  202. $groupAccess[] = [
  203. 'uid' => $id,
  204. 'group_id' => $datum,
  205. ];
  206. }
  207. $this->checkGroupAuth($checkGroups);
  208. }
  209. Db::name('admin_group_access')
  210. ->where('uid', $id)
  211. ->delete();
  212. $data = $this->excludeFields($data);
  213. $result = false;
  214. $this->model->startTrans();
  215. try {
  216. if(!empty($data['institution_id']))
  217. {
  218. $data['institution'] = Institution::where('id',$data['institution_id'])->value('name');
  219. }
  220. if(!empty($data['depart_id']))
  221. {
  222. $data['depart'] = Depart::where('id',$data['depart_id'])->value('depart_name');
  223. }
  224. if(!empty($data['create_user_id']))
  225. {
  226. $data['create_user'] = $this->model->where('id',$data['create_user_id'])->value('nickname');
  227. }
  228. $result = $row->save($data);
  229. if ($groupAccess) Db::name('admin_group_access')->insertAll($groupAccess);
  230. $this->model->commit();
  231. } catch (Throwable $e) {
  232. $this->model->rollback();
  233. $this->error($e->getMessage());
  234. }
  235. if ($result !== false) {
  236. $this->success(__('Update successful'));
  237. } else {
  238. $this->error(__('No rows updated'));
  239. }
  240. }
  241. unset($row['salt'], $row['login_failure']);
  242. $row['password'] = '';
  243. $this->success('', [
  244. 'row' => $row
  245. ]);
  246. }
  247. /**
  248. * 删除
  249. * @param null $ids
  250. * @throws Throwable
  251. */
  252. public function del($ids = null): void
  253. {
  254. if (!$this->request->isDelete() || !$ids) {
  255. $this->error(__('Parameter error'));
  256. }
  257. $where = [];
  258. $dataLimitAdminIds = $this->getDataLimitAdminIds();
  259. if ($dataLimitAdminIds) {
  260. $where[] = [$this->dataLimitField, 'in', $dataLimitAdminIds];
  261. }
  262. $pk = $this->model->getPk();
  263. $where[] = [$pk, 'in', $ids];
  264. $count = 0;
  265. $data = $this->model->where($where)->select();
  266. $this->model->startTrans();
  267. try {
  268. foreach ($data as $v) {
  269. if ($v->id != $this->auth->id) {
  270. $count += $v->delete();
  271. Db::name('admin_group_access')
  272. ->where('uid', $v['id'])
  273. ->delete();
  274. }
  275. }
  276. $this->model->commit();
  277. } catch (Throwable $e) {
  278. $this->model->rollback();
  279. $this->error($e->getMessage());
  280. }
  281. if ($count) {
  282. $this->success(__('Deleted successfully'));
  283. } else {
  284. $this->error(__('No rows were deleted'));
  285. }
  286. }
  287. /**
  288. * 检查分组权限
  289. * @throws Throwable
  290. */
  291. public function checkGroupAuth(array $groups): void
  292. {
  293. if ($this->auth->isSuperAdmin()) {
  294. return;
  295. }
  296. $authGroups = $this->auth->getAllAuthGroups('allAuthAndOthers');
  297. foreach ($groups as $group) {
  298. if (!in_array($group, $authGroups)) {
  299. $this->error(__('You have no permission to add an administrator to this group!'));
  300. }
  301. }
  302. }
  303. }