SensitiveData.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. namespace app\admin\controller\security;
  3. use app\common\controller\Backend;
  4. use app\admin\model\SensitiveData as SensitiveDataModel;
  5. use think\db\exception\PDOException;
  6. use think\exception\ValidateException;
  7. use think\facade\Db;
  8. use Exception;
  9. class SensitiveData extends Backend
  10. {
  11. protected $model = null;
  12. // 排除字段
  13. protected $preExcludeFields = ['updatetime', 'createtime'];
  14. protected $quickSearchField = 'controller';
  15. public function initialize()
  16. {
  17. parent::initialize();
  18. $this->model = new SensitiveDataModel();
  19. }
  20. /**
  21. * 查看
  22. */
  23. public function index()
  24. {
  25. $this->request->filter(['strip_tags', 'trim']);
  26. if ($this->request->param('select')) {
  27. $this->select();
  28. }
  29. list($where, $alias, $limit, $order) = $this->queryBuilder();
  30. $res = $this->model
  31. ->withJoin($this->withJoinTable, $this->withJoinType)
  32. ->alias($alias)
  33. ->where($where)
  34. ->order($order)
  35. ->paginate($limit);
  36. foreach ($res->items() as $item) {
  37. if ($item->data_fields) {
  38. $fields = [];
  39. foreach ($item->data_fields as $key => $field) {
  40. $fields[] = $field ?: $key;
  41. }
  42. $item->data_fields = $fields;
  43. }
  44. }
  45. $this->success('', [
  46. 'list' => $res->items(),
  47. 'total' => $res->total(),
  48. 'remark' => get_route_remark(),
  49. ]);
  50. }
  51. /**
  52. * 添加重写
  53. */
  54. public function add()
  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. $data = $this->excludeFields($data);
  62. $data['controller_as'] = str_ireplace('.php', '', $data['controller'] ?? '');
  63. $data['controller_as'] = strtolower(str_ireplace(['\\', '.'], '/', $data['controller_as']));
  64. $result = false;
  65. Db::startTrans();
  66. try {
  67. // 模型验证
  68. if ($this->modelValidate) {
  69. $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  70. if (class_exists($validate)) {
  71. $validate = new $validate;
  72. if ($this->modelSceneValidate) $validate->scene('add');
  73. $validate->check($data);
  74. }
  75. }
  76. if (is_array($data['fields'])) {
  77. $data['data_fields'] = [];
  78. foreach ($data['fields'] as $field) {
  79. $data['data_fields'][$field['name']] = $field['value'];
  80. }
  81. }
  82. $result = $this->model->save($data);
  83. Db::commit();
  84. } catch (ValidateException|Exception|PDOException $e) {
  85. Db::rollback();
  86. $this->error($e->getMessage());
  87. }
  88. if ($result !== false) {
  89. $this->success(__('Added successfully'));
  90. } else {
  91. $this->error(__('No rows were added'));
  92. }
  93. }
  94. // 放在add方法内,就不需要额外添加权限节点了
  95. $this->success('', [
  96. 'tables' => $this->getTableList(),
  97. 'controllers' => $this->getControllerList(),
  98. ]);
  99. }
  100. /**
  101. * 编辑重写
  102. * @param null $id
  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. if ($this->request->isPost()) {
  111. $data = $this->request->post();
  112. if (!$data) {
  113. $this->error(__('Parameter %s can not be empty', ['']));
  114. }
  115. $data = $this->excludeFields($data);
  116. $data['controller_as'] = str_ireplace('.php', '', $data['controller'] ?? '');
  117. $data['controller_as'] = strtolower(str_ireplace(['\\', '.'], '/', $data['controller_as']));
  118. $result = false;
  119. Db::startTrans();
  120. try {
  121. // 模型验证
  122. if ($this->modelValidate) {
  123. $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  124. if (class_exists($validate)) {
  125. $validate = new $validate;
  126. if ($this->modelSceneValidate) $validate->scene('edit');
  127. $validate->check($data);
  128. }
  129. }
  130. if (is_array($data['fields'])) {
  131. $data['data_fields'] = [];
  132. foreach ($data['fields'] as $field) {
  133. $data['data_fields'][$field['name']] = $field['value'];
  134. }
  135. }
  136. $result = $row->save($data);
  137. Db::commit();
  138. } catch (ValidateException|Exception|PDOException $e) {
  139. Db::rollback();
  140. $this->error($e->getMessage());
  141. }
  142. if ($result !== false) {
  143. $this->success(__('Update successful'));
  144. } else {
  145. $this->error(__('No rows updated'));
  146. }
  147. }
  148. $this->success('', [
  149. 'row' => $row,
  150. 'tables' => $this->getTableList(),
  151. 'controllers' => $this->getControllerList(),
  152. ]);
  153. }
  154. protected function getControllerList()
  155. {
  156. $outExcludeController = [
  157. 'Addon.php',
  158. 'Ajax.php',
  159. 'Dashboard.php',
  160. 'Index.php',
  161. 'Module.php',
  162. 'Terminal.php',
  163. 'auth/AdminLog.php',
  164. 'routine/AdminInfo.php',
  165. 'routine/Config.php',
  166. 'user/MoneyLog.php',
  167. 'user/ScoreLog.php',
  168. ];
  169. $outControllers = [];
  170. $controllers = get_controller_list();
  171. foreach ($controllers as $key => $controller) {
  172. if (!in_array($controller, $outExcludeController)) {
  173. $outControllers[$key] = $controller;
  174. }
  175. }
  176. return $outControllers;
  177. }
  178. protected function getTableList()
  179. {
  180. $tablePrefix = config('database.connections.mysql.prefix');
  181. $outExcludeTable = [
  182. // 功能表
  183. 'area',
  184. 'token',
  185. 'captcha',
  186. 'admin_group_access',
  187. 'config',
  188. // 无编辑功能
  189. 'admin_log',
  190. 'user_money_log',
  191. 'user_score_log',
  192. ];
  193. $outTables = [];
  194. $tables = get_table_list();
  195. $pattern = '/^' . $tablePrefix . '/i';
  196. foreach ($tables as $table => $tableComment) {
  197. $table = preg_replace($pattern, '', $table);
  198. if (!in_array($table, $outExcludeTable)) {
  199. $outTables[$table] = $tableComment;
  200. }
  201. }
  202. return $outTables;
  203. }
  204. }