Admin.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?php
  2. namespace app\admin\controller\auth;
  3. use Throwable;
  4. use think\facade\Db;
  5. use app\common\controller\Backend;
  6. use app\admin\model\Admin as AdminModel;
  7. class Admin extends Backend
  8. {
  9. /**
  10. * 模型
  11. * @var object
  12. * @phpstan-var AdminModel
  13. */
  14. protected object $model;
  15. protected array|string $preExcludeFields = ['create_time', 'update_time', 'password', 'salt', 'login_failure', 'last_login_time', 'last_login_ip'];
  16. protected array|string $quickSearchField = ['username', 'nickname'];
  17. /**
  18. * 开启数据限制
  19. */
  20. protected string|int|bool $dataLimit = 'allAuthAndOthers';
  21. protected string $dataLimitField = 'id';
  22. public function initialize(): void
  23. {
  24. parent::initialize();
  25. $this->model = new AdminModel();
  26. }
  27. /**
  28. * 查看
  29. * @throws Throwable
  30. */
  31. public function index(): void
  32. {
  33. if ($this->request->param('select')) {
  34. $this->select();
  35. }
  36. list($where, $alias, $limit, $order) = $this->queryBuilder();
  37. $res = $this->model
  38. ->withoutField('login_failure,password,salt')
  39. ->withJoin($this->withJoinTable, $this->withJoinType)
  40. ->alias($alias)
  41. ->where($where)
  42. ->order($order)
  43. ->paginate($limit);
  44. $this->success('', [
  45. 'list' => $res->items(),
  46. 'total' => $res->total(),
  47. 'remark' => get_route_remark(),
  48. ]);
  49. }
  50. /**
  51. * 添加
  52. * @throws Throwable
  53. */
  54. public function add(): void
  55. {
  56. if ($this->request->isPost()) {
  57. $data = $this->request->post();
  58. if (!$data) {
  59. $this->error(__('Parameter %s can not be empty', ['']));
  60. }
  61. if ($this->modelValidate) {
  62. try {
  63. $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  64. $validate = new $validate();
  65. $validate->scene('add')->check($data);
  66. } catch (Throwable $e) {
  67. $this->error($e->getMessage());
  68. }
  69. }
  70. $passwd = $data['password']; // 密码将被排除不直接入库
  71. $data = $this->excludeFields($data);
  72. $result = false;
  73. if ($data['group_arr']) $this->checkGroupAuth($data['group_arr']);
  74. $this->model->startTrans();
  75. try {
  76. $result = $this->model->save($data);
  77. if ($data['group_arr']) {
  78. $groupAccess = [];
  79. foreach ($data['group_arr'] as $datum) {
  80. $groupAccess[] = [
  81. 'uid' => $this->model->id,
  82. 'group_id' => $datum,
  83. ];
  84. }
  85. Db::name('admin_group_access')->insertAll($groupAccess);
  86. }
  87. $this->model->commit();
  88. if (!empty($passwd)) {
  89. $this->model->resetPassword($this->model->id, $passwd);
  90. }
  91. } catch (Throwable $e) {
  92. $this->model->rollback();
  93. $this->error($e->getMessage());
  94. }
  95. if ($result !== false) {
  96. $this->success(__('Added successfully'));
  97. } else {
  98. $this->error(__('No rows were added'));
  99. }
  100. }
  101. $this->error(__('Parameter error'));
  102. }
  103. /**
  104. * 编辑
  105. * @throws Throwable
  106. */
  107. public function edit(): void
  108. {
  109. $pk = $this->model->getPk();
  110. $id = $this->request->param($pk);
  111. $row = $this->model->find($id);
  112. if (!$row) {
  113. $this->error(__('Record not found'));
  114. }
  115. $dataLimitAdminIds = $this->getDataLimitAdminIds();
  116. if ($dataLimitAdminIds && !in_array($row[$this->dataLimitField], $dataLimitAdminIds)) {
  117. $this->error(__('You have no permission'));
  118. }
  119. if ($this->request->isPost()) {
  120. $data = $this->request->post();
  121. if (!$data) {
  122. $this->error(__('Parameter %s can not be empty', ['']));
  123. }
  124. if ($this->modelValidate) {
  125. try {
  126. $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  127. $validate = new $validate();
  128. $validate->scene('edit')->check($data);
  129. } catch (Throwable $e) {
  130. $this->error($e->getMessage());
  131. }
  132. }
  133. if ($this->auth->id == $data['id'] && $data['status'] == 'disable') {
  134. $this->error(__('Please use another administrator account to disable the current account!'));
  135. }
  136. if (!empty($data['password'])) {
  137. $this->model->resetPassword($row->id, $data['password']);
  138. }
  139. $groupAccess = [];
  140. if ($data['group_arr']) {
  141. $checkGroups = [];
  142. foreach ($data['group_arr'] as $datum) {
  143. if (!in_array($datum, $row->group_arr)) {
  144. $checkGroups[] = $datum;
  145. }
  146. $groupAccess[] = [
  147. 'uid' => $id,
  148. 'group_id' => $datum,
  149. ];
  150. }
  151. $this->checkGroupAuth($checkGroups);
  152. }
  153. Db::name('admin_group_access')
  154. ->where('uid', $id)
  155. ->delete();
  156. $data = $this->excludeFields($data);
  157. $result = false;
  158. $this->model->startTrans();
  159. try {
  160. $result = $row->save($data);
  161. if ($groupAccess) Db::name('admin_group_access')->insertAll($groupAccess);
  162. $this->model->commit();
  163. } catch (Throwable $e) {
  164. $this->model->rollback();
  165. $this->error($e->getMessage());
  166. }
  167. if ($result !== false) {
  168. $this->success(__('Update successful'));
  169. } else {
  170. $this->error(__('No rows updated'));
  171. }
  172. }
  173. unset($row['salt'], $row['login_failure']);
  174. $row['password'] = '';
  175. $this->success('', [
  176. 'row' => $row
  177. ]);
  178. }
  179. /**
  180. * 删除
  181. * @throws Throwable
  182. */
  183. public function del(): void
  184. {
  185. $where = [];
  186. $dataLimitAdminIds = $this->getDataLimitAdminIds();
  187. if ($dataLimitAdminIds) {
  188. $where[] = [$this->dataLimitField, 'in', $dataLimitAdminIds];
  189. }
  190. $ids = $this->request->param('ids/a', []);
  191. $where[] = [$this->model->getPk(), 'in', $ids];
  192. $data = $this->model->where($where)->select();
  193. $count = 0;
  194. $this->model->startTrans();
  195. try {
  196. foreach ($data as $v) {
  197. if ($v->id != $this->auth->id) {
  198. $count += $v->delete();
  199. Db::name('admin_group_access')
  200. ->where('uid', $v['id'])
  201. ->delete();
  202. }
  203. }
  204. $this->model->commit();
  205. } catch (Throwable $e) {
  206. $this->model->rollback();
  207. $this->error($e->getMessage());
  208. }
  209. if ($count) {
  210. $this->success(__('Deleted successfully'));
  211. } else {
  212. $this->error(__('No rows were deleted'));
  213. }
  214. }
  215. /**
  216. * 检查分组权限
  217. * @throws Throwable
  218. */
  219. private function checkGroupAuth(array $groups): void
  220. {
  221. if ($this->auth->isSuperAdmin()) {
  222. return;
  223. }
  224. $authGroups = $this->auth->getAllAuthGroups('allAuthAndOthers');
  225. foreach ($groups as $group) {
  226. if (!in_array($group, $authGroups)) {
  227. $this->error(__('You have no permission to add an administrator to this group!'));
  228. }
  229. }
  230. }
  231. }