SensitiveDataLog.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace app\admin\controller\security;
  3. use Throwable;
  4. use ba\TableManager;
  5. use think\facade\Db;
  6. use app\common\controller\Backend;
  7. use app\admin\model\SensitiveDataLog as SensitiveDataLogModel;
  8. class SensitiveDataLog extends Backend
  9. {
  10. /**
  11. * @var object
  12. * @phpstan-var SensitiveDataLogModel
  13. */
  14. protected object $model;
  15. // 排除字段
  16. protected string|array $preExcludeFields = [];
  17. protected string|array $quickSearchField = 'sensitive.name';
  18. protected array $withJoinTable = ['sensitive', 'admin'];
  19. public function initialize(): void
  20. {
  21. parent::initialize();
  22. $this->model = new SensitiveDataLogModel();
  23. }
  24. /**
  25. * 查看
  26. * @throws Throwable
  27. */
  28. public function index(): void
  29. {
  30. if ($this->request->param('select')) {
  31. $this->select();
  32. }
  33. list($where, $alias, $limit, $order) = $this->queryBuilder();
  34. $res = $this->model
  35. ->withJoin($this->withJoinTable, $this->withJoinType)
  36. ->alias($alias)
  37. ->where($where)
  38. ->order($order)
  39. ->paginate($limit);
  40. foreach ($res->items() as $item) {
  41. $item->id_value = $item['primary_key'] . '=' . $item->id_value;
  42. }
  43. $this->success('', [
  44. 'list' => $res->items(),
  45. 'total' => $res->total(),
  46. 'remark' => get_route_remark(),
  47. ]);
  48. }
  49. /**
  50. * 详情
  51. * @param string|int|null $id
  52. * @throws Throwable
  53. */
  54. public function info(string|int $id = null): void
  55. {
  56. $row = $this->model
  57. ->withJoin($this->withJoinTable, $this->withJoinType)
  58. ->where('sensitive_data_log.id', $id)
  59. ->find();
  60. if (!$row) {
  61. $this->error(__('Record not found'));
  62. }
  63. $this->success('', [
  64. 'row' => $row
  65. ]);
  66. }
  67. /**
  68. * 回滚
  69. * @param array|null $ids
  70. * @throws Throwable
  71. */
  72. public function rollback(array $ids = null): void
  73. {
  74. $data = $this->model->where('id', 'in', $ids)->select();
  75. if (!$data) {
  76. $this->error(__('Record not found'));
  77. }
  78. $count = 0;
  79. $this->model->startTrans();
  80. try {
  81. foreach ($data as $row) {
  82. if (Db::connect(TableManager::getConnection($row->connection))->name($row->data_table)->where($row->primary_key, $row->id_value)->update([
  83. $row->data_field => $row->before
  84. ])) {
  85. $row->delete();
  86. $count++;
  87. }
  88. }
  89. $this->model->commit();
  90. } catch (Throwable $e) {
  91. $this->model->rollback();
  92. $this->error($e->getMessage());
  93. }
  94. if ($count) {
  95. $this->success();
  96. } else {
  97. $this->error(__('No rows were rollback'));
  98. }
  99. }
  100. }