-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(TwoFactorBundle): Added console command to disable 2FA and overr…
…ide users:list command
- Loading branch information
1 parent
48fb561
commit 81cd472
Showing
3 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
lib/RoadizTwoFactorBundle/src/Console/DisableTwoFactorUserCommand.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' : '', | ||
]; | ||
} | ||
} |