Merge pull request #23210 from eileenmcnaughton/cancel
[civicrm-core.git] / CRM / Core / BAO / Dashboard.php
index 5ef7321b15f82dd5b105135c151e4bbfe01d21bd..4e19a8554668b0aedaf336626817aaee9475fa48 100644 (file)
@@ -134,16 +134,14 @@ class CRM_Core_BAO_Dashboard extends CRM_Core_DAO_Dashboard {
   /**
    * Check dashlet permission for current user.
    *
-   * @param array $permissions
-   * @param string $operator
+   * @param array|null $permissions
+   * @param string|null $operator
    *
    * @return bool
    *   true if user has permission to view dashlet
    */
-  public static function checkPermission($permissions, $operator) {
+  private static function checkPermission(?array $permissions, ?string $operator): bool {
     if ($permissions) {
-      $config = CRM_Core_Config::singleton();
-
       static $allComponents;
       if (!$allComponents) {
         $allComponents = CRM_Core_Component::getNames();
@@ -153,26 +151,11 @@ class CRM_Core_BAO_Dashboard extends CRM_Core_DAO_Dashboard {
       foreach ($permissions as $key) {
         $showDashlet = TRUE;
 
-        $componentName = NULL;
-        if (strpos($key, 'access') === 0) {
-          $componentName = trim(substr($key, 6));
-          if (!in_array($componentName, $allComponents)) {
-            $componentName = NULL;
-          }
-        }
-
-        // hack to handle case permissions
-        if (!$componentName
-          && in_array($key, ['access my cases and activities', 'access all cases and activities'])
-        ) {
-          $componentName = 'CiviCase';
-        }
+        $componentName = CRM_Core_Permission::getComponentName($key);
 
-        //hack to determine if it's a component related permission
+        // If the permission depends on a component, ensure it is enabled
         if ($componentName) {
-          if (!in_array($componentName, $config->enableComponents) ||
-            !CRM_Core_Permission::check($key)
-          ) {
+          if (!CRM_Core_Component::isEnabled($componentName) || !CRM_Core_Permission::check($key)) {
             $showDashlet = FALSE;
             if ($operator == 'AND') {
               return $showDashlet;
@@ -193,17 +176,10 @@ class CRM_Core_BAO_Dashboard extends CRM_Core_DAO_Dashboard {
         }
       }
 
-      if (!$showDashlet && !$hasPermission) {
-        return FALSE;
-      }
-      else {
-        return TRUE;
-      }
-    }
-    else {
-      // if permission is not set consider everyone has permission to access it.
-      return TRUE;
+      return $showDashlet || $hasPermission;
     }
+    // If permission is not set consider everyone has access.
+    return TRUE;
   }
 
   /**