Skip to content

Commit

Permalink
feat(TwoFactorBundle): Added console command to disable 2FA and overr…
Browse files Browse the repository at this point in the history
…ide users:list command
  • Loading branch information
ambroisemaupate committed Jun 27, 2023
1 parent 48fb561 commit 81cd472
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/RoadizTwoFactorBundle/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,6 @@ services:

scheb_two_factor.backup_code_manager:
alias: RZ\Roadiz\TwoFactorBundle\Backup\BackupCodeManager

RZ\Roadiz\CoreBundle\Console\UsersCommand:
alias: RZ\Roadiz\TwoFactorBundle\Console\UsersCommand
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

namespace RZ\Roadiz\TwoFactorBundle\Console;

use Doctrine\Persistence\ManagerRegistry;
use RZ\Roadiz\CoreBundle\Console\UsersCommand;
use RZ\Roadiz\TwoFactorBundle\Entity\TwoFactorUser;
use RZ\Roadiz\TwoFactorBundle\Security\Provider\TwoFactorUserProviderInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

#[AsCommand(
name: 'users:2fa:disable',
description: 'Disable two-factor authentication for a user.',
)]
final class DisableTwoFactorUserCommand extends UsersCommand
{
private TwoFactorUserProviderInterface $twoFactorUserProvider;

public function __construct(
ManagerRegistry $managerRegistry,
TwoFactorUserProviderInterface $twoFactorUserProvider,
string $name = null
) {
parent::__construct($managerRegistry, $name);
$this->twoFactorUserProvider = $twoFactorUserProvider;
$this->managerRegistry = $managerRegistry;
}

protected function configure(): void
{
$this->addArgument(
'username',
InputArgument::REQUIRED,
'Username'
);
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$name = $input->getArgument('username');
$user = $this->getUserForInput($input);

$twoFactorUser = $this->twoFactorUserProvider->getFromUser($user);

if (!$twoFactorUser instanceof TwoFactorUser) {
$io->warning('User “' . $name . '” does not have two-factor authentication enabled.');
return 1;
}

$this->twoFactorUserProvider->disable($twoFactorUser);
$io->success('Two-factor authentication disabled for user “' . $name . '”.');

return 0;
}

}
43 changes: 43 additions & 0 deletions lib/RoadizTwoFactorBundle/src/Console/UsersCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace RZ\Roadiz\TwoFactorBundle\Console;

use Doctrine\Persistence\ManagerRegistry;
use RZ\Roadiz\CoreBundle\Entity\User;
use RZ\Roadiz\TwoFactorBundle\Security\Provider\TwoFactorUserProviderInterface;
use Symfony\Component\Console\Attribute\AsCommand;

#[AsCommand(
name: 'users:list',
description: 'List all users or just one'
)]
final class UsersCommand extends \RZ\Roadiz\CoreBundle\Console\UsersCommand
{
private TwoFactorUserProviderInterface $twoFactorUserProvider;

public function __construct(
TwoFactorUserProviderInterface $twoFactorUserProvider,
ManagerRegistry $managerRegistry,
string $name = null
) {
parent::__construct($managerRegistry, $name);
$this->twoFactorUserProvider = $twoFactorUserProvider;
}

protected function getUserTableRow(User $user): array
{
$twoFactorUser = $this->twoFactorUserProvider->getFromUser($user);
return [
'Id' => $user->getId(),
'Username' => $user->getUsername(),
'Email' => $user->getEmail(),
'Disabled' => (!$user->isEnabled() ? 'X' : ''),
'Expired' => ($user->getExpired() ? 'X' : ''),
'Locked' => (!$user->isAccountNonLocked() ? 'X' : ''),
'Groups' => implode(' ', $user->getGroupNames()),
'2FA enabled' => null !== $twoFactorUser && $twoFactorUser->isTotpAuthenticationEnabled() ? 'X' : '',
];
}
}

0 comments on commit 81cd472

Please sign in to comment.