Skip to content

Commit

Permalink
Merge pull request #29351 from ufundo/no-cms-sync-on-standalone
Browse files Browse the repository at this point in the history
dev/core#4993 block Sync CMS Users form and functionality on Standalone
  • Loading branch information
artfulrobot authored Mar 6, 2024
2 parents 0d6ea26 + 7d8c738 commit ef3fc7f
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 31 deletions.
11 changes: 10 additions & 1 deletion CRM/Admin/Form/CMSUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ class CRM_Admin_Form_CMSUser extends CRM_Core_Form {
*/
public $submitOnce = TRUE;

/**
* Disable on Standalone
*/
public function preProcess() {
if (!\CRM_Utils_System::allowSynchronizeUsers()) {
\CRM_Core_Error::statusBounce(ts('This framework doesn\'t allow for syncing CMS users.'));
}
}

/**
* Build the form object.
*/
Expand All @@ -47,7 +56,7 @@ public function buildQuickForm() {
* Process the form submission.
*/
public function postProcess() {
$result = CRM_Utils_System::synchronizeUsers();
$result = CRM_Utils_System::synchronizeUsersIfAllowed();

$status = ts('Checked one user record.',
[
Expand Down
11 changes: 11 additions & 0 deletions CRM/Extension/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,17 @@ public function getStatus($key) {
}
}

/**
* Check if a given extension is installed and enabled
*
* @param $key
*
* @return bool
*/
public function isEnabled($key) {
return ($this->getStatus($key) === self::STATUS_INSTALLED);
}

/**
* Check if a given extension is incompatible with this version of CiviCRM
*
Expand Down
21 changes: 21 additions & 0 deletions CRM/Utils/System/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,27 @@ public function synchronizeUsers() {
return [];
}

/**
* Whether to allow access to CMS user sync action
* @return bool
*/
public function allowSynchronizeUsers() {
return TRUE;
}

/**
* Run CMS user sync if allowed, otherwise just returns empty array
* @return array
*/
public function synchronizeUsersIfAllowed() {
if ($this->allowSynchronizeUsers()) {
return $this->synchronizeUsers();
}
else {
return [];
}
}

/**
* Send an HTTP Response base on PSR HTTP RespnseInterface response.
*
Expand Down
8 changes: 5 additions & 3 deletions CRM/Utils/System/Standalone.php
Original file line number Diff line number Diff line change
Expand Up @@ -420,10 +420,12 @@ public function getUserIDFromUserObject($user) {
}

/**
* @inheritDoc
* CMS User Sync doesn't make sense when using standaloneusers
* (but leave open the door for other user extensions, which might have a sync method)
* @return bool
*/
public function synchronizeUsers() {
return Security::singleton()->synchronizeUsers();
public function allowSynchronizeUsers() {
return !\CRM_Extension_System::singleton()->getManager()->isEnabled('standaloneusers');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion ext/authx/tests/phpunit/Civi/Authx/AbstractFlowsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static function setUpBeforeClass(): void {
->installMe(__DIR__)
->callback(
function() {
\CRM_Utils_System::synchronizeUsers();
\CRM_Utils_System::synchronizeUsersIfAllowed();
},
'synchronizeUsers'
)
Expand Down
27 changes: 19 additions & 8 deletions ext/standaloneusers/CRM/Standaloneusers/Upgrader.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
use CRM_Standaloneusers_ExtensionUtil as E;
use Civi\Api4\MessageTemplate;
use Civi\Api4\Navigation;

/**
* Collection of upgrade steps.
Expand Down Expand Up @@ -99,18 +100,28 @@ protected function createPasswordResetMessageTemplate() {
// }

/**
* Example: Run a simple query when a module is enabled.
* On enable:
* - disable the user sync menu item
*/
// public function enable() {
// CRM_Core_DAO::executeQuery('UPDATE foo SET is_active = 1 WHERE bar = "whiz"');
// }
public function enable() {
// standaloneusers is incompatible with user sync, so disable this nav menu item
Navigation::update(FALSE)
->addWhere('url', '=', 'civicrm/admin/synchUser?reset=1')
->addValue('is_active', FALSE)
->execute();
}

/**
* Example: Run a simple query when a module is disabled.
* On disable:
* - re-enable the user sync menu item
*/
// public function disable() {
// CRM_Core_DAO::executeQuery('UPDATE foo SET is_active = 0 WHERE bar = "whiz"');
// }
public function disable() {
// reinstate user sync menu item
Navigation::update(FALSE)
->addWhere('url', '=', 'civicrm/admin/synchUser?reset=1')
->addValue('is_active', TRUE)
->execute();
}

/**
* Example: Run a couple simple queries.
Expand Down
15 changes: 0 additions & 15 deletions ext/standaloneusers/Civi/Standalone/Security.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,21 +279,6 @@ public function getCMSPermissionsUrlParams() {
return ['ufAccessURL' => '/civicrm/admin/roles'];
}

/**
* Since our User entity contains a FK to a contact, it's not possible for a User to exist without a contact.
*
* @todo review this (what if contact is deleted?)
*/
public function synchronizeUsers() {

$userCount = \Civi\Api4\User::get(FALSE)->selectRowCount()->execute()->countMatched();
return [
'contactCount' => $userCount,
'contactMatching' => $userCount,
'contactCreated' => 0,
];
}

/**
* High level function to encrypt password using the site-default mechanism.
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/CiviTest/CiviEndToEndTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static function setUpBeforeClass(): void {
'name' => $GLOBALS['_CV']['ADMIN_USER'],
'pass' => $GLOBALS['_CV']['ADMIN_PASS'],
]);
CRM_Utils_System::synchronizeUsers();
CRM_Utils_System::synchronizeUsersIfAllowed();

parent::setUpBeforeClass();
}
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/E2E/Cache/CacheTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static function setUpBeforeClass(): void {
'name' => $GLOBALS['_CV']['ADMIN_USER'],
'pass' => $GLOBALS['_CV']['ADMIN_PASS'],
]);
CRM_Utils_System::synchronizeUsers();
\CRM_Utils_System::synchronizeUsersIfAllowed();

parent::setUpBeforeClass();
}
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/E2E/Extern/AuthxRestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static function setUpBeforeClass(): void {
->install(['authx'])
->callback(
function() {
\CRM_Utils_System::synchronizeUsers();
\CRM_Utils_System::synchronizeUsersIfAllowed();
},
'synchronizeUsers'
)
Expand Down

0 comments on commit ef3fc7f

Please sign in to comment.