Admin.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. $this->success('', [
  75. 'list' => $res->items(),
  76. 'total' => $res->total(),
  77. 'remark' => get_route_remark(),
  78. ]);
  79. }
  80. /**
  81. * 添加
  82. * @throws Throwable
  83. */
  84. public function add(): void
  85. {
  86. if ($this->request->isPost()) {
  87. $data = $this->request->post();
  88. if (!$data) {
  89. $this->error(__('Parameter %s can not be empty', ['']));
  90. }
  91. /**
  92. * 由于有密码字段-对方法进行重写
  93. * 数据验证
  94. */
  95. if ($this->modelValidate) {
  96. try {
  97. $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  98. $validate = new $validate();
  99. $validate->scene('add')->check($data);
  100. } catch (Throwable $e) {
  101. $this->error($e->getMessage());
  102. }
  103. }
  104. $salt = Random::build('alnum', 16);
  105. $passwd = encrypt_password($data['password'], $salt);
  106. $data = $this->excludeFields($data);
  107. if(!empty($data['institution_id']))
  108. {
  109. $data['institution'] = Institution::where('id',$data['institution_id'])->value('name');
  110. }
  111. if(!empty($data['depart_id']))
  112. {
  113. $data['depart'] = Depart::where('id',$data['depart_id'])->value('depart_name');
  114. }
  115. if(!empty($data['create_user_id']))
  116. {
  117. $data['create_user'] = $this->model->where('id',$data['create_user_id'])->value('nickname');
  118. }
  119. $result = false;
  120. if ($data['group_arr']) $this->checkGroupAuth($data['group_arr']);
  121. $this->model->startTrans();
  122. try {
  123. $data['salt'] = $salt;
  124. $data['password'] = $passwd;
  125. $result = $this->model->save($data);
  126. if ($data['group_arr']) {
  127. $groupAccess = [];
  128. foreach ($data['group_arr'] as $datum) {
  129. $groupAccess[] = [
  130. 'uid' => $this->model->id,
  131. 'group_id' => $datum,
  132. ];
  133. }
  134. Db::name('admin_group_access')->insertAll($groupAccess);
  135. }
  136. $this->model->commit();
  137. } catch (Throwable $e) {
  138. $this->model->rollback();
  139. $this->error($e->getMessage());
  140. }
  141. if ($result !== false) {
  142. $this->success(__('Added successfully'));
  143. } else {
  144. $this->error(__('No rows were added'));
  145. }
  146. }
  147. $this->error(__('Parameter error'));
  148. }
  149. /**
  150. * 编辑
  151. * @throws Throwable
  152. */
  153. public function edit($id = null): void
  154. {
  155. $row = $this->model->find($id);
  156. if (!$row) {
  157. $this->error(__('Record not found'));
  158. }
  159. $dataLimitAdminIds = $this->getDataLimitAdminIds();
  160. if ($dataLimitAdminIds && !in_array($row[$this->dataLimitField], $dataLimitAdminIds)) {
  161. $this->error(__('You have no permission'));
  162. }
  163. if ($this->request->isPost()) {
  164. $data = $this->request->post();
  165. if (!$data) {
  166. $this->error(__('Parameter %s can not be empty', ['']));
  167. }
  168. /**
  169. * 由于有密码字段-对方法进行重写
  170. * 数据验证
  171. */
  172. if ($this->modelValidate) {
  173. try {
  174. $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  175. $validate = new $validate();
  176. $validate->scene('edit')->check($data);
  177. } catch (Throwable $e) {
  178. $this->error($e->getMessage());
  179. }
  180. }
  181. if ($this->auth->id == $data['id'] && $data['status'] == '0') {
  182. $this->error(__('Please use another administrator account to disable the current account!'));
  183. }
  184. if (isset($data['password']) && $data['password']) {
  185. $this->model->resetPassword($data['id'], $data['password']);
  186. }
  187. $groupAccess = [];
  188. if ($data['group_arr']) {
  189. $checkGroups = [];
  190. foreach ($data['group_arr'] as $datum) {
  191. if (!in_array($datum, $row->group_arr)) {
  192. $checkGroups[] = $datum;
  193. }
  194. $groupAccess[] = [
  195. 'uid' => $id,
  196. 'group_id' => $datum,
  197. ];
  198. }
  199. $this->checkGroupAuth($checkGroups);
  200. }
  201. Db::name('admin_group_access')
  202. ->where('uid', $id)
  203. ->delete();
  204. $data = $this->excludeFields($data);
  205. $result = false;
  206. $this->model->startTrans();
  207. try {
  208. if(!empty($data['institution_id']))
  209. {
  210. $data['institution'] = Institution::where('id',$data['institution_id'])->value('name');
  211. }
  212. if(!empty($data['depart_id']))
  213. {
  214. $data['depart'] = Depart::where('id',$data['depart_id'])->value('depart_name');
  215. }
  216. if(!empty($data['create_user_id']))
  217. {
  218. $data['create_user'] = $this->model->where('id',$data['create_user_id'])->value('nickname');
  219. }
  220. $result = $row->save($data);
  221. if ($groupAccess) Db::name('admin_group_access')->insertAll($groupAccess);
  222. $this->model->commit();
  223. } catch (Throwable $e) {
  224. $this->model->rollback();
  225. $this->error($e->getMessage());
  226. }
  227. if ($result !== false) {
  228. $this->success(__('Update successful'));
  229. } else {
  230. $this->error(__('No rows updated'));
  231. }
  232. }
  233. unset($row['salt'], $row['login_failure']);
  234. $row['password'] = '';
  235. $this->success('', [
  236. 'row' => $row
  237. ]);
  238. }
  239. /**
  240. * 删除
  241. * @param null $ids
  242. * @throws Throwable
  243. */
  244. public function del($ids = null): void
  245. {
  246. if (!$this->request->isDelete() || !$ids) {
  247. $this->error(__('Parameter error'));
  248. }
  249. $where = [];
  250. $dataLimitAdminIds = $this->getDataLimitAdminIds();
  251. if ($dataLimitAdminIds) {
  252. $where[] = [$this->dataLimitField, 'in', $dataLimitAdminIds];
  253. }
  254. $pk = $this->model->getPk();
  255. $where[] = [$pk, 'in', $ids];
  256. $count = 0;
  257. $data = $this->model->where($where)->select();
  258. $this->model->startTrans();
  259. try {
  260. foreach ($data as $v) {
  261. if ($v->id != $this->auth->id) {
  262. $count += $v->delete();
  263. Db::name('admin_group_access')
  264. ->where('uid', $v['id'])
  265. ->delete();
  266. }
  267. }
  268. $this->model->commit();
  269. } catch (Throwable $e) {
  270. $this->model->rollback();
  271. $this->error($e->getMessage());
  272. }
  273. if ($count) {
  274. $this->success(__('Deleted successfully'));
  275. } else {
  276. $this->error(__('No rows were deleted'));
  277. }
  278. }
  279. /**
  280. * 检查分组权限
  281. * @throws Throwable
  282. */
  283. public function checkGroupAuth(array $groups): void
  284. {
  285. if ($this->auth->isSuperAdmin()) {
  286. return;
  287. }
  288. $authGroups = $this->auth->getAllAuthGroups('allAuthAndOthers');
  289. foreach ($groups as $group) {
  290. if (!in_array($group, $authGroups)) {
  291. $this->error(__('You have no permission to add an administrator to this group!'));
  292. }
  293. }
  294. }
  295. }