Skip to content

Commit 113bf55

Browse files
authored
Merge pull request #359 from dotkernel/issue-523
updated user status, removed isDeleted
2 parents 65b0a56 + 8289a9e commit 113bf55

File tree

11 files changed

+14
-48
lines changed

11 files changed

+14
-48
lines changed

data/doctrine/fixtures/UserLoader.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ public function load(ObjectManager $manager): void
3434
->setIdentity('[email protected]')
3535
->usePassword('dotkernel')
3636
->setStatus(UserStatusEnum::Active)
37-
->setIsDeleted(false)
3837
->setHash(User::generateHash())
3938
->addRole($guestRole)
4039
->addRole($userRole);

data/doctrine/migrations/Version20241030082958.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function up(Schema $schema): void
3030
$this->addSql('CREATE TABLE oauth_clients (id INT UNSIGNED AUTO_INCREMENT NOT NULL, name VARCHAR(40) NOT NULL, secret VARCHAR(100) DEFAULT NULL, redirect VARCHAR(191) NOT NULL, revoked TINYINT(1) DEFAULT 0 NOT NULL, isConfidential TINYINT(1) DEFAULT 0 NOT NULL, user_id BINARY(16) DEFAULT NULL, INDEX IDX_13CE8101A76ED395 (user_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4');
3131
$this->addSql('CREATE TABLE oauth_refresh_tokens (id INT UNSIGNED AUTO_INCREMENT NOT NULL, revoked TINYINT(1) DEFAULT 0 NOT NULL, expires_at DATETIME NOT NULL, access_token_id INT UNSIGNED DEFAULT NULL, INDEX IDX_5AB6872CCB2688 (access_token_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4');
3232
$this->addSql('CREATE TABLE oauth_scopes (id INT UNSIGNED AUTO_INCREMENT NOT NULL, scope VARCHAR(191) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4');
33-
$this->addSql('CREATE TABLE user (uuid BINARY(16) NOT NULL, identity VARCHAR(191) NOT NULL, password VARCHAR(191) NOT NULL, status ENUM(\'active\', \'pending\') DEFAULT \'pending\' NOT NULL, isDeleted TINYINT(1) NOT NULL, hash VARCHAR(64) NOT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, UNIQUE INDEX UNIQ_8D93D6496A95E9C4 (identity), UNIQUE INDEX UNIQ_8D93D649D1B862B8 (hash), PRIMARY KEY(uuid)) DEFAULT CHARACTER SET utf8mb4');
33+
$this->addSql('CREATE TABLE user (uuid BINARY(16) NOT NULL, identity VARCHAR(191) NOT NULL, password VARCHAR(191) NOT NULL, status ENUM(\'active\', \'pending\', \'deleted\') DEFAULT \'pending\' NOT NULL, hash VARCHAR(64) NOT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, UNIQUE INDEX UNIQ_8D93D6496A95E9C4 (identity), UNIQUE INDEX UNIQ_8D93D649D1B862B8 (hash), PRIMARY KEY(uuid)) DEFAULT CHARACTER SET utf8mb4');
3434
$this->addSql('CREATE TABLE user_roles (userUuid BINARY(16) NOT NULL, roleUuid BINARY(16) NOT NULL, INDEX IDX_54FCD59FD73087E9 (userUuid), INDEX IDX_54FCD59F88446210 (roleUuid), PRIMARY KEY(userUuid, roleUuid)) DEFAULT CHARACTER SET utf8mb4');
3535
$this->addSql('CREATE TABLE user_avatar (uuid BINARY(16) NOT NULL, name VARCHAR(191) NOT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, userUuid BINARY(16) DEFAULT NULL, UNIQUE INDEX UNIQ_73256912D73087E9 (userUuid), PRIMARY KEY(uuid)) DEFAULT CHARACTER SET utf8mb4');
3636
$this->addSql('CREATE TABLE user_detail (uuid BINARY(16) NOT NULL, firstName VARCHAR(191) DEFAULT NULL, lastName VARCHAR(191) DEFAULT NULL, email VARCHAR(191) NOT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, userUuid BINARY(16) DEFAULT NULL, UNIQUE INDEX UNIQ_4B5464AED73087E9 (userUuid), PRIMARY KEY(uuid)) DEFAULT CHARACTER SET utf8mb4');

