SensitiveDataLog.php 2.8 KB

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