afform - Display status warning if email-tokens are enabled without authx
authorTim Otten <totten@civicrm.org>
Tue, 23 Feb 2021 14:43:46 +0000 (06:43 -0800)
committerTim Otten <totten@civicrm.org>
Wed, 3 Mar 2021 20:19:05 +0000 (12:19 -0800)
ext/afform/core/Civi/Afform/StatusChecks.php [new file with mode: 0644]
ext/afform/core/afform.php

diff --git a/ext/afform/core/Civi/Afform/StatusChecks.php b/ext/afform/core/Civi/Afform/StatusChecks.php
new file mode 100644 (file)
index 0000000..c1a7d7c
--- /dev/null
@@ -0,0 +1,67 @@
+<?php
+/*
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC. All rights reserved.                        |
+ |                                                                    |
+ | This work is published under the GNU AGPLv3 license with some      |
+ | permitted exceptions and without any warranty. For full license    |
+ | and copyright information, see https://civicrm.org/licensing       |
+ +--------------------------------------------------------------------+
+ */
+
+namespace Civi\Afform;
+
+use CRM_Afform_ExtensionUtil as E;
+
+class StatusChecks {
+
+  /**
+   * Afform has a soft dependency on Authx, which is used to generate authenticated email links.
+   *
+   * @param \Civi\Core\Event\GenericHookEvent $e
+   * @see CRM_Utils_Hook::check()
+   */
+  public static function hook_civicrm_check($e) {
+    $hasAuthx = \CRM_Extension_System::singleton()->getMapper()->isActiveModule('authx');
+    $tokenFormCount = count(Tokens::getTokenForms());
+    if (!$hasAuthx) {
+      if ($tokenFormCount) {
+        $e->messages[] = new \CRM_Utils_Check_Message(
+          'afform_token_authx',
+          E::ts('Email token support has been configured for %2 form(s), which requires extended authentication services. Please enable "AuthX" in <a href="%1">Manage Extensions</a>.', [
+            1 => \CRM_Utils_System::url('civicrm/admin/extensions', 'reset=1'),
+            2 => $tokenFormCount,
+          ]),
+          E::ts('AuthX Required'),
+          \Psr\Log\LogLevel::ERROR,
+          'fa-chain-broken'
+        );
+      }
+      else {
+        $e->messages[] = new \CRM_Utils_Check_Message(
+          'afform_token_authx',
+          E::ts('To generate authenticated email links for custom forms, enable extended authentication services (AuthX) in <a href="%1">Manage Extensions</a>.', [
+            1 => \CRM_Utils_System::url('civicrm/admin/extensions', 'reset=1'),
+          ]),
+          E::ts('AuthX Suggested'),
+          \Psr\Log\LogLevel::INFO,
+          'fa-lightbulb-o'
+        );
+      }
+    }
+
+    if ($hasAuthx && $tokenFormCount > 0 && !in_array('jwt', \Civi::settings()->get('authx_auto_cred'))) {
+      $e->messages[] = new \CRM_Utils_Check_Message(
+        'afform_token_authx',
+        E::ts('Email token support has been configured for %1 form(s). This requires JWT authentication, <code>authx_auto_cred</code> does not include JWT. ', [
+          1 => $tokenFormCount,
+        ]),
+        E::ts('AuthX Configuration'),
+        \Psr\Log\LogLevel::ERROR,
+        'fa-chain-broken'
+      );
+
+    }
+  }
+
+}
index 717249af6cf326025e79d15303ad26ea0502a0cb..be6cf09f0c83b1849e497a9b6fc19d4ee2b80346 100644 (file)
@@ -54,11 +54,14 @@ function afform_civicrm_config(&$config) {
   $dispatcher->addListener(Submit::EVENT_NAME, [Submit::class, 'processGenericEntity'], -1000);
   $dispatcher->addListener('hook_civicrm_angularModules', ['\Civi\Afform\AngularDependencyMapper', 'autoReq'], -1000);
   $dispatcher->addListener('hook_civicrm_alterAngular', ['\Civi\Afform\AfformMetadataInjector', 'preprocess']);
+  $dispatcher->addListener('hook_civicrm_check', ['\Civi\Afform\StatusChecks', 'hook_civicrm_check']);
 
   // Register support for email tokens
-  $dispatcher->addListener('hook_civicrm_alterMailContent', ['\Civi\Afform\Tokens', 'applyCkeditorWorkaround']);
-  $dispatcher->addListener('hook_civicrm_tokens', ['\Civi\Afform\Tokens', 'hook_civicrm_tokens']);
-  $dispatcher->addListener('hook_civicrm_tokenValues', ['\Civi\Afform\Tokens', 'hook_civicrm_tokenValues']);
+  if (CRM_Extension_System::singleton()->getMapper()->isActiveModule('authx')) {
+    $dispatcher->addListener('hook_civicrm_alterMailContent', ['\Civi\Afform\Tokens', 'applyCkeditorWorkaround']);
+    $dispatcher->addListener('hook_civicrm_tokens', ['\Civi\Afform\Tokens', 'hook_civicrm_tokens']);
+    $dispatcher->addListener('hook_civicrm_tokenValues', ['\Civi\Afform\Tokens', 'hook_civicrm_tokenValues']);
+  }
 }
 
 /**