From 1c5f00b3db86fb6bbdf0b60eb175802219f8509e Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Tue, 16 Feb 2021 18:50:24 -0800 Subject: [PATCH] GuzzleMiddleware::url() - Distinguish frontend and backend routes Before: The `route://` scheme assumed backend access. After: The `frontend://` and `backend://` schemes are unambiguous. The `route://` scheme will make a best-guess by determining if the menu item has `is_public`. --- CRM/Utils/GuzzleMiddleware.php | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/CRM/Utils/GuzzleMiddleware.php b/CRM/Utils/GuzzleMiddleware.php index 500d29cbc6..b6e8fb7e2d 100644 --- a/CRM/Utils/GuzzleMiddleware.php +++ b/CRM/Utils/GuzzleMiddleware.php @@ -13,14 +13,18 @@ class CRM_Utils_GuzzleMiddleware { * the construction of Civi-related URLs. It enables URL schemes for: * * - route://ROUTE_NAME (aka) route:ROUTE_NAME + * - backend://ROUTE_NAME (aka) backend:ROUTE_NAME + * - frontend://ROUTE_NAME (aka) frontend:ROUTE_NAME * - var://PATH_EXPRESSION (aka) var:PATH_EXPRESSION * - ext://EXTENSION/FILE (aka) ext:EXTENSION/FILE * - assetBuilder://ASSET_NAME?PARAMS (aka) assetBuilder:ASSET_NAME?PARAMS * * Compare: * - * $http->get(CRM_Utils_System::url('civicrm/dashboard', NULL, TRUE, NULL, FALSE)) - * $http->get('route:civicrm/dashboard') + * $http->get(CRM_Utils_System::url('civicrm/dashboard', NULL, TRUE, NULL, FALSE, ??)) + * $http->get('route://civicrm/dashboard') + * $http->get('frontend://civicrm/dashboard') + * $http->get('backend://civicrm/dashboard') * * $http->get(Civi::paths()->getUrl('[civicrm.files]/foo.txt')) * $http->get('var:[civicrm.files]/foo.txt') @@ -81,6 +85,11 @@ class CRM_Utils_GuzzleMiddleware { $scheme = ($hostPath[0] === '[') ? 'var' : 'route'; } + if ($scheme === 'route') { + $menu = CRM_Core_Menu::get($hostPath); + $scheme = ($menu && !empty($menu['is_public'])) ? 'frontend' : 'backend'; + } + switch ($scheme) { case 'assetBuilder': // Ex: 'assetBuilder:dynamic.css' or 'assetBuilder://dynamic.css?foo=bar' @@ -98,10 +107,14 @@ class CRM_Utils_GuzzleMiddleware { // Ex: 'var:[civicrm.files]/foo.txt' or 'var://[civicrm.files]/foo.txt' return $copyParams(\Civi::paths()->getUrl($hostPath, 'absolute')); - case 'route': - // Ex: 'route:civicrm/my-page' or 'route://civicrm/my-page' + case 'backend': + // Ex: 'backend:civicrm/my-page' or 'backend://civicrm/my-page' return $copyParams(\CRM_Utils_System::url($hostPath, NULL, TRUE, NULL, FALSE)); + case 'frontend': + // Ex: 'frontend:civicrm/my-page' or 'frontend://civicrm/my-page' + return $copyParams(\CRM_Utils_System::url($hostPath, NULL, TRUE, NULL, FALSE, TRUE)); + default: return NULL; } -- 2.25.1