SensitiveData.php 7.1 KB

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