Skip to content

Commit

Permalink
Merge pull request #19243 from demeritcowboy/remove-old-xoauth2
Browse files Browse the repository at this point in the history
dev/core#2264 - Remove never-used IMAP_XOAUTH2 option value before it gets more confusing
  • Loading branch information
seamuslee001 authored Jan 8, 2021
2 parents a788e59 + 45d9783 commit 82d42f6
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 15 deletions.
6 changes: 1 addition & 5 deletions CRM/Mailing/MailStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,8 @@ public static function getStore($name = NULL) {
private static function getProtocolDefaults($protocol) {
switch ($protocol) {
case 'IMAP':
case 'IMAP_XOAUTH2':
return [
// For backward compat with pre-release XOAuth2 configurations
'auth' => $protocol === 'IMAP_XOAUTH2' ? 'XOAuth2' : 'Password',
// In a simpler world:
// 'auth' => 'Password',
'auth' => 'Password',
'factory' => function($mailSettings) {
$useXOAuth2 = ($mailSettings['auth'] === 'XOAuth2');
return new CRM_Mailing_MailStore_Imap($mailSettings['server'], $mailSettings['username'], $mailSettings['password'], (bool) $mailSettings['is_ssl'], $mailSettings['source'], $useXOAuth2);
Expand Down
76 changes: 68 additions & 8 deletions CRM/Upgrade/Incremental/php/FiveThirtyFour.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,14 @@ class CRM_Upgrade_Incremental_php_FiveThirtyFour extends CRM_Upgrade_Incremental
* @param null $currentVer
*/
public function setPreUpgradeMessage(&$preUpgradeMessage, $rev, $currentVer = NULL) {
// Example: Generate a pre-upgrade message.
// if ($rev == '5.12.34') {
// $preUpgradeMessage .= '<p>' . ts('A new permission, "%1", has been added. This permission is now used to control access to the Manage Tags screen.', array(1 => ts('manage tags'))) . '</p>';
// }
if ($rev === '5.34.alpha1') {
$xoauth2Value = CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_MailSettings', 'protocol', 'IMAP_XOAUTH2');
if (!empty($xoauth2Value)) {
if ($this->isXOAUTH2InUse($xoauth2Value)) {
$preUpgradeMessage .= '<p>' . $this->getXOAuth2Warning() . '</p>';
}
}
}
}

/**
Expand All @@ -40,10 +44,14 @@ public function setPreUpgradeMessage(&$preUpgradeMessage, $rev, $currentVer = NU
* an intermediate version; note that setPostUpgradeMessage is called repeatedly with different $revs.
*/
public function setPostUpgradeMessage(&$postUpgradeMessage, $rev) {
// Example: Generate a post-upgrade message.
// if ($rev == '5.12.34') {
// $postUpgradeMessage .= '<br /><br />' . ts("By default, CiviCRM now disables the ability to import directly from SQL. To use this feature, you must explicitly grant permission 'import SQL datasource'.");
// }
if ($rev === '5.34.alpha1') {
$xoauth2Value = CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_MailSettings', 'protocol', 'IMAP_XOAUTH2');
if (!empty($xoauth2Value)) {
if ($this->isXOAUTH2InUse($xoauth2Value)) {
$postUpgradeMessage .= '<div class="crm-error"><ul><li>' . $this->getXOAuth2Warning() . '</li></ul></div>';
}
}
}
}

/*
Expand Down Expand Up @@ -73,6 +81,8 @@ public function upgrade_5_34_alpha1(string $rev): void {

$this->addTask('Set defaults and required on financial type boolean fields', 'updateFinancialTypeTable');
$this->addTask('Set defaults and required on pledge fields', 'updatePledgeTable');

$this->addTask('Remove never used IMAP_XOAUTH2 option value', 'removeUnusedXOAUTH2');
}

/**
Expand Down Expand Up @@ -130,4 +140,54 @@ public static function updatePledgeTable(CRM_Queue_TaskContext $ctx): bool {
return TRUE;
}

/**
* This option value was never used, but check anyway if someone happens
* to be using it and then ask them to report what they're doing with it.
* There's no way to send a message to the user during the task, so we have
* to check it here and also as a pre/post upgrade message.
* Similar to removeGooglePlusOption from 5.23 except there we know some
* people would have used it.
*/
public static function removeUnusedXOAUTH2(CRM_Queue_TaskContext $ctx) {
$xoauth2Value = CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_MailSettings', 'protocol', 'IMAP_XOAUTH2');
if (!empty($xoauth2Value)) {
if (!self::isXOAUTH2InUse($xoauth2Value)) {
CRM_Core_DAO::executeQuery("DELETE ov FROM civicrm_option_value ov
INNER JOIN civicrm_option_group og
ON (og.name = 'mail_protocol' AND ov.option_group_id = og.id)
WHERE ov.value = %1",
[1 => [$xoauth2Value, 'Positive']]);
}
}
return TRUE;
}

/**
* Determine if option value is enabled or used in mail settings.
* @return bool
*/
private static function isXOAUTH2InUse($xoauth2Value) {
$enabled = (bool) CRM_Core_DAO::SingleValueQuery("SELECT ov.is_active FROM civicrm_option_value ov
INNER JOIN civicrm_option_group og
ON (og.name = 'mail_protocol' AND ov.option_group_id = og.id)
WHERE ov.value = %1",
[1 => [$xoauth2Value, 'Positive']]);
$usedInMailSettings = (bool) CRM_Core_DAO::SingleValueQuery("SELECT id FROM civicrm_mail_settings WHERE protocol = %1", [1 => [$xoauth2Value, 'Positive']]);
return $enabled || $usedInMailSettings;
}

/**
* @return string
*/
private function getXOAuth2Warning():string {
// Leaving out ts() since it's unlikely this message will ever
// be displayed to anyone.
return strtr(
'This system has enabled "IMAP_XOAUTH2" which was experimentally declared in CiviCRM v5.24. CiviCRM v5.33+ includes a supported replacement ("oauth-client"), and the experimental "IMAP_XOAUTH2" should be removed. Please visit %1 to discuss.',
[
'%1' => '<a target="_blank" href="https://lab.civicrm.org/dev/core/-/issues/2264">dev/core#2264</a>',
]
);
}

}
2 changes: 1 addition & 1 deletion sql/civicrm_generated.mysql

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion xml/templates/civicrm_data.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,6 @@ VALUES
(@option_group_id_mp, 'Maildir', 2, 'Maildir', NULL, 0, NULL, 2, NULL, 0, 0, 1, NULL, NULL , NULL),
(@option_group_id_mp, 'POP3', 3, 'POP3', NULL, 0, NULL, 3, NULL, 0, 0, 1, NULL, NULL , NULL),
(@option_group_id_mp, 'Localdir', 4, 'Localdir', NULL, 0, NULL, 4, NULL, 0, 0, 1, NULL, NULL , NULL),
(@option_group_id_mp, 'IMAP XOAUTH2', 5, 'IMAP_XOAUTH2', NULL, 0, NULL, 5, NULL, 0, 0, 0, NULL, NULL , NULL),

-- priority
(@option_group_id_priority, '{ts escape="sql"}Urgent{/ts}', 1, 'Urgent', NULL, 0, NULL, 1, NULL, 0, 0, 1, NULL, NULL, NULL),
Expand Down

0 comments on commit 82d42f6

Please sign in to comment.