parsePrefix() - Don't confuse financialacls dynamic-permissions with permission-prefixes
authorTim Otten <totten@civicrm.org>
Mon, 13 Jun 2022 23:45:35 +0000 (16:45 -0700)
committerTim Otten <totten@civicrm.org>
Wed, 22 Feb 2023 00:02:38 +0000 (16:02 -0800)
CRM/Utils/String.php
tests/phpunit/CRM/Core/Permission/BaseTest.php
tests/phpunit/CRM/Utils/StringTest.php

index 69f74151a05f7cc96dc65dc35839c2e3a7ab7bee..7cb44d0fc18ce9121d7e0bdf8e4e8b28e4f3bbb9 100644 (file)
@@ -692,18 +692,22 @@ class CRM_Utils_String {
    * @param string $string
    *   E.g. "view all contacts". Syntax: "[prefix:]name".
    * @param string|null $defaultPrefix
+   * @param string $validPrefixPattern
+   *   A regular expression used to determine if a prefix is valid.
+   *   To wit: Prefixes MUST be strictly alphanumeric.
    *
    * @return array
    *   (0 => string|NULL $prefix, 1 => string $value)
    */
-  public static function parsePrefix($delim, $string, $defaultPrefix = NULL) {
+  public static function parsePrefix($delim, $string, $defaultPrefix = NULL, $validPrefixPattern = '/^[A-Za-z0-9]+$/') {
     $pos = strpos($string, $delim);
     if ($pos === FALSE) {
       return [$defaultPrefix, $string];
     }
-    else {
-      return [substr($string, 0, $pos), substr($string, 1 + $pos)];
-    }
+
+    $lhs = substr($string, 0, $pos);
+    $rhs = substr($string, 1 + $pos);
+    return preg_match($validPrefixPattern, $lhs) ? [$lhs, $rhs] : [$defaultPrefix, $string];
   }
 
   /**
index 63ddc5fee4533420ec45211747f8abaf4a8309c2..2b1e701fab553c5ad1aec34dd40933d7290c12c2 100644 (file)
@@ -16,6 +16,7 @@ class CRM_Core_Permission_BaseTest extends CiviUnitTestCase {
     $cases = [];
 
     $cases[] = ['administer CiviCRM', 'administer CiviCRM'];
+    $cases[] = ['create contributions of type Event Fee: Canada', 'create contributions of type Event Fee: Canada'];
     $cases[] = ['cms:universal name', 'local name'];
     $cases[] = ['cms:universal name2', 'local name2'];
     $cases[] = ['cms:unknown universal name', CRM_Core_Permission::ALWAYS_DENY_PERMISSION];
index 43850aaa48be888b38ce176fb9507c85e0f577df..a593e25ee126f453a0ddcc8192712d9ccafd4d91 100644 (file)
@@ -132,6 +132,7 @@ class CRM_Utils_StringTest extends CiviUnitTestCase {
   public function parsePrefixData(): array {
     $cases = [];
     $cases[] = ['administer CiviCRM', NULL, [NULL, 'administer CiviCRM']];
+    $cases[] = ['create contributions of type Event Fee: Canada', NULL, [NULL, 'create contributions of type Event Fee: Canada']];
     $cases[] = ['administer CiviCRM', 'com_civicrm', ['com_civicrm', 'administer CiviCRM']];
     $cases[] = ['Drupal:access user profiles', NULL, ['Drupal', 'access user profiles']];
     $cases[] = ['Joomla:component:perm', NULL, ['Joomla', 'component:perm']];