Skip to content

Commit

Permalink
Add Enabled Field to User
Browse files Browse the repository at this point in the history
  • Loading branch information
0x46616c6b committed Dec 20, 2023
1 parent a378d8a commit 68d19ca
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 20 deletions.
4 changes: 4 additions & 0 deletions config/doctrine/User.orm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ App\Entity\User:
type: boolean
options:
default: 0
enabled:
type: boolean
options:
default: 1
lastLoginTime:
type: datetime
nullable: true
Expand Down
17 changes: 14 additions & 3 deletions src/Admin/UserAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ class UserAdmin extends Admin
{
use DomainGuesserAwareTrait;

protected function generateBaseRoutePattern(bool $isChildAdmin = false): string {
protected function generateBaseRoutePattern(bool $isChildAdmin = false): string
{

Check warning on line 30 in src/Admin/UserAdmin.php

View check run for this annotation

Codecov / codecov/patch

src/Admin/UserAdmin.php#L29-L30

Added lines #L29 - L30 were not covered by tests
return 'user';
}

Expand Down Expand Up @@ -56,6 +57,7 @@ protected function configureFormFields(FormMapper $form): void
$userId = (null === $user) ? null : $user->getId();

$form
->add('enabled', CheckboxType::class, ['required' => false])

Check warning on line 60 in src/Admin/UserAdmin.php

View check run for this annotation

Codecov / codecov/patch

src/Admin/UserAdmin.php#L60

Added line #L60 was not covered by tests
->add('email', EmailType::class, ['disabled' => !$this->isNewObject()])
->add('plainPassword', PasswordType::class, [
'label' => 'form.password',
Expand Down Expand Up @@ -101,6 +103,14 @@ protected function configureFormFields(FormMapper $form): void
protected function configureDatagridFilters(DatagridMapper $filter): void
{
$filter
->add('enabled', ChoiceFilter::class, [
'field_options' => [
'required' => false,
'choices' => [0 => 'No', 1 => 'Yes'],
],
'field_type' => ChoiceType::class,
'show_filter' => true,
])

Check warning on line 113 in src/Admin/UserAdmin.php

View check run for this annotation

Codecov / codecov/patch

src/Admin/UserAdmin.php#L106-L113

Added lines #L106 - L113 were not covered by tests
->add('email', null, [
'show_filter' => true,
])
Expand Down Expand Up @@ -174,6 +184,8 @@ protected function configureListFields(ListMapper $list): void
'name' => 'edit',
],
])
->add('enabled')
->add('deleted')

Check warning on line 188 in src/Admin/UserAdmin.php

View check run for this annotation

Codecov / codecov/patch

src/Admin/UserAdmin.php#L187-L188

Added lines #L187 - L188 were not covered by tests
->addIdentifier('email', null, [
'route' => [
'name' => 'edit',
Expand All @@ -187,8 +199,7 @@ protected function configureListFields(ListMapper $list): void
->add('recoverySecretBox', 'boolean', [
'label' => 'Recovery Token',
])
->add('mailCrypt')
->add('deleted');
->add('mailCrypt');

Check warning on line 202 in src/Admin/UserAdmin.php

View check run for this annotation

Codecov / codecov/patch

src/Admin/UserAdmin.php#L202

Added line #L202 was not covered by tests
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Command/UsersCheckPasswordCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ protected function execute(InputInterface $input, OutputInterface $output): ?int
}

// block spammers from login but not lookup
if (false === $userDbLookup && $user->hasRole(Roles::SPAM)) {
if (false === $userDbLookup && ($user->hasRole(Roles::SPAM) || !$user->isEnabled())) {
return 1;
}

Expand Down
6 changes: 5 additions & 1 deletion src/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Traits\DeleteTrait;
use App\Traits\DomainAwareTrait;
use App\Traits\EmailTrait;
use App\Traits\EnableTrait;
use App\Traits\IdTrait;
use App\Traits\InvitationVoucherTrait;
use App\Traits\LastLoginTimeTrait;
Expand Down Expand Up @@ -38,6 +39,7 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface, Passwor
use CreationTimeTrait;
use UpdatedTimeTrait;
use EmailTrait;
use EnableTrait;
use QuotaTrait;
use PasswordTrait;
use SaltTrait;
Expand Down Expand Up @@ -68,6 +70,7 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface, Passwor
public function __construct()
{
$this->deleted = false;
$this->enabled = true;
$this->passwordVersion = self::CURRENT_PASSWORD_VERSION;
$currentDateTime = new \DateTime();
$this->creationTime = $currentDateTime;
Expand Down Expand Up @@ -113,7 +116,8 @@ public function getUsername(): ?string
/**
* @return string
*/
public function getUserIdentifier(): string {
public function getUserIdentifier(): string
{
return $this->email;
}

Expand Down
33 changes: 18 additions & 15 deletions src/Repository/UserRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
class UserRepository extends AbstractRepository implements PasswordUpgraderInterface
{
/**
* @param $email
*
* @return object|User|null
* @param string $email
*
* @return User|null
*/
public function findByEmail($email)
public function findByEmail(string $email): ?User

Check warning on line 19 in src/Repository/UserRepository.php

View check run for this annotation

Codecov / codecov/patch

src/Repository/UserRepository.php#L19

Added line #L19 was not covered by tests
{
return $this->findOneBy(['email' => $email]);
}
Expand All @@ -40,7 +40,7 @@ public function findInactiveUsers(int $days)
$expression = $expressionBuilder->eq('deleted', 0);
} else {
$dateTime = new \DateTime();
$dateTime->sub(new \DateInterval('P'.$days.'D'));
$dateTime->sub(new \DateInterval('P' . $days . 'D'));

Check warning on line 43 in src/Repository/UserRepository.php

View check run for this annotation

Codecov / codecov/patch

src/Repository/UserRepository.php#L43

Added line #L43 was not covered by tests
$expression = $expressionBuilder->andX(
$expressionBuilder->eq('deleted', 0),
$expressionBuilder->orX(
Expand Down Expand Up @@ -78,26 +78,29 @@ public function countDeletedUsers(): int

public function countUsersWithRecoveryToken(): int
{
return $this->matching(Criteria::create()
->where(Criteria::expr()->eq('deleted', false))
->andWhere(Criteria::expr()->neq('recoverySecretBox', null))
return $this->matching(
Criteria::create()
->where(Criteria::expr()->eq('deleted', false))
->andWhere(Criteria::expr()->neq('recoverySecretBox', null))

Check warning on line 84 in src/Repository/UserRepository.php

View check run for this annotation

Codecov / codecov/patch

src/Repository/UserRepository.php#L81-L84

Added lines #L81 - L84 were not covered by tests
)->count();
}

public function countUsersWithMailCrypt(): int
{
return $this->matching(Criteria::create()
->where(Criteria::expr()->eq('deleted', false))
->andWhere(Criteria::expr()->eq('mailCrypt', true))
return $this->matching(
Criteria::create()
->where(Criteria::expr()->eq('deleted', false))
->andWhere(Criteria::expr()->eq('mailCrypt', true))

Check warning on line 93 in src/Repository/UserRepository.php

View check run for this annotation

Codecov / codecov/patch

src/Repository/UserRepository.php#L90-L93

Added lines #L90 - L93 were not covered by tests
)->count();
}

public function countUsersWithTwofactor(): int
{
return $this->matching(Criteria::create()
->where(Criteria::expr()->eq('deleted', false))
->andWhere(Criteria::expr()->eq('totpConfirmed', 1))
->andWhere(Criteria::expr()->neq('totpSecret', null))
return $this->matching(
Criteria::create()
->where(Criteria::expr()->eq('deleted', false))
->andWhere(Criteria::expr()->eq('totpConfirmed', 1))
->andWhere(Criteria::expr()->neq('totpSecret', null))

Check warning on line 103 in src/Repository/UserRepository.php

View check run for this annotation

Codecov / codecov/patch

src/Repository/UserRepository.php#L99-L103

Added lines #L99 - L103 were not covered by tests
)->count();
}

Expand Down
26 changes: 26 additions & 0 deletions src/Traits/EnableTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App\Traits;

trait EnableTrait
{
/**
* @var bool;
*/
private $enabled;

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

public function getEnabled(): bool

Check warning on line 17 in src/Traits/EnableTrait.php

View check run for this annotation

Codecov / codecov/patch

src/Traits/EnableTrait.php#L17

Added line #L17 was not covered by tests
{
return $this->enabled;

Check warning on line 19 in src/Traits/EnableTrait.php

View check run for this annotation

Codecov / codecov/patch

src/Traits/EnableTrait.php#L19

Added line #L19 was not covered by tests
}

public function setEnabled(bool $enabled): void

Check warning on line 22 in src/Traits/EnableTrait.php

View check run for this annotation

Codecov / codecov/patch

src/Traits/EnableTrait.php#L22

Added line #L22 was not covered by tests
{
$this->enabled = $enabled;
}

Check warning on line 25 in src/Traits/EnableTrait.php

View check run for this annotation

Codecov / codecov/patch

src/Traits/EnableTrait.php#L24-L25

Added lines #L24 - L25 were not covered by tests
}

0 comments on commit 68d19ca

Please sign in to comment.