-
-
Notifications
You must be signed in to change notification settings - Fork 474
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add version reference change and version deletion audit logs
- Loading branch information
Showing
8 changed files
with
235 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Packagist. | ||
* | ||
* (c) Jordi Boggiano <j.boggiano@seld.be> | ||
* Nils Adermann <naderman@naderman.de> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace App\EventListener; | ||
|
||
use App\Entity\AuditRecord; | ||
use App\Entity\User; | ||
use App\Entity\Version; | ||
use App\Util\DoctrineTrait; | ||
use Doctrine\Bundle\DoctrineBundle\Attribute\AsEntityListener; | ||
use Doctrine\ORM\EntityManager; | ||
use Doctrine\ORM\Event\PreUpdateEventArgs; | ||
use Doctrine\Persistence\Event\LifecycleEventArgs; | ||
use Doctrine\Persistence\ManagerRegistry; | ||
use Symfony\Bundle\SecurityBundle\Security; | ||
|
||
#[AsEntityListener(event: 'preRemove', entity: Version::class)] | ||
#[AsEntityListener(event: 'preUpdate', entity: Version::class)] | ||
#[AsEntityListener(event: 'postUpdate', entity: Version::class)] | ||
class VersionListener | ||
{ | ||
use DoctrineTrait; | ||
|
||
/** @var list<AuditRecord> */ | ||
private array $buffered = []; | ||
|
||
public function __construct( | ||
private ManagerRegistry $doctrine, | ||
private Security $security, | ||
) { | ||
} | ||
|
||
/** | ||
* @param LifecycleEventArgs<EntityManager> $event | ||
*/ | ||
public function preRemove(Version $version, LifecycleEventArgs $event): void | ||
{ | ||
$record = AuditRecord::versionDeleted($version, $this->getUser()); | ||
$this->getEM()->persist($record); | ||
// let the record be flushed together with the entity | ||
} | ||
|
||
public function preUpdate(Version $version, PreUpdateEventArgs $event): void | ||
{ | ||
if (($event->hasChangedField('source') || $event->hasChangedField('dist')) && !$version->isDevelopment()) { | ||
$oldDistRef = $event->getOldValue('dist')['reference'] ?? null; | ||
$oldSourceRef = $event->getOldValue('source')['reference'] ?? null; | ||
$newDistRef = $event->getNewValue('dist')['reference'] ?? null; | ||
$newSourceRef = $event->getNewValue('source')['reference'] ?? null; | ||
if ($oldDistRef !== $newDistRef || $oldSourceRef !== $newSourceRef) { | ||
// buffering things to be inserted in postUpdate once we can confirm it is done | ||
$this->buffered[] = AuditRecord::versionReferenceChange($version, $oldSourceRef, $oldDistRef); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @param LifecycleEventArgs<EntityManager> $event | ||
*/ | ||
public function postUpdate(Version $version, LifecycleEventArgs $event): void | ||
{ | ||
if ($this->buffered) { | ||
$repo = $this->getEM()->getRepository(AuditRecord::class); | ||
foreach ($this->buffered as $record) { | ||
$repo->insert($record); | ||
} | ||
$this->buffered = []; | ||
} | ||
} | ||
|
||
private function getUser(): User|null | ||
{ | ||
$user = $this->security->getUser(); | ||
|
||
return $user instanceof User ? $user : null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Packagist. | ||
* | ||
* (c) Jordi Boggiano <j.boggiano@seld.be> | ||
* Nils Adermann <naderman@naderman.de> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace App\Tests\Controller; | ||
|
||
use App\Audit\AuditRecordType; | ||
use App\Entity\Version; | ||
use Doctrine\DBAL\Connection; | ||
use Doctrine\Persistence\ManagerRegistry; | ||
use App\Entity\Package; | ||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||
|
||
class VersionAuditRecordTest extends KernelTestCase | ||
{ | ||
public function setUp(): void | ||
{ | ||
self::bootKernel(); | ||
static::getContainer()->get(Connection::class)->beginTransaction(); | ||
|
||
parent::setUp(); | ||
} | ||
|
||
public function tearDown(): void | ||
{ | ||
static::getContainer()->get(Connection::class)->rollBack(); | ||
|
||
parent::tearDown(); | ||
} | ||
|
||
public function testVersionChangesGetRecorded(): void | ||
{ | ||
$container = static::getContainer(); | ||
$em = $container->get(ManagerRegistry::class)->getManager(); | ||
|
||
$package = new Package(); | ||
$package->setRepository('https://github.com/composer/composer'); | ||
|
||
$version = new Version(); | ||
$version->setPackage($package); | ||
$version->setName($package->getName()); | ||
$version->setVersion('1.0.0'); | ||
$version->setNormalizedVersion('1.0.0.0'); | ||
$version->setDevelopment(false); | ||
$version->setLicense([]); | ||
$version->setAutoload([]); | ||
$version->setDist(['reference' => 'old-dist-ref', 'type' => 'zip', 'url' => 'https://example.org/dist.zip']); | ||
|
||
$em->persist($package); | ||
$em->persist($version); | ||
$em->flush(); | ||
|
||
$version->setDist(['reference' => 'new-dist-ref', 'type' => 'zip', 'url' => 'https://example.org/dist.zip']); | ||
$version->setSource(['reference' => 'new-source-ref', 'type' => 'git', 'url' => 'git://example.org/dist.zip']); | ||
$em->persist($version); | ||
$em->flush(); | ||
|
||
$logs = $container->get(Connection::class)->fetchAllAssociative('SELECT * FROM audit_log ORDER BY id DESC'); | ||
self::assertCount(2, $logs); // package creation + version reference change | ||
self::assertSame(AuditRecordType::VersionReferenceChange->value, $logs[0]['type']); | ||
self::assertSame('{"name": "composer/composer", "dist_to": "new-dist-ref", "version": "1.0.0", "dist_from": "old-dist-ref", "source_to": "new-source-ref", "source_from": null}', $logs[0]['attributes']); | ||
|
||
// verify that only reference changes triggers a new audit log | ||
$version->setDist(['reference' => 'new-dist-ref', 'type' => 'zip2', 'url' => 'https://example.org/dist.zip2']); | ||
$version->setSource(['reference' => 'new-source-ref', 'type' => 'git2', 'url' => 'git://example.org/dist.zip2']); | ||
$em->persist($version); | ||
$em->flush(); | ||
|
||
$logs = $container->get(Connection::class)->fetchAllAssociative('SELECT * FROM audit_log ORDER BY id DESC'); | ||
self::assertCount(2, $logs); | ||
|
||
$em->remove($version); | ||
$em->flush(); | ||
|
||
$logs = $container->get(Connection::class)->fetchAllAssociative('SELECT * FROM audit_log ORDER BY id DESC'); | ||
self::assertCount(3, $logs); | ||
self::assertSame(AuditRecordType::VersionDeleted->value, $logs[0]['type']); | ||
} | ||
} |