Skip to content

Commit

Permalink
Add isActive field and UserDeactivatedException for user management.
Browse files Browse the repository at this point in the history
  • Loading branch information
deeravenger committed Nov 30, 2024
1 parent ae9d4ff commit 4d83f2d
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/EconumoBundle/Domain/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface
*/
private string $password;

private bool $isActive = true;

/**
* @var Collection|self[]
*/
Expand Down Expand Up @@ -175,6 +177,10 @@ public function connectUser(self $user): void
return;
}

if (!$user->isActive()) {
return;
}

$this->connections->add($user);
}

Expand All @@ -189,6 +195,7 @@ public function deleteConnection(self $user): void
public function getConnections(): Collection
{
$criteria = Criteria::create()
->where(Criteria::expr()->eq('isActive', true))
->orderBy(['name' => Criteria::ASC]);
return $this->connections->matching($criteria);
}
Expand Down Expand Up @@ -291,4 +298,17 @@ public function updateDefaultBudget(?Id $budgetId): void
}
}
}

public function isActive(): bool
{
return $this->isActive;
}

public function deactivate(): void
{
if ($this->isActive()) {
$this->isActive = false;
$this->updatedAt = new DateTime();
}
}
}
11 changes: 11 additions & 0 deletions src/EconumoBundle/Domain/Exception/UserDeactivatedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace App\EconumoBundle\Domain\Exception;

use App\EconumoBundle\Domain\Exception\DomainException;

class UserDeactivatedException extends DomainException
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
<field name="avatarUrl" type="string" column="avatar_url" nullable="false"/>
<field name="password" type="string" column="password" nullable="false"/>
<field name="salt" type="string" column="salt" length="40" nullable="false"/>
<field name="isActive" type="boolean" column="is_active" nullable="false">
<options>
<option name="default">1</option>
</options>
</field>
<field name="createdAt" type="datetime_immutable" column="created_at" nullable="false"/>
<field name="updatedAt" type="datetime" column="updated_at" nullable="false"/>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace App\EconumoBundle\Infrastructure\Doctrine\Migration;

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

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20241130222656 extends AbstractMigration
{
public function getDescription() : string
{
return '';
}

public function up(Schema $schema) : void
{
// this up() migration is auto-generated, please modify it to your needs
$this->skipIf($this->connection->getDatabasePlatform()->getName() !== 'sqlite', "Migration can only be executed safely on 'sqlite'.");

$this->addSql("ALTER TABLE users ADD COLUMN is_active BOOLEAN DEFAULT '1' NOT NULL");
}

public function down(Schema $schema) : void
{
// this down() migration is auto-generated, please modify it to your needs
$this->skipIf($this->connection->getDatabasePlatform()->getName() !== 'sqlite', "Migration can only be executed safely on 'sqlite'.");

$this->addSql('ALTER TABLE users DROP COLUMN is_active');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use App\EconumoBundle\Domain\Entity\ValueObject\Id;
use App\EconumoBundle\Domain\Entity\ValueObject\Identifier;
use App\EconumoBundle\Domain\Exception\NotFoundException;
use App\EconumoBundle\Domain\Exception\UserDeactivatedException;
use App\EconumoBundle\Domain\Repository\UserRepositoryInterface;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\Exception\ORMException;
Expand Down Expand Up @@ -78,6 +79,10 @@ public function loadByIdentifier(Identifier $identifier): User
throw new NotFoundException(sprintf('User with identifier %s not found', $identifier));
}

if (!$user->isActive()) {
throw new UserDeactivatedException('User is deactivated');
}

return $user;
}

Expand All @@ -88,6 +93,10 @@ public function get(Id $id): User
throw new NotFoundException(sprintf('User with ID %s not found', $id));
}

if (!$item->isActive()) {
throw new UserDeactivatedException('User is deactivated');
}

return $item;
}

Expand All @@ -98,6 +107,10 @@ public function getByEmail(Email $email): User
throw new NotFoundException(sprintf('User with email %s not found', $email));
}

if (!$user->isActive()) {
throw new UserDeactivatedException('User is deactivated');
}

return $user;
}

Expand All @@ -108,6 +121,6 @@ public function getReference(Id $id): User

public function getAll(): array
{
return $this->findAll();
return $this->findBy(['isActive' => true]);
}
}

0 comments on commit 4d83f2d

Please sign in to comment.