Merge pull request #23742 from eileenmcnaughton/import_remove
[civicrm-core.git] / CRM / Core / Task.php
index a947f4729c001b9fd6fb7cfdbd10d770ed01a87e..3ef3690a722ecb3d58016d31f9806d060328a562 100644 (file)
@@ -47,7 +47,7 @@ abstract class CRM_Core_Task {
    *
    * @var array
    */
-  public static $_tasks = NULL;
+  public static $_tasks = [];
 
   /**
    * @var string
@@ -165,6 +165,51 @@ abstract class CRM_Core_Task {
     ];
   }
 
+  /**
+   * Filter tasks based on the permission key, if available.
+   *
+   * @param array $tasks
+   * @param bool $hasEditContactPermission
+   *   Does the user have permission to edit the given contact. Required where
+   *   permission to edit the user is required in conjunction with permission
+   *   to do the task.
+   *
+   * @return array
+   */
+  protected static function getTasksFilteredByPermission(array $tasks, bool $hasEditContactPermission): array {
+    foreach ($tasks as $index => $task) {
+      // requires_edit_contact_permission is a (hopefully transient way) of denoting which
+      // tasks need 'edit this contact' on top of the membership permission.
+      if (!empty($task['requires_edit_contact_permission']) && !$hasEditContactPermission) {
+        unset($tasks[$index]);
+      }
+      elseif (!empty($task['permissions']) && !CRM_Core_Permission::check($task['permissions'])) {
+        unset($tasks[$index]);
+      }
+    }
+    return $tasks;
+  }
+
+  /**
+   * Get task tiles filtered by any declared permissions.
+   *
+   * @param array $tasks
+   * @param bool $hasEditContactPermission
+   *   Does the user have permission to edit the given contact. Required where
+   *   permission to edit the user is required in conjunction with permission
+   *   to do the task.
+   *
+   * @return array
+   */
+  protected static function getTitlesFilteredByPermission(array $tasks, bool $hasEditContactPermission): array {
+    $availableTasks = self::getTasksFilteredByPermission($tasks, $hasEditContactPermission);
+    $return = [];
+    foreach ($availableTasks as $key => $details) {
+      $return[$key] = $details['title'];
+    }
+    return $return;
+  }
+
   /**
    * Function to return the task information on basis of provided task's form name
    *