Skip to content

Commit

Permalink
Ref #5: enable Gedmo Loggable & log User attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
LucileDT committed Mar 27, 2024
1 parent 682836a commit 3c997fb
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 0 deletions.
3 changes: 3 additions & 0 deletions config/packages/stof_doctrine_extensions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
# See the official DoctrineExtensions documentation for more details: https://github.com/doctrine-extensions/DoctrineExtensions/tree/main/doc
stof_doctrine_extensions:
default_locale: fr_FR
orm:
default:
loggable: true
1 change: 1 addition & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ services:
class: Gedmo\SoftDeleteable\SoftDeleteableListener
tags:
- { name: doctrine.event_subscriber, connection: default }
# log of activities is stored in config/packages/stof_doctrine_extensions.yaml

# makes classes in src/ available to be used as services
# this creates a service per class whose id is the fully-qualified class name
Expand Down
33 changes: 33 additions & 0 deletions migrations/Version20240326220159.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

final class Version20240326220159 extends AbstractMigration
{
public function getDescription(): string
{
return 'Create activity logs table';
}

public function up(Schema $schema): void
{
$this->addSql('CREATE SEQUENCE log_activity_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
$this->addSql('CREATE TABLE log_activity (id INT NOT NULL, action VARCHAR(8) NOT NULL, logged_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, object_id VARCHAR(64) DEFAULT NULL, object_class VARCHAR(191) NOT NULL, version INT NOT NULL, data TEXT DEFAULT NULL, username VARCHAR(191) DEFAULT NULL, PRIMARY KEY(id))');
$this->addSql('CREATE INDEX log_activity_user_index ON log_activity (username)');
$this->addSql('CREATE INDEX log_activity_class_index ON log_activity (object_class)');
$this->addSql('CREATE INDEX log_activity_date_index ON log_activity (logged_at)');
$this->addSql('CREATE INDEX log_activity_version_index ON log_activity (object_id, object_class, version)');
$this->addSql('COMMENT ON COLUMN log_activity.data IS \'(DC2Type:array)\'');
}

public function down(Schema $schema): void
{
$this->addSql('DROP SEQUENCE log_activity_id_seq CASCADE');
$this->addSql('DROP TABLE log_activity');
}
}
16 changes: 16 additions & 0 deletions src/Entity/LogActivity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App\Entity;

use App\Repository\LogActivityRepository;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Loggable\Entity\MappedSuperclass\AbstractLogEntry;

#[ORM\Entity(repositoryClass: LogActivityRepository::class)]
#[ORM\Index(columns: ['username'], name: 'log_activity_user_index')]
#[ORM\Index(columns: ['object_class'], name: 'log_activity_class_index')]
#[ORM\Index(columns: ['logged_at'], name: 'log_activity_date_index')]
#[ORM\Index(columns: ['object_id', 'object_class', 'version'], name: 'log_activity_version_index')]
class LogActivity extends AbstractLogEntry
{
}
3 changes: 3 additions & 0 deletions src/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#[ORM\Table(name: '`user`')]
#[ORM\UniqueConstraint(name: 'UNIQ_IDENTIFIER_USERNAME', fields: ['username'])]
#[Gedmo\SoftDeleteable(fieldName: 'deletedAt', timeAware: false, hardDelete: true)]
#[Gedmo\Loggable(logEntryClass: LogActivity::class)]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
// manage soft deletion
Expand All @@ -24,12 +25,14 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface
private ?int $id = null;

#[ORM\Column(length: 180)]
#[Gedmo\Versioned]
private ?string $username = null;

/**
* @var list<string> The user roles
*/
#[ORM\Column]
#[Gedmo\Versioned]
private array $roles = [];

/**
Expand Down
25 changes: 25 additions & 0 deletions src/Repository/LogActivityRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace App\Repository;

use App\Entity\LogActivity;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepositoryInterface;
use Doctrine\Persistence\ManagerRegistry;
use Gedmo\Loggable\Entity\Repository\LogEntryRepository;

/**
* @implements ServiceEntityRepositoryInterface<LogActivity>
*
* @method LogActivity|null find($id, $lockMode = null, $lockVersion = null)
* @method LogActivity|null findOneBy(array $criteria, array $orderBy = null)
* @method LogActivity[] findAll()
* @method LogActivity[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class LogActivityRepository extends LogEntryRepository implements ServiceEntityRepositoryInterface
{
public function __construct(ManagerRegistry $registry)
{
$logActivityManager = $registry->getManagerForClass(LogActivity::class);
parent::__construct($logActivityManager, $logActivityManager->getClassMetadata(LogActivity::class));
}
}

0 comments on commit 3c997fb

Please sign in to comment.