src/User/src/Entity/User.php

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,6 @@ class User extends AbstractEntity implements UserEntityInterface
5151
#[ORM\Column(type: 'user_status_enum', options: ['default' => UserStatusEnum::Pending])]
5252
protected UserStatusEnum $status = UserStatusEnum::Pending;
5353

54-
#[ORM\Column(name: "isDeleted", type: "boolean")]
55-
protected bool $isDeleted = false;
56-
5754
#[ORM\Column(name: "hash", type: "string", length: 64, unique: true)]
5855
protected string $hash;
5956

@@ -103,18 +100,6 @@ public function setStatus(UserStatusEnum $status): self
103100
return $this;
104101
}
105102

106-
public function isDeleted(): bool
107-
{
108-
return $this->isDeleted;
109-
}
110-
111-
public function setIsDeleted(bool $isDeleted): self
112-
{
113-
$this->isDeleted = $isDeleted;
114-
115-
return $this;
116-
}
117-
118103
public function getHash(): string
119104
{
120105
return $this->hash;
@@ -273,11 +258,9 @@ public function isPending(): bool
273258
return $this->status === UserStatusEnum::Pending;
274259
}
275260

276-
public function markAsDeleted(): self
261+
public function isDeleted(): bool
277262
{
278-
$this->isDeleted = true;
279-
280-
return $this;
263+
return $this->status === UserStatusEnum::Deleted;
281264
}
282265

