Skip to content

Commit 28ba31b

Browse files
committed
MC-22950: Enable 2FA by default for Admins
- Added patch to reset any existing u2f config
1 parent 19c76d2 commit 28ba31b

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\TwoFactorAuth\Setup\Patch\Data;
9+
10+
use Magento\Framework\Exception\NoSuchEntityException;
11+
use Magento\Framework\Setup\ModuleDataSetupInterface;
12+
use Magento\Framework\Setup\Patch\DataPatchInterface;
13+
use Magento\TwoFactorAuth\Api\UserConfigManagerInterface;
14+
use Magento\TwoFactorAuth\Model\Provider\Engine\U2fKey;
15+
use Magento\User\Model\ResourceModel\User\CollectionFactory;
16+
use Magento\User\Model\User;
17+
18+
/**
19+
* Reset the U2f data due to rewrite
20+
*/
21+
class ResetU2FConfig implements DataPatchInterface
22+
{
23+
/**
24+
* @var ModuleDataSetupInterface
25+
*/
26+
private $moduleDataSetup;
27+
28+
/**
29+
* @var CollectionFactory
30+
*/
31+
private $userCollectionFactory;
32+
33+
/**
34+
* @var UserConfigManagerInterface
35+
*/
36+
private $userConfigManager;
37+
38+
/**
39+
* @param ModuleDataSetupInterface $moduleDataSetup
40+
* @param CollectionFactory $userCollectionFactory
41+
* @param UserConfigManagerInterface $userConfigManager
42+
*/
43+
public function __construct(
44+
ModuleDataSetupInterface $moduleDataSetup,
45+
CollectionFactory $userCollectionFactory,
46+
UserConfigManagerInterface $userConfigManager
47+
) {
48+
$this->moduleDataSetup = $moduleDataSetup;
49+
$this->userCollectionFactory = $userCollectionFactory;
50+
$this->userConfigManager = $userConfigManager;
51+
}
52+
53+
/**
54+
* @inheritdoc
55+
*/
56+
public function apply()
57+
{
58+
$this->moduleDataSetup->startSetup();
59+
60+
/** @var \Magento\User\Model\ResourceModel\User\Collection $collection */
61+
$collection = $this->userCollectionFactory->create();
62+
63+
foreach ($collection as $user) {
64+
/** @var $user User */
65+
66+
try {
67+
$this->userConfigManager->setProviderConfig((int)$user->getId(), U2fKey::CODE, []);
68+
} catch (NoSuchEntityException $e) {
69+
continue;
70+
}
71+
}
72+
73+
$this->moduleDataSetup->endSetup();
74+
}
75+
76+
/**
77+
* @inheritdoc
78+
*/
79+
public static function getDependencies()
80+
{
81+
return [];
82+
}
83+
84+
/**
85+
* @inheritdoc
86+
*/
87+
public function getAliases()
88+
{
89+
return [];
90+
}
91+
}

0 commit comments

Comments
 (0)