| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace DH\AuditorBundle\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use DH\Auditor\Exception\AccessDeniedException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use DH\Auditor\Provider\Doctrine\Auditing\Annotation\Security; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use DH\Auditor\Provider\Doctrine\Persistence\Reader\Reader; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use DH\Auditor\Provider\Doctrine\Persistence\Schema\SchemaManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use DH\Auditor\Provider\Doctrine\Service\AuditingService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use DH\AuditorBundle\Helper\UrlHelper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Symfony\Component\HttpFoundation\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Symfony\Component\HttpFoundation\Response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Symfony\Component\Routing\Annotation\Route; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Symfony\Component\Security\Core\Exception\AccessDeniedException as SymfonyAccessDeniedException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Twig\Environment; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * @see \DH\AuditorBundle\Tests\Controller\ViewerControllerTest | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | class ViewerController | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     private Environment $environment; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     public function __construct(Environment $environment) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         $this->environment = $environment; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     #[Route(path: '/audit', name: 'dh_auditor_list_audits', methods: ['GET'])] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     public function listAuditsAction(Reader $reader): Response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $schemaManager = new SchemaManager($reader->getProvider()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         /** @var AuditingService[] $auditingServices */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $auditingServices = $reader->getProvider()->getAuditingServices(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $audited = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $scope = Security::VIEW_SCOPE; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         foreach ($auditingServices as $auditingService) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             $audited = array_merge( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |                 $audited, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |                 array_filter( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |                     $schemaManager->getAuditableTableNames($auditingService->getEntityManager()), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                     static function ($entity) use ($reader, $scope) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                         $roleChecker = $reader->getProvider()->getAuditor()->getConfiguration()->getRoleChecker(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                         return null === $roleChecker ? true : $roleChecker($entity, $scope); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |                     }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |                     ARRAY_FILTER_USE_KEY | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |                 ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         return $this->renderView('@DHAuditor/Audit/audits.html.twig', [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             'audited' => $audited, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             'reader' => $reader, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |     #[Route(path: '/audit/transaction/{hash}', name: 'dh_auditor_show_transaction', methods: ['GET'])] | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |     public function showTransactionAction(Reader $reader, string $hash): Response | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |         $audits = $reader->getAuditsByTransactionHash($hash); | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |         return $this->renderView('@DHAuditor/Audit/transaction.html.twig', [ | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |             'hash' => $hash, | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |             'audits' => $audits, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     #[Route(path: '/audit/{entity}/{id}', name: 'dh_auditor_show_entity_history', methods: ['GET'])] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     public function showEntityHistoryAction(Request $request, Reader $reader, string $entity, null|int|string $id = null): Response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         \assert(\is_string($request->query->get('page', '1')) || \is_int($request->query->get('page', '1'))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         $page = (int) $request->query->get('page', '1'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $page = $page < 1 ? 1 : $page; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         $entity = UrlHelper::paramToNamespace($entity); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         if (!$reader->getProvider()->isAuditable($entity)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             throw new NotFoundHttpException('Not Found'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             $pager = $reader->paginate($reader->createQuery($entity, [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |                 'object_id' => $id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |                 'page' => $page, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |                 'page_size' => Reader::PAGE_SIZE, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             ]), $page, Reader::PAGE_SIZE); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         } catch (AccessDeniedException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             throw new SymfonyAccessDeniedException('Access Denied.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         return $this->renderView('@DHAuditor/Audit/entity_history.html.twig', [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             'id' => $id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             'entity' => $entity, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             'paginator' => $pager, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     protected function renderView(string $view, array $parameters = []): Response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         return new Response($this->environment->render($view, $parameters)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 106 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 107 |  |  |  |