283266
public function renewHash(): self
@@ -311,7 +294,6 @@ public function getArrayCopy(): array
311294
'hash' => $this->getHash(),
312295
'identity' => $this->getIdentity(),
313296
'status' => $this->getStatus(),
314-
'isDeleted' => $this->isDeleted(),
315297
'avatar' => $this->getAvatar()?->getArrayCopy(),
316298
'detail' => $this->getDetail()->getArrayCopy(),
317299
'roles' => $this->getRoles()->map(function (UserRole $userRole) {

src/User/src/Enum/UserStatusEnum.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ enum UserStatusEnum: string
88
{
99
case Active = 'active';
1010
case Pending = 'pending';
11+
case Deleted = 'deleted';
1112
}

src/User/src/OpenAPI.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1067,7 +1067,6 @@
10671067
new OA\Property(property: 'hash', type: 'string'),
10681068
new OA\Property(property: 'identity', type: 'string'),
10691069
new OA\Property(property: 'status', type: 'string', example: UserStatusEnum::Active),
1070-
new OA\Property(property: 'isDeleted', type: 'boolean', example: false),
10711070
new OA\Property(property: 'avatar', ref: '#/components/schemas/UserAvatar', nullable: true),
10721071
new OA\Property(property: 'detail', ref: '#/components/schemas/UserDetail'),
10731072
new OA\Property(

src/User/src/Repository/UserRepository.php

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,6 @@ public function getUsers(array $filters = []): UserCollection
5252
$qb->andWhere('user.status = :status')->setParameter('status', $filters['status']);
5353
}
5454

55-
if (isset($filters['deleted'])) {
56-
switch ($filters['deleted']) {
57-
case 'true':
58-
$qb->andWhere('user.isDeleted = :isDeleted')->setParameter('isDeleted', true);
59-
break;
60-
case 'false':
61-
$qb->andWhere('user.isDeleted = :isDeleted')->setParameter('isDeleted', false);
62-
break;
63-
}
64-
}
65-
6655
if (! empty($filters['search'])) {
6756
$qb->andWhere(
6857
$qb->expr()->orX(
@@ -78,6 +67,8 @@ public function getUsers(array $filters = []): UserCollection
7867
$qb->andWhere('roles.name = :role')->setParameter('role', $filters['role']);
7968
}
8069

70+
//ignore deleted users
71+
$qb->andWhere('user.status != :status')->setParameter('status', UserStatusEnum::Deleted);
8172
$qb->getQuery()->useQueryCache(true);
8273

8374
return new UserCollection($qb, false);
@@ -115,8 +106,9 @@ public function getUserEntityByUserCredentials(
115106
$qb->select(['u.password', 'u.status'])
116107
->from(User::class, 'u')
117108
->andWhere('u.identity = :identity')
118-
->andWhere('u.isDeleted = 0')
119-
->setParameter('identity', $username);
109+
->andWhere('u.status != :status')
110+
->setParameter('identity', $username)
111+
->setParameter('status', UserStatusEnum::Deleted);
120112
break;
121113
default:
122114
throw new OAuthServerException(Message::INVALID_CLIENT_ID, 6, 'invalid_client', 401);

src/User/src/Service/UserService.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public function deleteUser(User $user): User
115115
{
116116
$this->revokeTokens($user);
117117

118-
return $this->anonymizeUser($user->markAsDeleted());
118+
return $this->anonymizeUser($user->setStatus(UserStatusEnum::Deleted));
119119
}
120120

121121
/**
@@ -198,7 +198,7 @@ public function findResetPasswordByHash(?string $hash): UserResetPassword
198198
public function findByEmail(string $email): User
199199
{
200200
$user = $this->userDetailRepository->findOneBy(['email' => $email])?->getUser();
201-
if (! $user instanceof User) {
201+
if (! $user instanceof User || $user->isDeleted()) {
202202
throw new NotFoundException(Message::USER_NOT_FOUND);
203203
}
204204

@@ -219,7 +219,7 @@ public function findByIdentity(string $identity): ?User
219219
public function findOneBy(array $params = []): User
220220
{
221221
$user = $this->userRepository->findOneBy($params);
222-
if (! $user instanceof User) {
222+
if (! $user instanceof User || $user->isDeleted()) {
223223
throw new NotFoundException(Message::USER_NOT_FOUND);
224224
}
225225

@@ -370,10 +370,6 @@ public function updateUser(User $user, array $data = []): User
370370
$user->setStatus($data['status']);
371371
}
372372

373-
if (isset($data['isDeleted'])) {
374-
$user->setIsDeleted($data['isDeleted']);
375-
}
376-
377373
if (isset($data['hash'])) {
378374
$user->setHash($data['hash']);
379375
}

test/Functional/AdminTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,15 +401,13 @@ public function testAdminCanCreateUserAccount(): void
401401
$this->assertArrayHasKey('hash', $data);
402402
$this->assertArrayHasKey('identity', $data);
403403
$this->assertArrayHasKey('status', $data);
404-
$this->assertArrayHasKey('isDeleted', $data);
405404
$this->assertArrayHasKey('avatar', $data);
406405
$this->assertArrayHasKey('detail', $data);
407406
$this->assertArrayHasKey('roles', $data);
408407
$this->assertNotEmpty($data['uuid']);
409408
$this->assertNotEmpty($data['hash']);
410409
$this->assertSame($userData['identity'], $data['identity']);
411410
$this->assertSame(UserStatusEnum::Pending->value, $data['status']);
412-
$this->assertFalse($data['isDeleted']);
413411
$this->assertEmpty($data['avatar']);
414412
$this->assertEmpty($data['resetPasswords']);
415413
$this->assertArrayHasKey('firstName', $data['detail']);

test/Functional/UserTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ public function testRegisterAccount(): void
108108

109109
$this->assertSame($user['identity'], $data['identity']);
110110
$this->assertSame(UserStatusEnum::Pending->value, $data['status']);
111-
$this->assertFalse($data['isDeleted']);
112111
$this->assertArrayHasKey('detail', $data);
113112
$this->assertArrayHasKey('email', $data['detail']);
114113
$this->assertArrayHasKey('firstName', $data['detail']);

test/Unit/App/Middleware/AuthorizationMiddlewareTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Api\App\UserIdentity;
1414
use Api\User\Entity\User;
1515
use Api\User\Entity\UserRole;
16+
use Api\User\Enum\UserStatusEnum;
1617
use Api\User\Repository\UserRepository;
1718
use Fig\Http\Message\StatusCodeInterface;
1819
use Laminas\Diactoros\ServerRequest;
@@ -108,7 +109,7 @@ public function testAuthorizationInactiveUser(): void
108109

109110
public function testAuthorizationUserNotFoundOrDeleted(): void
110111
{
111-
$user = (new User())->markAsDeleted();
112+
$user = (new User())->setStatus(UserStatusEnum::Deleted);
112113
$this->userRepository->method('findOneBy')->willReturn($user);
113114
$this->authorization->method('isGranted')->willReturn(false);
114115

0 commit comments

Comments
 (0)