diff --git a/app/DoctrineMigrations/Version20160328060523.php b/app/DoctrineMigrations/Version20160328060523.php new file mode 100644 index 0000000..1b60693 --- /dev/null +++ b/app/DoctrineMigrations/Version20160328060523.php @@ -0,0 +1,40 @@ +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'); + } +} diff --git a/src/Skobkin/Bundle/PointToolsBundle/Entity/UserRenameEvent.php b/src/Skobkin/Bundle/PointToolsBundle/Entity/UserRenameEvent.php new file mode 100644 index 0000000..46edae2 --- /dev/null +++ b/src/Skobkin/Bundle/PointToolsBundle/Entity/UserRenameEvent.php @@ -0,0 +1,174 @@ +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; + } +} diff --git a/src/Skobkin/Bundle/PointToolsBundle/EventListener/UserRenameSubscriber.php b/src/Skobkin/Bundle/PointToolsBundle/EventListener/UserRenameSubscriber.php new file mode 100644 index 0000000..fc9636b --- /dev/null +++ b/src/Skobkin/Bundle/PointToolsBundle/EventListener/UserRenameSubscriber.php @@ -0,0 +1,60 @@ +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(); + } + } +} \ No newline at end of file diff --git a/src/Skobkin/Bundle/PointToolsBundle/Resources/config/services.yml b/src/Skobkin/Bundle/PointToolsBundle/Resources/config/services.yml index caa2d3e..442ff83 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Resources/config/services.yml +++ b/src/Skobkin/Bundle/PointToolsBundle/Resources/config/services.yml @@ -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 } \ No newline at end of file + - { 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 }