Skip to content

Commit

Permalink
Fix some regressions in mobile messaging (#22701)
Browse files Browse the repository at this point in the history
* Fix setting of delegated management not detected correctly

* Make error message translatable

* Add a error message when a phone number already exists
  • Loading branch information
sgiehl committed Oct 22, 2024
1 parent 02565b7 commit 41fba14
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
8 changes: 6 additions & 2 deletions plugins/MobileMessaging/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion plugins/MobileMessaging/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions plugins/MobileMessaging/lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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]",
Expand Down

0 comments on commit 41fba14

Please sign in to comment.