Skip to content

Commit

Permalink
UID support (#20)
Browse files Browse the repository at this point in the history
* Added explicit support to Ramsey UUIDs, Symfony UIDs (UUID and ULID)

* Moved resource handling from TransactionProcessor to AuditTrait

* Removed useless deps from require-dev
  • Loading branch information
DamienHarper authored Feb 3, 2021
1 parent 38ea173 commit fe26ddc
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 21 deletions.
3 changes: 0 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@
"extra": {
"branch-alias": {
"dev-master": "1.x-dev"
},
"symfony": {
"require": "3.4.*"
}
}
}
3 changes: 2 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ parameters:
- '~Parameter \#1 \$name of method Symfony\\Component\\Console\\Command\\Command\:\:setName\(\) expects string, string\|null given~'
- '~Parameter \#1 \$(first|max)Results? of method Doctrine\\DBAL\\Query\\QueryBuilder\:\:set(First|Max)Results?\(\) expects int, null given~'
- '~Parameter \#1 \$entity of method DH\\Auditor\\Provider\\Doctrine\\Persistence\\Schema\\SchemaManager\:\:(create|update)AuditTable\(\) expects string, string\|null given.~'
- '~Parameter \#1 \$event of method Symfony\\Contracts\\EventDispatcher\\EventDispatcherInterface\:\:dispatch\(\) expects object, string given~'
- '~Parameter \#2 \$eventName of method Symfony\\Contracts\\EventDispatcher\\EventDispatcherInterface\:\:dispatch\(\) expects string\|null, DH\\Auditor\\Event\\LifecycleEvent given\.~'
- '~Cannot call method setFetchMode\(\) on Doctrine\\DBAL\\Driver\\ResultStatement\|int~'
- '~Cannot call method fetchAll\(\) on Doctrine\\DBAL\\Driver\\ResultStatement\|int~'
- '~Parameter \#1 \$keep of method DH\\Auditor\\Provider\\Doctrine\\Persistence\\Command\\CleanAuditLogsCommand\:\:validateKeepArgument\(\) expects string, string\|null given~'
- '~Parameter \#1 \$eventName of method Symfony\\Component\\EventDispatcher\\EventDispatcherInterface\:\:dispatch\(\) expects string, DH\\Auditor\\Event\\LifecycleEvent given~'
17 changes: 17 additions & 0 deletions src/Provider/Doctrine/Auditing/Transaction/AuditTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,23 @@ private function value(EntityManagerInterface $entityManager, Type $type, $value
case DoctrineHelper::getDoctrineType('BOOLEAN'):
$convertedValue = $type->convertToPHPValue($value, $platform);

break;
case 'uuid_binary':
case 'uuid_binary_ordered_time':
case 'uuid':
case 'ulid':
// Ramsey UUID / Symfony UID (UUID/ULID)
$convertedValue = (string) $value;

break;
case DoctrineHelper::getDoctrineType('BINARY'):
if (\is_resource($value)) {
// let's replace resources with a "simple" representation: resourceType#resourceId
$convertedValue = get_resource_type($value).'#'.get_resource_id($value);
} else {
$convertedValue = $type->convertToDatabaseValue($value, $platform);
}

break;
default:
$convertedValue = $type->convertToDatabaseValue($value, $platform);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,30 +214,14 @@ private function audit(array $data): void
$auditTable = $schema.$configuration->getTablePrefix().$data['table'].$configuration->getTableSuffix();
$dt = new DateTime('now', new DateTimeZone($this->provider->getAuditor()->getConfiguration()->getTimezone()));

$diffs = json_encode($data['diff']);
if (false === $diffs) {
// json_encode failed, probably because of an unsupported type (resource type)
// let's replace resources with a "simple" representation
foreach ($data['diff'] as $property => $changeset) {
if (isset($changeset['old']) && \is_resource($changeset['old'])) {
$data['diff'][$property]['old'] = get_resource_type($changeset['old']).'#'.get_resource_id($changeset['old']);
}
if (isset($changeset['new']) && \is_resource($changeset['new'])) {
$data['diff'][$property]['new'] = get_resource_type($changeset['new']).'#'.get_resource_id($changeset['new']);
}
}

$diffs = json_encode($data['diff']);
}

$payload = [
'entity' => $data['entity'],
'table' => $auditTable,
'type' => $data['action'],
'object_id' => (string) $data['id'],
'discriminator' => $data['discriminator'],
'transaction_hash' => (string) $data['transaction_hash'],
'diffs' => $diffs,
'diffs' => json_encode($data['diff']),
'blame_id' => $data['blame']['user_id'],
'blame_user' => $data['blame']['username'],
'blame_user_fqdn' => $data['blame']['user_fqdn'],
Expand Down

0 comments on commit fe26ddc

Please sign in to comment.