|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace DH\DoctrineAuditBundle\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use DH\DoctrineAuditBundle\Exception\AccessDeniedException; |
|
6
|
|
|
use DH\DoctrineAuditBundle\Exception\InvalidArgumentException; |
|
7
|
|
|
use DH\DoctrineAuditBundle\Helper\AuditHelper; |
|
8
|
|
|
use DH\DoctrineAuditBundle\Reader\AuditEntry; |
|
9
|
|
|
use DH\DoctrineAuditBundle\Reader\AuditReader; |
|
10
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
|
11
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
12
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
13
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
|
14
|
|
|
|
|
15
|
|
|
class AuditController extends AbstractController |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @Route("/audit", name="dh_doctrine_audit_list_audits", methods={"GET"}) |
|
19
|
|
|
*/ |
|
20
|
|
|
public function listAuditsAction(): Response |
|
21
|
|
|
{ |
|
22
|
|
|
$reader = $this->container->get('dh_doctrine_audit.reader'); |
|
23
|
|
|
|
|
24
|
|
|
return $this->render('@DHDoctrineAudit/Audit/audits.html.twig', [ |
|
25
|
|
|
'audited' => $reader->getEntities(), |
|
26
|
|
|
'reader' => $reader, |
|
27
|
|
|
]); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @Route("/audit/revert/{id}/{object}/{entity}/{field}", name="dh_doctrine_audit_revert") |
|
32
|
|
|
* |
|
33
|
|
|
* @param $id |
|
34
|
|
|
* @param $object |
|
35
|
|
|
* @param $entity |
|
36
|
|
|
* @param $field |
|
37
|
|
|
* |
|
38
|
|
|
* @return \Symfony\Component\HttpFoundation\RedirectResponse |
|
39
|
|
|
*/ |
|
40
|
|
|
public function revertEntityHistoryAction($id, $object, $entity, $field) |
|
41
|
|
|
{ |
|
42
|
|
|
// get audit reader service |
|
43
|
|
|
$reader = $this->container->get('dh_doctrine_audit.reader'); |
|
44
|
|
|
// $am = $this->container->get('dh_doctrine_audit.manager'); |
|
45
|
|
|
// get audit entity manager |
|
46
|
|
|
$em = $this->container->get('doctrine.orm.default_entity_manager'); |
|
47
|
|
|
|
|
48
|
|
|
/** @var AuditEntry $entity_audit */ |
|
49
|
|
|
$entity_audit = $reader->getAudit(AuditHelper::paramToNamespace($entity), $id); |
|
50
|
|
|
$audited_entity = AuditHelper::paramToNamespace($entity); |
|
51
|
|
|
$current_entity = $em->getRepository($audited_entity)->find($object); |
|
52
|
|
|
|
|
53
|
|
|
// Get all differences |
|
54
|
|
|
$diffs = $entity_audit[0]->getDiffs(); |
|
55
|
|
|
// get field value to revert |
|
56
|
|
|
$field_value = $diffs[$field]['old']; |
|
57
|
|
|
|
|
58
|
|
|
$setMethod = "set{$field}"; |
|
59
|
|
|
|
|
60
|
|
|
$current_entity->{$setMethod}($field_value); |
|
61
|
|
|
|
|
62
|
|
|
$em->persist($current_entity); |
|
63
|
|
|
$em->flush(); |
|
64
|
|
|
|
|
65
|
|
|
return $this->redirectToRoute('dh_doctrine_audit_show_entity_history', [ |
|
66
|
|
|
'entity' => $entity, |
|
67
|
|
|
]); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @Route("/audit/transaction/{hash}", name="dh_doctrine_audit_show_transaction", methods={"GET"}) |
|
72
|
|
|
* |
|
73
|
|
|
* @param string $hash |
|
74
|
|
|
* |
|
75
|
|
|
* @throws InvalidArgumentException |
|
76
|
|
|
* @throws \Doctrine\ORM\ORMException |
|
77
|
|
|
* |
|
78
|
|
|
* @return Response |
|
79
|
|
|
*/ |
|
80
|
|
|
public function showTransactionAction(string $hash): Response |
|
81
|
|
|
{ |
|
82
|
|
|
$reader = $this->container->get('dh_doctrine_audit.reader'); |
|
83
|
|
|
$audits = $reader->getAuditsByTransactionHash($hash); |
|
84
|
|
|
|
|
85
|
|
|
return $this->render('@DHDoctrineAudit/Audit/transaction.html.twig', [ |
|
86
|
|
|
'hash' => $hash, |
|
87
|
|
|
'audits' => $audits, |
|
88
|
|
|
]); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @Route("/audit/{entity}/{id}", name="dh_doctrine_audit_show_entity_history", methods={"GET"}) |
|
93
|
|
|
* |
|
94
|
|
|
* @param Request $request |
|
95
|
|
|
* @param string $entity |
|
96
|
|
|
* @param int|string $id |
|
97
|
|
|
* |
|
98
|
|
|
* @return Response |
|
99
|
|
|
*/ |
|
100
|
|
|
public function showEntityHistoryAction(Request $request, string $entity, $id = null): Response |
|
101
|
|
|
{ |
|
102
|
|
|
$page = (int) $request->query->get('page', 1); |
|
103
|
|
|
$entity = AuditHelper::paramToNamespace($entity); |
|
104
|
|
|
|
|
105
|
|
|
$reader = $this->container->get('dh_doctrine_audit.reader'); |
|
106
|
|
|
|
|
107
|
|
|
if (!$reader->getConfiguration()->isAuditable($entity)) { |
|
108
|
|
|
throw $this->createNotFoundException(); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
try { |
|
112
|
|
|
$entries = $reader->getAuditsPager($entity, $id, $page, AuditReader::PAGE_SIZE); |
|
113
|
|
|
} catch (AccessDeniedException $e) { |
|
114
|
|
|
throw $this->createAccessDeniedException(); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
return $this->render('@DHDoctrineAudit/Audit/entity_history.html.twig', [ |
|
118
|
|
|
'id' => $id, |
|
119
|
|
|
'entity' => $entity, |
|
120
|
|
|
'entries' => $entries, |
|
121
|
|
|
]); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|