From 345acaefa559158f8c5a17581804b6407412ccd3 Mon Sep 17 00:00:00 2001 From: Guy Sartorelli Date: Thu, 15 Aug 2024 17:04:18 +1200 Subject: [PATCH] API Standardise extension hooks --- src/Dev/Validation/DatabaseAdminExtension.php | 2 +- src/ORM/DataExtension.php | 16 ++++++++-------- src/ORM/DataObject.php | 9 ++++----- src/ORM/Hierarchy/Hierarchy.php | 4 ++-- src/Security/InheritedPermissionFlusher.php | 4 ++-- src/Security/Member.php | 10 +++++----- .../MemberAuthenticator/LostPasswordHandler.php | 4 ++-- .../MemberAuthenticator/MemberAuthenticator.php | 6 +++--- .../MemberAuthenticator/MemberLoginForm.php | 4 ++-- tests/php/ORM/DataExtensionTest/Extension1.php | 6 +++--- tests/php/ORM/DataExtensionTest/Extension2.php | 6 +++--- .../EditingAllowedDeletingDeniedExtension.php | 6 +++--- .../MemberTest/ViewingAllowedExtension.php | 2 +- .../MemberTest/ViewingDeniedExtension.php | 2 +- 14 files changed, 40 insertions(+), 41 deletions(-) diff --git a/src/Dev/Validation/DatabaseAdminExtension.php b/src/Dev/Validation/DatabaseAdminExtension.php index fbcf5ffc244..db0c83351d8 100644 --- a/src/Dev/Validation/DatabaseAdminExtension.php +++ b/src/Dev/Validation/DatabaseAdminExtension.php @@ -21,7 +21,7 @@ class DatabaseAdminExtension extends Extension * @param bool $testMode * @throws ReflectionException */ - public function onAfterBuild(bool $quiet, bool $populate, bool $testMode): void + protected function onAfterBuild(bool $quiet, bool $populate, bool $testMode): void { $service = RelationValidationService::singleton(); diff --git a/src/ORM/DataExtension.php b/src/ORM/DataExtension.php index 399182d3f17..53ffad3b71c 100644 --- a/src/ORM/DataExtension.php +++ b/src/ORM/DataExtension.php @@ -38,7 +38,7 @@ public function __construct() * @param ValidationResult $validationResult Local validation result * @throws ValidationException */ - public function validate(ValidationResult $validationResult) + protected function updateValidate(ValidationResult $validationResult) { } @@ -112,7 +112,7 @@ protected function onAfterDelete() * * See {@link DataObject::requireDefaultRecords()} for context. */ - public function requireDefaultRecords() + protected function onRequireDefaultRecords() { } @@ -121,7 +121,7 @@ public function requireDefaultRecords() * * See {@link DataObject::populateDefaults()} for context. */ - public function populateDefaults() + protected function onAfterPopulateDefaults() { } @@ -130,7 +130,7 @@ public function populateDefaults() * * See {@link DataObject::onAfterBuild()} for context. */ - public function onAfterBuild() + protected function onAfterBuild() { } @@ -144,7 +144,7 @@ public function onAfterBuild() * @param Member $member * @return bool|null */ - public function can($member) + protected function can($member) { } @@ -158,7 +158,7 @@ public function can($member) * @param Member $member * @return bool|null */ - public function canEdit($member) + protected function canEdit($member) { } @@ -172,7 +172,7 @@ public function canEdit($member) * @param Member $member * @return bool|null */ - public function canDelete($member) + protected function canDelete($member) { } @@ -186,7 +186,7 @@ public function canDelete($member) * @param Member $member * @return bool|null */ - public function canCreate($member) + protected function canCreate($member) { } diff --git a/src/ORM/DataObject.php b/src/ORM/DataObject.php index 43414fc301a..36bce9e59d6 100644 --- a/src/ORM/DataObject.php +++ b/src/ORM/DataObject.php @@ -1228,7 +1228,7 @@ public function forceChange() public function validate() { $result = ValidationResult::create(); - $this->extend('validate', $result); + $this->extend('updateValidate', $result); return $result; } @@ -1311,7 +1311,6 @@ protected function onAfterDelete() * Will traverse the defaults of the current class and all its parent classes. * Called by the constructor when creating new records. * - * @uses DataExtension::populateDefaults() * @return static $this */ public function populateDefaults() @@ -1348,7 +1347,7 @@ public function populateDefaults() } } - $this->extend('populateDefaults'); + $this->extend('onAfterPopulateDefaults'); return $this; } @@ -3542,7 +3541,7 @@ public function flushCache($persistent = true) } } - $this->extend('flushCache'); + $this->extend('onFlushCache'); $this->components = []; $this->eagerLoadedData = []; @@ -3800,7 +3799,7 @@ public function requireDefaultRecords() } // Let any extensions make their own database default data - $this->extend('requireDefaultRecords', $dummy); + $this->extend('onRequireDefaultRecords', $dummy); } /** diff --git a/src/ORM/Hierarchy/Hierarchy.php b/src/ORM/Hierarchy/Hierarchy.php index 40adbb8bf2e..ef4ab01dc11 100644 --- a/src/ORM/Hierarchy/Hierarchy.php +++ b/src/ORM/Hierarchy/Hierarchy.php @@ -116,7 +116,7 @@ public static function get_extra_config($class, $extension, $args) * * @param ValidationResult $validationResult */ - public function validate(ValidationResult $validationResult) + protected function updateValidate(ValidationResult $validationResult) { // The object is new, won't be looping. $owner = $this->owner; @@ -572,7 +572,7 @@ public function getBreadcrumbs($separator = ' » ') * - Children (instance) * - NumChildren (instance) */ - public function flushCache() + protected function onFlushCache() { $this->owner->_cache_children = null; Hierarchy::$cache_numChildren = []; diff --git a/src/Security/InheritedPermissionFlusher.php b/src/Security/InheritedPermissionFlusher.php index d4aa344e4a4..b815b8859ec 100644 --- a/src/Security/InheritedPermissionFlusher.php +++ b/src/Security/InheritedPermissionFlusher.php @@ -23,7 +23,7 @@ class InheritedPermissionFlusher extends DataExtension implements Flushable */ public static function flush() { - singleton(__CLASS__)->flushCache(); + singleton(__CLASS__)->onFlushCache(); } /** @@ -77,7 +77,7 @@ public function getServices() /** * Flushes all registered MemberCacheFlusher services */ - public function flushCache() + protected function onFlushCache() { $ids = $this->getMemberIDList(); foreach ($this->getServices() as $service) { diff --git a/src/Security/Member.php b/src/Security/Member.php index ad241beac8e..82b228d8ef4 100644 --- a/src/Security/Member.php +++ b/src/Security/Member.php @@ -452,7 +452,7 @@ public function isPasswordExpired(): bool */ public function beforeMemberLoggedIn() { - $this->extend('beforeMemberLoggedIn'); + $this->extend('onBeforeMemberLoggedIn'); } /** @@ -470,7 +470,7 @@ public function afterMemberLoggedIn() $this->write(); // Audit logging hook - $this->extend('afterMemberLoggedIn'); + $this->extend('onAfterMemberLoggedIn'); } /** @@ -497,7 +497,7 @@ public function regenerateTempID() */ public function beforeMemberLoggedOut(HTTPRequest $request = null) { - $this->extend('beforeMemberLoggedOut', $request); + $this->extend('onBeforeMemberLoggedOut', $request); } /** @@ -507,7 +507,7 @@ public function beforeMemberLoggedOut(HTTPRequest $request = null) */ public function afterMemberLoggedOut(HTTPRequest $request = null) { - $this->extend('afterMemberLoggedOut', $request); + $this->extend('onAfterMemberLoggedOut', $request); } /** @@ -1709,7 +1709,7 @@ public function registerFailedLogin() $this->FailedLoginCount = 0; } } - $this->extend('registerFailedLogin'); + $this->extend('onRegisterFailedLogin'); $this->write(); } diff --git a/src/Security/MemberAuthenticator/LostPasswordHandler.php b/src/Security/MemberAuthenticator/LostPasswordHandler.php index 41d3718b210..0e019552e0d 100644 --- a/src/Security/MemberAuthenticator/LostPasswordHandler.php +++ b/src/Security/MemberAuthenticator/LostPasswordHandler.php @@ -152,7 +152,7 @@ public function redirectToLostPassword() /** * Forgot password form handler method. * Called when the user clicks on "I've lost my password". - * Extensions can use the 'forgotPassword' method to veto executing + * Extensions can use the 'onForgotPassword' method to veto executing * the logic, by returning FALSE. In this case, the user will be redirected back * to the form without further action. It is recommended to set a message * in the form detailing why the action was denied. @@ -168,7 +168,7 @@ public function forgotPassword(array $data, Form $form): HTTPResponse $member = $this->getMemberFromData($data); // Allow vetoing forgot password requests - $results = $this->extend('forgotPassword', $member); + $results = $this->extend('onForgotPassword', $member); if ($results && is_array($results) && in_array(false, $results ?? [], true)) { return $this->redirectToLostPassword(); } diff --git a/src/Security/MemberAuthenticator/MemberAuthenticator.php b/src/Security/MemberAuthenticator/MemberAuthenticator.php index 29ed27a17e8..ec53cffebd5 100644 --- a/src/Security/MemberAuthenticator/MemberAuthenticator.php +++ b/src/Security/MemberAuthenticator/MemberAuthenticator.php @@ -195,18 +195,18 @@ protected function recordLoginAttempt($data, HTTPRequest $request, $member, $suc $attempt->Status = LoginAttempt::SUCCESS; // Audit logging hook - $member->extend('authenticationSucceeded'); + $member->extend('onAuthenticationSucceeded'); } else { // Failed login - we're trying to see if a user exists with this email (disregarding wrong passwords) $attempt->Status = LoginAttempt::FAILURE; if ($member) { // Audit logging hook $attempt->MemberID = $member->ID; - $member->extend('authenticationFailed', $data, $request); + $member->extend('onAuthenticationFailed', $data, $request); } else { // Audit logging hook Member::singleton() - ->extend('authenticationFailedUnknownUser', $data, $request); + ->extend('onAuthenticationFailedUnknownUser', $data, $request); } } diff --git a/src/Security/MemberAuthenticator/MemberLoginForm.php b/src/Security/MemberAuthenticator/MemberLoginForm.php index eba6afc74dd..8ce5c2b4b99 100644 --- a/src/Security/MemberAuthenticator/MemberLoginForm.php +++ b/src/Security/MemberAuthenticator/MemberLoginForm.php @@ -23,9 +23,9 @@ * Log-in form for the "member" authentication method. * * Available extension points: - * - "authenticationFailed": Called when login was not successful. + * - "onAuthenticationFailed": Called when login was not successful. * Arguments: $data containing the form submission - * - "forgotPassword": Called before forgot password logic kicks in, + * - "onForgotPassword": Called before forgot password logic kicks in, * allowing extensions to "veto" execution by returning FALSE. * Arguments: $member containing the detected Member record */ diff --git a/tests/php/ORM/DataExtensionTest/Extension1.php b/tests/php/ORM/DataExtensionTest/Extension1.php index 010e3a863e2..1b9fb62ae20 100644 --- a/tests/php/ORM/DataExtensionTest/Extension1.php +++ b/tests/php/ORM/DataExtensionTest/Extension1.php @@ -8,17 +8,17 @@ class Extension1 extends DataExtension implements TestOnly { - public function canOne($member = null) + protected function canOne($member = null) { return true; } - public function canTwo($member = null) + protected function canTwo($member = null) { return false; } - public function canThree($member = null) + protected function canThree($member = null) { } } diff --git a/tests/php/ORM/DataExtensionTest/Extension2.php b/tests/php/ORM/DataExtensionTest/Extension2.php index c37e37d13a0..10f964a0f0c 100644 --- a/tests/php/ORM/DataExtensionTest/Extension2.php +++ b/tests/php/ORM/DataExtensionTest/Extension2.php @@ -8,17 +8,17 @@ class Extension2 extends DataExtension implements TestOnly { - public function canOne($member = null) + protected function canOne($member = null) { return true; } - public function canTwo($member = null) + protected function canTwo($member = null) { return true; } - public function canThree($member = null) + protected function canThree($member = null) { } } diff --git a/tests/php/Security/MemberTest/EditingAllowedDeletingDeniedExtension.php b/tests/php/Security/MemberTest/EditingAllowedDeletingDeniedExtension.php index ac61a60ca15..6afd5185a04 100644 --- a/tests/php/Security/MemberTest/EditingAllowedDeletingDeniedExtension.php +++ b/tests/php/Security/MemberTest/EditingAllowedDeletingDeniedExtension.php @@ -8,17 +8,17 @@ class EditingAllowedDeletingDeniedExtension extends DataExtension implements TestOnly { - public function canView($member = null) + protected function canView($member = null) { return true; } - public function canEdit($member = null) + protected function canEdit($member = null) { return true; } - public function canDelete($member = null) + protected function canDelete($member = null) { return false; } diff --git a/tests/php/Security/MemberTest/ViewingAllowedExtension.php b/tests/php/Security/MemberTest/ViewingAllowedExtension.php index e941dd472e0..b71ccbfae92 100644 --- a/tests/php/Security/MemberTest/ViewingAllowedExtension.php +++ b/tests/php/Security/MemberTest/ViewingAllowedExtension.php @@ -8,7 +8,7 @@ class ViewingAllowedExtension extends DataExtension implements TestOnly { - public function canView($member = null) + protected function canView($member = null) { return true; } diff --git a/tests/php/Security/MemberTest/ViewingDeniedExtension.php b/tests/php/Security/MemberTest/ViewingDeniedExtension.php index 82089daaaf4..35ef55db174 100644 --- a/tests/php/Security/MemberTest/ViewingDeniedExtension.php +++ b/tests/php/Security/MemberTest/ViewingDeniedExtension.php @@ -8,7 +8,7 @@ class ViewingDeniedExtension extends DataExtension implements TestOnly { - public function canView($member = null) + protected function canView($member = null) { return false; }