Admin.php 7.9 KB

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