From fbc9369b2ce7756a7dfbaf2474e2f1ecb6c7b54b Mon Sep 17 00:00:00 2001 From: gavynj Date: Thu, 17 Oct 2024 13:11:51 +1300 Subject: [PATCH 1/4] fix: incorrect created date shown on backup method --- src/FormField/RegisteredMFAMethodListField.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/FormField/RegisteredMFAMethodListField.php b/src/FormField/RegisteredMFAMethodListField.php index 65dc4d3..a425b4b 100644 --- a/src/FormField/RegisteredMFAMethodListField.php +++ b/src/FormField/RegisteredMFAMethodListField.php @@ -58,8 +58,8 @@ public function getSchemaDataDefaults() ], // We need all available methods so we can re-register pre-existing methods 'allAvailableMethods' => $generator->getAvailableMethods(), - 'backupCreationDate' => $this->getBackupMethod() - ? $this->getBackupMethod()->Created + 'backupCreatedDate' => $this->getBackupMethod($member) + ? $this->getBackupMethod($member)->Created : null, 'resetEndpoint' => SecurityAdmin::singleton()->Link("users/reset/{$this->value}"), 'isMFARequired' => EnforcementManager::create()->isMFARequired(), @@ -72,9 +72,9 @@ public function getSchemaDataDefaults() * * @return RegisteredMethod|null */ - protected function getBackupMethod(): ?RegisteredMethod + protected function getBackupMethod($member = null): ?RegisteredMethod { $backupMethod = MethodRegistry::singleton()->getBackupMethod(); - return RegisteredMethodManager::singleton()->getFromMember(Security::getCurrentUser(), $backupMethod); + return RegisteredMethodManager::singleton()->getFromMember($member ?? Security::getCurrentUser(), $backupMethod); } } From be3d84caf90a895e652c4934693c697338c82568 Mon Sep 17 00:00:00 2001 From: gavynj Date: Sat, 19 Oct 2024 12:27:38 +1300 Subject: [PATCH 2/4] chore: linting + docblock updated --- src/FormField/RegisteredMFAMethodListField.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/FormField/RegisteredMFAMethodListField.php b/src/FormField/RegisteredMFAMethodListField.php index a425b4b..33f6f6b 100644 --- a/src/FormField/RegisteredMFAMethodListField.php +++ b/src/FormField/RegisteredMFAMethodListField.php @@ -70,11 +70,15 @@ public function getSchemaDataDefaults() /** * Get the registered backup method (if any) from the currently logged in user. * + * @param Member|null $member * @return RegisteredMethod|null */ - protected function getBackupMethod($member = null): ?RegisteredMethod + protected function getBackupMethod(Member $member = null): ?RegisteredMethod { $backupMethod = MethodRegistry::singleton()->getBackupMethod(); - return RegisteredMethodManager::singleton()->getFromMember($member ?? Security::getCurrentUser(), $backupMethod); + return RegisteredMethodManager::singleton()->getFromMember( + $member ?? Security::getCurrentUser(), + $backupMethod + ); } } From 2ab7b64a3a2fa488a98003c92cb456a2275b1437 Mon Sep 17 00:00:00 2001 From: gavynj Date: Sat, 19 Oct 2024 12:46:24 +1300 Subject: [PATCH 3/4] fix: require Member object in getBackupMethod --- src/FormField/RegisteredMFAMethodListField.php | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/FormField/RegisteredMFAMethodListField.php b/src/FormField/RegisteredMFAMethodListField.php index 33f6f6b..84bb831 100644 --- a/src/FormField/RegisteredMFAMethodListField.php +++ b/src/FormField/RegisteredMFAMethodListField.php @@ -70,15 +70,12 @@ public function getSchemaDataDefaults() /** * Get the registered backup method (if any) from the currently logged in user. * - * @param Member|null $member + * @param Member $member * @return RegisteredMethod|null */ - protected function getBackupMethod(Member $member = null): ?RegisteredMethod + protected function getBackupMethod(Member $member): ?RegisteredMethod { $backupMethod = MethodRegistry::singleton()->getBackupMethod(); - return RegisteredMethodManager::singleton()->getFromMember( - $member ?? Security::getCurrentUser(), - $backupMethod - ); + return RegisteredMethodManager::singleton()->getFromMember($member, $backupMethod); } } From 5ffc71020722131d939bdc5541a7319efe44f640 Mon Sep 17 00:00:00 2001 From: gavynj Date: Fri, 25 Oct 2024 13:58:06 +1300 Subject: [PATCH 4/4] fix: use private $member property --- .../RegisteredMFAMethodListField.php | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/FormField/RegisteredMFAMethodListField.php b/src/FormField/RegisteredMFAMethodListField.php index 84bb831..542e529 100644 --- a/src/FormField/RegisteredMFAMethodListField.php +++ b/src/FormField/RegisteredMFAMethodListField.php @@ -16,6 +16,11 @@ class RegisteredMFAMethodListField extends FormField { + /** + * @var Member + */ + private $member; + /** * {@inheritDoc} * @@ -44,13 +49,13 @@ public function getSchemaDataDefaults() $generator = SchemaGenerator::create(); if (!$this->value && $this->getForm() && $this->getForm()->getRecord() instanceof Member) { - $member = $this->getForm()->getRecord(); + $this->member = $this->getForm()->getRecord(); } else { - $member = DataObject::get_by_id(Member::class, $this->value); + $this->member = DataObject::get_by_id(Member::class, $this->value); } return array_merge($defaults, [ - 'schema' => $generator->getSchema($member) + [ + 'schema' => $generator->getSchema($this->member) + [ 'endpoints' => [ 'register' => $adminController->Link('register/{urlSegment}'), 'remove' => $adminController->Link('method/{urlSegment}'), @@ -58,8 +63,8 @@ public function getSchemaDataDefaults() ], // We need all available methods so we can re-register pre-existing methods 'allAvailableMethods' => $generator->getAvailableMethods(), - 'backupCreatedDate' => $this->getBackupMethod($member) - ? $this->getBackupMethod($member)->Created + 'backupCreatedDate' => $this->getBackupMethod() + ? $this->getBackupMethod()->Created : null, 'resetEndpoint' => SecurityAdmin::singleton()->Link("users/reset/{$this->value}"), 'isMFARequired' => EnforcementManager::create()->isMFARequired(), @@ -70,12 +75,14 @@ public function getSchemaDataDefaults() /** * Get the registered backup method (if any) from the currently logged in user. * - * @param Member $member * @return RegisteredMethod|null */ - protected function getBackupMethod(Member $member): ?RegisteredMethod + protected function getBackupMethod(): ?RegisteredMethod { $backupMethod = MethodRegistry::singleton()->getBackupMethod(); - return RegisteredMethodManager::singleton()->getFromMember($member, $backupMethod); + return RegisteredMethodManager::singleton()->getFromMember( + $this->member ?? Security::getCurrentUser(), + $backupMethod + ); } }