User rename logging implemented.
This commit is contained in:
parent
a36259dc34
commit
13e35d939d
app/DoctrineMigrations
src/Skobkin/Bundle/PointToolsBundle
40
app/DoctrineMigrations/Version20160328060523.php
Normal file
40
app/DoctrineMigrations/Version20160328060523.php
Normal file
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
|
||||
namespace Application\Migrations;
|
||||
|
||||
use Doctrine\DBAL\Migrations\AbstractMigration;
|
||||
use Doctrine\DBAL\Schema\Schema;
|
||||
|
||||
/**
|
||||
* Auto-generated Migration: Please modify to your needs!
|
||||
*/
|
||||
class Version20160328060523 extends AbstractMigration
|
||||
{
|
||||
/**
|
||||
* @param Schema $schema
|
||||
*/
|
||||
public function up(Schema $schema)
|
||||
{
|
||||
// this up() migration is auto-generated, please modify it to your needs
|
||||
$this->abortIf($this->connection->getDatabasePlatform()->getName() != 'postgresql', 'Migration can only be executed safely on \'postgresql\'.');
|
||||
|
||||
$this->addSql('CREATE SEQUENCE users.rename_log_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
|
||||
$this->addSql('CREATE TABLE users.rename_log (id INT NOT NULL, user_id INT NOT NULL, date TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, old_login TEXT NOT NULL, new_login TEXT NOT NULL, PRIMARY KEY(id))');
|
||||
$this->addSql('CREATE INDEX IDX_10D64DDA76ED395 ON users.rename_log (user_id)');
|
||||
$this->addSql('CREATE INDEX idx_rename_log_date ON users.rename_log (date)');
|
||||
$this->addSql('CREATE INDEX idx_rename_log_old_login ON users.rename_log (old_login)');
|
||||
$this->addSql('ALTER TABLE users.rename_log ADD CONSTRAINT FK_10D64DDA76ED395 FOREIGN KEY (user_id) REFERENCES users.users (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Schema $schema
|
||||
*/
|
||||
public function down(Schema $schema)
|
||||
{
|
||||
// this down() migration is auto-generated, please modify it to your needs
|
||||
$this->abortIf($this->connection->getDatabasePlatform()->getName() != 'postgresql', 'Migration can only be executed safely on \'postgresql\'.');
|
||||
|
||||
$this->addSql('DROP SEQUENCE users.rename_log_id_seq CASCADE');
|
||||
$this->addSql('DROP TABLE users.rename_log');
|
||||
}
|
||||
}
|
174
src/Skobkin/Bundle/PointToolsBundle/Entity/UserRenameEvent.php
Normal file
174
src/Skobkin/Bundle/PointToolsBundle/Entity/UserRenameEvent.php
Normal file
|
@ -0,0 +1,174 @@
|
|||
<?php
|
||||
|
||||
namespace Skobkin\Bundle\PointToolsBundle\Entity;
|
||||
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
|
||||
/**
|
||||
* UserRenameEvent
|
||||
*
|
||||
* @ORM\Table(name="users.rename_log", schema="users", indexes={
|
||||
* @ORM\Index(name="idx_rename_log_date", columns={"date"}),
|
||||
* @ORM\Index(name="idx_rename_log_old_login", columns={"old_login"})
|
||||
* })
|
||||
* @ORM\Entity
|
||||
* @ORM\HasLifecycleCallbacks
|
||||
*/
|
||||
class UserRenameEvent
|
||||
{
|
||||
/**
|
||||
* @var integer
|
||||
*
|
||||
* @ORM\Column(name="id", type="integer")
|
||||
* @ORM\Id
|
||||
* @ORM\GeneratedValue(strategy="AUTO")
|
||||
*/
|
||||
private $id;
|
||||
|
||||
/**
|
||||
* @var User
|
||||
*
|
||||
* @ORM\ManyToOne(targetEntity="Skobkin\Bundle\PointToolsBundle\Entity\User")
|
||||
* @ORM\JoinColumn(name="user_id", nullable=false, onDelete="CASCADE")
|
||||
*/
|
||||
private $user;
|
||||
|
||||
/**
|
||||
* @var \DateTime
|
||||
*
|
||||
* @ORM\Column(name="date", type="datetime")
|
||||
*/
|
||||
private $date;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*
|
||||
* @ORM\Column(name="old_login", type="text")
|
||||
*/
|
||||
private $oldLogin;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*
|
||||
* @ORM\Column(name="new_login", type="text")
|
||||
*/
|
||||
private $newLogin;
|
||||
|
||||
|
||||
public function __construct(User $user, $old, $new)
|
||||
{
|
||||
$this->user = $user;
|
||||
$this->oldLogin = $old;
|
||||
$this->newLogin = $new;
|
||||
}
|
||||
|
||||
/**
|
||||
* @ORM\PrePersist
|
||||
*/
|
||||
public function prePersist()
|
||||
{
|
||||
$this->date = new \DateTime();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get id
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public function getId()
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set date
|
||||
*
|
||||
* @param \DateTime $date
|
||||
* @return UserRenameEvent
|
||||
*/
|
||||
public function setDate($date)
|
||||
{
|
||||
$this->date = $date;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get date
|
||||
*
|
||||
* @return \DateTime
|
||||
*/
|
||||
public function getDate()
|
||||
{
|
||||
return $this->date;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set oldLogin
|
||||
*
|
||||
* @param string $oldLogin
|
||||
* @return UserRenameEvent
|
||||
*/
|
||||
public function setOldLogin($oldLogin)
|
||||
{
|
||||
$this->oldLogin = $oldLogin;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get oldLogin
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getOldLogin()
|
||||
{
|
||||
return $this->oldLogin;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set newLogin
|
||||
*
|
||||
* @param string $newLogin
|
||||
* @return UserRenameEvent
|
||||
*/
|
||||
public function setNewLogin($newLogin)
|
||||
{
|
||||
$this->newLogin = $newLogin;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get newLogin
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getNewLogin()
|
||||
{
|
||||
return $this->newLogin;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set user
|
||||
*
|
||||
* @param User $user
|
||||
* @return UserRenameEvent
|
||||
*/
|
||||
public function setUser(User $user)
|
||||
{
|
||||
$this->user = $user;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get user
|
||||
*
|
||||
* @return User
|
||||
*/
|
||||
public function getUser()
|
||||
{
|
||||
return $this->user;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
<?php
|
||||
|
||||
namespace Skobkin\Bundle\PointToolsBundle\EventListener;
|
||||
|
||||
use Doctrine\Common\EventSubscriber;
|
||||
// For new doctrine: https://symfony.com/doc/current/cookbook/doctrine/event_listeners_subscribers.html#creating-the-subscriber-class
|
||||
//use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
|
||||
//use Doctrine\Common\Persistence\Event\PreUpdateEventArgs;
|
||||
use Doctrine\ORM\Event\PostFlushEventArgs;
|
||||
use Doctrine\ORM\Event\PreUpdateEventArgs;
|
||||
use Skobkin\Bundle\PointToolsBundle\Entity\User;
|
||||
use Skobkin\Bundle\PointToolsBundle\Entity\UserRenameEvent;
|
||||
|
||||
class UserRenameSubscriber implements EventSubscriber
|
||||
{
|
||||
/**
|
||||
* @var UserRenameEvent[]
|
||||
*/
|
||||
private $items = [];
|
||||
|
||||
public function getSubscribedEvents()
|
||||
{
|
||||
return [
|
||||
'preUpdate',
|
||||
'postFlush',
|
||||
];
|
||||
}
|
||||
|
||||
public function preUpdate(PreUpdateEventArgs $event)
|
||||
{
|
||||
/** @var User $entity */
|
||||
$entity = $event->getObject();
|
||||
|
||||
if (!$entity instanceof User) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($event->hasChangedField('login')) {
|
||||
$old = $event->getOldValue('login');
|
||||
$new = $event->getNewValue('login');
|
||||
|
||||
$this->items[] = new UserRenameEvent($entity, $old, $new);
|
||||
}
|
||||
}
|
||||
|
||||
public function postFlush(PostFlushEventArgs $event)
|
||||
{
|
||||
if (0 !== count($this->items)) {
|
||||
$em = $event->getEntityManager();
|
||||
|
||||
foreach ($this->items as $item) {
|
||||
$em->persist($item);
|
||||
}
|
||||
|
||||
$this->items = [];
|
||||
|
||||
$em->flush();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -51,10 +51,17 @@ services:
|
|||
- @skobkin__point_tools.service_factory.comment_factory
|
||||
- @skobkin__point_tools.service_factory.tag_factory
|
||||
|
||||
# Custom Markdown parser
|
||||
markdown.parser.point:
|
||||
class: Skobkin\Bundle\PointToolsBundle\Service\Markdown\PointParser
|
||||
arguments:
|
||||
- []
|
||||
- @router
|
||||
tags:
|
||||
- { name: markdown.parser }
|
||||
- { name: markdown.parser }
|
||||
|
||||
# Event listener
|
||||
point_tools.event_listener.user_rename_subscriber:
|
||||
class: Skobkin\Bundle\PointToolsBundle\EventListener\UserRenameSubscriber
|
||||
tags:
|
||||
- { name: doctrine.event_subscriber, connection: default }
|
||||
|
|
Loading…
Reference in a new issue