Merge pull request #22378 from eileenmcnaughton/notice
[civicrm-core.git] / ext / authx / authx.php
index 95fd219557b8a725f4f1d7367051701f05f2b720..25fe959f4c37ddce5b238e5780010f2974a8a16a 100644 (file)
@@ -38,6 +38,44 @@ Civi::dispatcher()->addListener('civi.invoke.auth', function($e) {
   }
 });
 
+/**
+ * Perform a system login.
+ *
+ * This is useful for backend scripts that need to switch to a specific user.
+ *
+ * As needed, this will update the Civi session and CMS data.
+ *
+ * @param array{flow: ?string, useSession: ?bool, principal: ?array, cred: ?string,} $details
+ *   Describe the authentication process with these properties:
+ *
+ *   - string $flow (default 'script');
+ *     The type of authentication flow being used
+ *     Ex: 'param', 'header', 'auto'
+ *   - bool $useSession (default FALSE)
+ *     If TRUE, then the authentication should be persistent (in a session variable).
+ *     If FALSE, then the authentication should be ephemeral (single page-request).
+ *
+ *   And then ONE of these properties to describe the user/principal:
+ *
+ *   - string $cred
+ *     The credential, as formatted in the 'Authorization' header.
+ *     Ex: 'Bearer 12345', 'Basic ASDFFDSA=='
+ *   - array $principal
+ *     Description of a validated principal.
+ *     Must include 'contactId', 'userId', xor 'user'
+ * @return array{contactId: int, userId: ?int, flow: string, credType: string, useSession: bool}
+ *   An array describing the authenticated session.
+ * @throws \Civi\Authx\AuthxException
+ */
+function authx_login(array $details): array {
+  $defaults = ['flow' => 'script', 'useSession' => FALSE];
+  $details = array_merge($defaults, $details);
+  $auth = new \Civi\Authx\Authenticator();
+  $auth->setRejectMode('exception');
+  $auth->auth(NULL, array_merge($defaults, $details));
+  return \CRM_Core_Session::singleton()->get("authx");
+}
+
 /**
  * @return \Civi\Authx\AuthxInterface
  */
@@ -85,15 +123,6 @@ function authx_civicrm_config(&$config) {
   _authx_civix_civicrm_config($config);
 }
 
-/**
- * Implements hook_civicrm_xmlMenu().
- *
- * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_xmlMenu
- */
-function authx_civicrm_xmlMenu(&$files) {
-  _authx_civix_civicrm_xmlMenu($files);
-}
-
 /**
  * Implements hook_civicrm_install().
  *
@@ -148,54 +177,6 @@ function authx_civicrm_upgrade($op, CRM_Queue_Queue $queue = NULL) {
   return _authx_civix_civicrm_upgrade($op, $queue);
 }
 
-/**
- * Implements hook_civicrm_managed().
- *
- * Generate a list of entities to create/deactivate/delete when this module
- * is installed, disabled, uninstalled.
- *
- * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_managed
- */
-function authx_civicrm_managed(&$entities) {
-  _authx_civix_civicrm_managed($entities);
-}
-
-/**
- * Implements hook_civicrm_caseTypes().
- *
- * Generate a list of case-types.
- *
- * Note: This hook only runs in CiviCRM 4.4+.
- *
- * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_caseTypes
- */
-function authx_civicrm_caseTypes(&$caseTypes) {
-  _authx_civix_civicrm_caseTypes($caseTypes);
-}
-
-/**
- * Implements hook_civicrm_angularModules().
- *
- * Generate a list of Angular modules.
- *
- * Note: This hook only runs in CiviCRM 4.5+. It may
- * use features only available in v4.6+.
- *
- * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_angularModules
- */
-function authx_civicrm_angularModules(&$angularModules) {
-  _authx_civix_civicrm_angularModules($angularModules);
-}
-
-/**
- * Implements hook_civicrm_alterSettingsFolders().
- *
- * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_alterSettingsFolders
- */
-function authx_civicrm_alterSettingsFolders(&$metaDataFolders = NULL) {
-  _authx_civix_civicrm_alterSettingsFolders($metaDataFolders);
-}
-
 /**
  * Implements hook_civicrm_entityTypes().
  *
@@ -207,13 +188,6 @@ function authx_civicrm_entityTypes(&$entityTypes) {
   _authx_civix_civicrm_entityTypes($entityTypes);
 }
 
-/**
- * Implements hook_civicrm_thems().
- */
-function authx_civicrm_themes(&$themes) {
-  _authx_civix_civicrm_themes($themes);
-}
-
 /**
  * Implements hook_civicrm_permission().
  *