From 41fba1420054beaf1a353b655a5f40f9a29e65ac Mon Sep 17 00:00:00 2001 From: Stefan Giehl Date: Tue, 22 Oct 2024 09:56:26 +0200 Subject: [PATCH] Fix some regressions in mobile messaging (#22701) * Fix setting of delegated management not detected correctly * Make error message translatable * Add a error message when a phone number already exists --- plugins/MobileMessaging/API.php | 8 ++++++-- plugins/MobileMessaging/Model.php | 2 +- plugins/MobileMessaging/lang/en.json | 2 ++ 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/plugins/MobileMessaging/API.php b/plugins/MobileMessaging/API.php index 2112075ffdf..a794355f51c 100644 --- a/plugins/MobileMessaging/API.php +++ b/plugins/MobileMessaging/API.php @@ -97,11 +97,15 @@ public function addPhoneNumber(string $phoneNumber): void // Check format matches the international public telecommunication numbering plan (E.164) // See https://en.wikipedia.org/wiki/E.164 if (!preg_match('/^\+[0-9]{5,30}$/', $phoneNumber)) { - throw new \Exception("The phone number $phoneNumber does not match the expected number format."); + throw new \Exception(Piwik::translate('MobileMessaging_IncorrectNumberFormat', $phoneNumber)); } $phoneNumbers = $this->model->getPhoneNumbers(Piwik::getCurrentUserLogin(), false); + if (!empty($phoneNumbers[$phoneNumber])) { + throw new \Exception(Piwik::translate('MobileMessaging_NumberAlreadyAdded', $phoneNumber)); + } + $unverifiedPhoneNumbers = array_filter( $phoneNumbers, function ($phoneNumber) { @@ -176,7 +180,7 @@ private function sanitizePhoneNumber($phoneNumber) // Avoid that any method tries to handle phone numbers that are obviously too long if (strlen($phoneNumber) > 100) { - throw new \Exception("The phone number $phoneNumber does not match the expected number format."); + throw new \Exception(Piwik::translate('MobileMessaging_IncorrectNumberFormat', $phoneNumber)); } return $phoneNumber; diff --git a/plugins/MobileMessaging/Model.php b/plugins/MobileMessaging/Model.php index bbd634d8ff9..f236c92c84b 100644 --- a/plugins/MobileMessaging/Model.php +++ b/plugins/MobileMessaging/Model.php @@ -233,7 +233,7 @@ public function getCredentialManagerSettings(): array public function getDelegatedManagement(): bool { $option = Option::get(MobileMessaging::DELEGATED_MANAGEMENT_OPTION); - return true === $option || 'true' === $option || 1 === $option; + return true === $option || 'true' === $option || 1 === $option || '1' === $option; } public function setDelegatedManagement(bool $delegatedManagement): void diff --git a/plugins/MobileMessaging/lang/en.json b/plugins/MobileMessaging/lang/en.json index 02402ddb43f..de13d0cbefa 100644 --- a/plugins/MobileMessaging/lang/en.json +++ b/plugins/MobileMessaging/lang/en.json @@ -40,7 +40,9 @@ "Settings_ResendVerification": "Resend verification code", "Settings_NewVerificationCodeSent": "A new verification code has been sent.", "VerificationCodeRecentlySentError": "A verification code for phone number %1$s has already been requested recently.", + "NumberAlreadyAdded": "The phone number %1$s has already been added.", "TooManyUnverifiedNumbersError": "You cannot add more than 3 unverified phone numbers!", + "IncorrectNumberFormat": "The phone number %1$s does not match the expected number format.", "SettingsMenu": "Mobile Messaging", "ConfirmRemovePhoneNumber": "Are you sure you want to remove the phone number %1$s?", "SMS_Content_Too_Long": "[too long]",