15
15
use Magento \TwoFactorAuth \Api \UserConfigTokenManagerInterface ;
16
16
use Magento \TwoFactorAuth \Api \UserNotifierInterface ;
17
17
use Magento \Framework \Authorization \PolicyInterface as Authorization ;
18
+ use Magento \Framework \App \CacheInterface ;
19
+ use Magento \Framework \App \ObjectManager ;
18
20
19
21
/**
20
22
* @inheritDoc
@@ -41,22 +43,30 @@ class UserConfigRequestManager implements UserConfigRequestManagerInterface
41
43
*/
42
44
private $ auth ;
43
45
46
+ /**
47
+ * @var CacheInterface
48
+ */
49
+ private $ cache ;
50
+
44
51
/**
45
52
* @param TfaInterface $tfa
46
53
* @param UserNotifierInterface $notifier
47
54
* @param UserConfigTokenManagerInterface $tokenManager
48
55
* @param Authorization $auth
56
+ * @param CacheInterface|null $cache
49
57
*/
50
58
public function __construct (
51
59
TfaInterface $ tfa ,
52
60
UserNotifierInterface $ notifier ,
53
61
UserConfigTokenManagerInterface $ tokenManager ,
54
- Authorization $ auth
62
+ Authorization $ auth ,
63
+ CacheInterface $ cache = null
55
64
) {
56
65
$ this ->tfa = $ tfa ;
57
66
$ this ->notifier = $ notifier ;
58
67
$ this ->tokenManager = $ tokenManager ;
59
68
$ this ->auth = $ auth ;
69
+ $ this ->cache = $ cache ?? ObjectManager::getInstance ()->get (CacheInterface::class);
60
70
}
61
71
62
72
/**
@@ -75,11 +85,18 @@ public function sendConfigRequestTo(User $user): void
75
85
{
76
86
$ userId = (int )$ user ->getId ();
77
87
if (empty ($ this ->tfa ->getUserProviders ($ userId ))) {
88
+ $ tfaToken = $ this ->cache ->load (SignedTokenManager::CACHE_ID . $ userId );
89
+ $ isValidOldToken = false ;
90
+ if ($ tfaToken !== false ) {
91
+ $ isValidOldToken = $ this ->tokenManager ->isValidFor ($ userId , $ tfaToken );
92
+ }
78
93
//Application level configuration is required.
79
94
if (!$ this ->auth ->isAllowed ($ user ->getAclRole (), 'Magento_TwoFactorAuth::config ' )) {
80
95
throw new AuthorizationException (__ ('User is not authorized to edit 2FA configuration ' ));
81
96
}
82
- $ this ->notifier ->sendAppConfigRequestMessage ($ user , $ this ->tokenManager ->issueFor ($ userId ));
97
+ if (!$ isValidOldToken ) {
98
+ $ this ->notifier ->sendAppConfigRequestMessage ($ user , $ this ->tokenManager ->issueFor ($ userId ));
99
+ }
83
100
} else {
84
101
//Personal provider config required.
85
102
$ this ->notifier ->sendUserConfigRequestMessage ($ user , $ this ->tokenManager ->issueFor ($ userId ));
0 commit comments