Admin.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. $arr = $this->model->find($id);
  164. $row = [];
  165. foreach ($arr->toArray() as $k=>$v)
  166. {
  167. $row[strtolower($k)] = $v;
  168. }
  169. if (!$row) {
  170. $this->error(__('Record not found'));
  171. }
  172. $dataLimitAdminIds = $this->getDataLimitAdminIds();
  173. if ($dataLimitAdminIds && !in_array($row[$this->dataLimitField], $dataLimitAdminIds)) {
  174. $this->error(__('You have no permission'));
  175. }
  176. if ($this->request->isPost()) {
  177. $data = $this->request->post();
  178. if (!$data) {
  179. $this->error(__('Parameter %s can not be empty', ['']));
  180. }
  181. /**
  182. * 由于有密码字段-对方法进行重写
  183. * 数据验证
  184. */
  185. if ($this->modelValidate) {
  186. try {
  187. $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  188. $validate = new $validate();
  189. $validate->scene('edit')->check($data);
  190. } catch (Throwable $e) {
  191. $this->error($e->getMessage());
  192. }
  193. }
  194. if ($this->auth->id == $data['id'] && $data['status'] == '0') {
  195. $this->error(__('Please use another administrator account to disable the current account!'));
  196. }
  197. if (isset($data['password']) && $data['password']) {
  198. $this->model->resetPassword($data['id'], $data['password']);
  199. }
  200. $groupAccess = [];
  201. if ($data['group_arr']) {
  202. $checkGroups = [];
  203. foreach ($data['group_arr'] as $datum) {
  204. if (!in_array($datum, $arr->group_arr)) {
  205. $checkGroups[] = $datum;
  206. }
  207. $groupAccess[] = [
  208. 'uid' => $id,
  209. 'group_id' => $datum,
  210. ];
  211. }
  212. $this->checkGroupAuth($checkGroups);
  213. }
  214. Db::name('admin_group_access')
  215. ->where('uid', $id)
  216. ->delete();
  217. $data = $this->excludeFields($data);
  218. $result = false;
  219. $this->model->startTrans();
  220. try {
  221. if(!empty($data['institution_id']))
  222. {
  223. $data['institution'] = Institution::where('id',$data['institution_id'])->value('name');
  224. }
  225. if(!empty($data['depart_id']))
  226. {
  227. $data['depart'] = Depart::where('id',$data['depart_id'])->value('depart_name');
  228. }
  229. if(!empty($data['create_user_id']))
  230. {
  231. $data['create_user'] = $this->model->where('id',$data['create_user_id'])->value('nickname');
  232. }
  233. unset($data['id']);
  234. $result = $arr->save($data);
  235. if ($groupAccess) Db::name('admin_group_access')->insertAll($groupAccess);
  236. $this->model->commit();
  237. } catch (Throwable $e) {
  238. $this->model->rollback();
  239. $this->error($e->getMessage());
  240. }
  241. if ($result !== false) {
  242. $this->success(__('Update successful'));
  243. } else {
  244. $this->error(__('No rows updated'));
  245. }
  246. }
  247. unset($row['salt'], $row['login_failure']);
  248. $row['password'] = '';
  249. $this->success('', [
  250. 'row' => $row
  251. ]);
  252. }
  253. /**
  254. * 删除
  255. * @param null $ids
  256. * @throws Throwable
  257. */
  258. public function del($ids = null): void
  259. {
  260. if (!$this->request->isDelete() || !$ids) {
  261. $this->error(__('Parameter error'));
  262. }
  263. $where = [];
  264. $dataLimitAdminIds = $this->getDataLimitAdminIds();
  265. if ($dataLimitAdminIds) {
  266. $where[] = [$this->dataLimitField, 'in', $dataLimitAdminIds];
  267. }
  268. $pk = $this->model->getPk();
  269. $where[] = [$pk, 'in', $ids];
  270. $count = 0;
  271. $data = $this->model->where($where)->select();
  272. $this->model->startTrans();
  273. try {
  274. foreach ($data as $v) {
  275. // if ($v->ID != $this->auth->ID) {
  276. $count += $v->where('ID',$v->ID)->delete();
  277. Db::name('admin_group_access')
  278. ->where('uid', $v['id'])
  279. ->delete();
  280. // }
  281. }
  282. $this->model->commit();
  283. } catch (Throwable $e) {
  284. $this->model->rollback();
  285. $this->error($e->getMessage());
  286. }
  287. if ($count) {
  288. $this->success(__('Deleted successfully'));
  289. } else {
  290. $this->error(__('No rows were deleted'));
  291. }
  292. }
  293. /**
  294. * 检查分组权限
  295. * @throws Throwable
  296. */
  297. public function checkGroupAuth(array $groups): void
  298. {
  299. if ($this->auth->isSuperAdmin()) {
  300. return;
  301. }
  302. $authGroups = $this->auth->getAllAuthGroups('allAuthAndOthers');
  303. foreach ($groups as $group) {
  304. if (!in_array($group, $authGroups)) {
  305. $this->error(__('You have no permission to add an administrator to this group!'));
  306. }
  307. }
  308. }
  309. }