Skip to content

Commit

Permalink
Add Migration for Enabled Field
Browse files Browse the repository at this point in the history
  • Loading branch information
0x46616c6b committed Dec 20, 2023
1 parent 81aefaf commit 4f78d5d
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 0 deletions.
37 changes: 37 additions & 0 deletions migrations/Version20231220194844.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

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

/**
* Add enabled field to User entity.
*/
final class Version20231220194844 extends AbstractMigration
{
public function getDescription(): string
{
return 'Add enabled field to User entity.';
}

public function up(Schema $schema): void
{
if ($schema->getTable('virtual_users')->hasColumn('enabled')) {
return;
}

$this->addSql('ALTER TABLE virtual_users ADD enabled TINYINT(1) NOT NULL');
}

public function down(Schema $schema): void
{
if (!$schema->getTable('virtual_users')->hasColumn('enabled')) {
return;
}

$this->addSql('ALTER TABLE virtual_users DROP enabled');
}
}
53 changes: 53 additions & 0 deletions migrations/Version20231220203032.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use App\Entity\User;
use App\Enum\Roles;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Remove Roles::SPAM from users and disable them.
*/
final class Version20231220203032 extends AbstractMigration implements ContainerAwareInterface
{
private $container;

public function setContainer(ContainerInterface $container = null): void
{
$this->container = $container;
}

public function getDescription(): string
{
return 'Remove Roles::SPAM from users and disable them.';
}

public function up(Schema $schema): void
{
$entityManager = $this->container->get('doctrine.orm.entity_manager');
/** @var \App\Entity\User[] $users */
$users = $entityManager->getRepository(User::class)->findAll();
foreach ($users as $user) {
if (!$user->hasRole(Roles::SPAM) || $user->isDeleted()) {
continue;
}

$user->setRoles(array_filter($user->getRoles(), function ($role) {
return $role !== Roles::SPAM;
}));
$user->setEnabled(false);
$entityManager->persist($user);
}
$entityManager->flush();
}

public function down(Schema $schema): void
{
}
}
1 change: 1 addition & 0 deletions src/DataFixtures/LoadUserData.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class LoadUserData extends Fixture implements OrderedFixtureInterface, Container
['email' => 'user@example.org', 'roles' => [Roles::USER]],
['email' => 'support@example.org', 'roles' => [Roles::MULTIPLIER]],
['email' => 'suspicious@example.org', 'roles' => [Roles::SUSPICIOUS]],
['email' => 'spammer@example.org', 'roles' => [Roles::SPAM]],
['email' => 'domain@example.com', 'roles' => [Roles::DOMAIN_ADMIN]],
];

Expand Down
1 change: 1 addition & 0 deletions src/Enum/Roles.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ final class Roles
{
public const PERMANENT = 'ROLE_PERMANENT';
public const MULTIPLIER = 'ROLE_MULTIPLIER';
/** @deprecated */
public const SPAM = 'ROLE_SPAM';
public const SUSPICIOUS = 'ROLE_SUSPICIOUS';
public const USER = 'ROLE_USER';
Expand Down

0 comments on commit 4f78d5d

Please sign in to comment.