diff --git a/config/doctrine/User.orm.yml b/config/doctrine/User.orm.yml index 774ef902..90ddaab7 100644 --- a/config/doctrine/User.orm.yml +++ b/config/doctrine/User.orm.yml @@ -30,6 +30,10 @@ App\Entity\User: type: boolean options: default: 0 + enabled: + type: boolean + options: + default: 1 lastLoginTime: type: datetime nullable: true diff --git a/src/Admin/UserAdmin.php b/src/Admin/UserAdmin.php index 5958bc0d..b59d80e4 100644 --- a/src/Admin/UserAdmin.php +++ b/src/Admin/UserAdmin.php @@ -26,7 +26,8 @@ class UserAdmin extends Admin { use DomainGuesserAwareTrait; - protected function generateBaseRoutePattern(bool $isChildAdmin = false): string { + protected function generateBaseRoutePattern(bool $isChildAdmin = false): string + { return 'user'; } @@ -56,6 +57,7 @@ protected function configureFormFields(FormMapper $form): void $userId = (null === $user) ? null : $user->getId(); $form + ->add('enabled', CheckboxType::class, ['required' => false]) ->add('email', EmailType::class, ['disabled' => !$this->isNewObject()]) ->add('plainPassword', PasswordType::class, [ 'label' => 'form.password', @@ -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, + ]) ->add('email', null, [ 'show_filter' => true, ]) @@ -174,6 +184,8 @@ protected function configureListFields(ListMapper $list): void 'name' => 'edit', ], ]) + ->add('enabled') + ->add('deleted') ->addIdentifier('email', null, [ 'route' => [ 'name' => 'edit', @@ -187,8 +199,7 @@ protected function configureListFields(ListMapper $list): void ->add('recoverySecretBox', 'boolean', [ 'label' => 'Recovery Token', ]) - ->add('mailCrypt') - ->add('deleted'); + ->add('mailCrypt'); } /** diff --git a/src/Command/UsersCheckPasswordCommand.php b/src/Command/UsersCheckPasswordCommand.php index 9e3152f1..9313b0cd 100644 --- a/src/Command/UsersCheckPasswordCommand.php +++ b/src/Command/UsersCheckPasswordCommand.php @@ -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; } diff --git a/src/Entity/User.php b/src/Entity/User.php index bf56406d..ac2b859b 100644 --- a/src/Entity/User.php +++ b/src/Entity/User.php @@ -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; @@ -38,6 +39,7 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface, Passwor use CreationTimeTrait; use UpdatedTimeTrait; use EmailTrait; + use EnableTrait; use QuotaTrait; use PasswordTrait; use SaltTrait; @@ -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; @@ -113,7 +116,8 @@ public function getUsername(): ?string /** * @return string */ - public function getUserIdentifier(): string { + public function getUserIdentifier(): string + { return $this->email; } diff --git a/src/Repository/UserRepository.php b/src/Repository/UserRepository.php index fdc33c5b..747d6655 100644 --- a/src/Repository/UserRepository.php +++ b/src/Repository/UserRepository.php @@ -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 { return $this->findOneBy(['email' => $email]); } @@ -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')); $expression = $expressionBuilder->andX( $expressionBuilder->eq('deleted', 0), $expressionBuilder->orX( @@ -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)) )->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)) )->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)) )->count(); } diff --git a/src/Traits/EnableTrait.php b/src/Traits/EnableTrait.php new file mode 100644 index 00000000..d2cafe70 --- /dev/null +++ b/src/Traits/EnableTrait.php @@ -0,0 +1,26 @@ +enabled; + } + + public function getEnabled(): bool + { + return $this->enabled; + } + + public function setEnabled(bool $enabled): void + { + $this->enabled = $enabled; + } +}