SensitiveDataLog.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. * @throws Throwable
  52. */
  53. public function info(): void
  54. {
  55. $pk = $this->model->getPk();
  56. $id = $this->request->param($pk);
  57. $row = $this->model
  58. ->withJoin($this->withJoinTable, $this->withJoinType)
  59. ->where('sensitive_data_log.id', $id)
  60. ->find();
  61. if (!$row) {
  62. $this->error(__('Record not found'));
  63. }
  64. $this->success('', [
  65. 'row' => $row
  66. ]);
  67. }
  68. /**
  69. * 回滚
  70. * @throws Throwable
  71. */
  72. public function rollback(): void
  73. {
  74. $ids = $this->request->param('ids/a', []);
  75. $data = $this->model->where('id', 'in', $ids)->select();
  76. if (!$data) {
  77. $this->error(__('Record not found'));
  78. }
  79. $count = 0;
  80. $this->model->startTrans();
  81. try {
  82. foreach ($data as $row) {
  83. if (Db::connect(TableManager::getConnection($row->connection))->name($row->data_table)->where($row->primary_key, $row->id_value)->update([
  84. $row->data_field => $row->before
  85. ])) {
  86. $row->delete();
  87. $count++;
  88. }
  89. }
  90. $this->model->commit();
  91. } catch (Throwable $e) {
  92. $this->model->rollback();
  93. $this->error($e->getMessage());
  94. }
  95. if ($count) {
  96. $this->success();
  97. } else {
  98. $this->error(__('No rows were rollback'));
  99. }
  100. }
  101. }