Admin.php 8.2 KB

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