CRM-14995 tidyups
[civicrm-core.git] / CRM / Core / Permission.php
index 0c3671f7eb3b9833b82b3805d308722ba17836b0..c6ff25a6eebddb257587efdfdc076870bc2e3c38 100644 (file)
@@ -1,9 +1,9 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.4                                                |
+ | CiviCRM version 4.5                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2013                                |
+ | Copyright CiviCRM LLC (c) 2004-2014                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -28,7 +28,7 @@
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2013
+ * @copyright CiviCRM LLC (c) 2004-2014
  * $Id$
  *
  */
@@ -188,20 +188,22 @@ class CRM_Core_Permission {
    * Get all groups from database, filtered by permissions
    * for this user
    *
-   * @param string $groupType     type of group(Access/Mailing)
-   * @param boolen $excludeHidden exclude hidden groups.
+   * @param string $groupType type of group(Access/Mailing)
+   * @param bool|\boolen $excludeHidden exclude hidden groups.
    *
    * @access public
    * @static
    *
    * @return array - array reference of all groups.
-   *
    */
   public static function group($groupType, $excludeHidden = TRUE) {
     $config = CRM_Core_Config::singleton();
     return $config->userPermissionClass->group($groupType, $excludeHidden);
   }
 
+  /**
+   * @return bool
+   */
   public static function customGroupAdmin() {
     $admin = FALSE;
 
@@ -225,6 +227,12 @@ class CRM_Core_Permission {
     return FALSE;
   }
 
+  /**
+   * @param int $type
+   * @param bool $reset
+   *
+   * @return array
+   */
   public static function customGroup($type = CRM_Core_Permission::VIEW, $reset = FALSE) {
     $customGroups = CRM_Core_PseudoConstant::get('CRM_Core_DAO_CustomField', 'custom_group_id',
       array('fresh' => $reset));
@@ -239,6 +247,13 @@ class CRM_Core_Permission {
     return CRM_ACL_API::group($type, NULL, 'civicrm_custom_group', $customGroups, $defaultGroups);
   }
 
+  /**
+   * @param int $type
+   * @param null $prefix
+   * @param bool $reset
+   *
+   * @return string
+   */
   static function customGroupClause($type = CRM_Core_Permission::VIEW, $prefix = NULL, $reset = FALSE) {
     if (self::customGroupAdmin()) {
       return ' ( 1 ) ';
@@ -253,15 +268,26 @@ class CRM_Core_Permission {
     }
   }
 
+  /**
+   * @param $gid
+   * @param int $type
+   *
+   * @return bool
+   */
   public static function ufGroupValid($gid, $type = CRM_Core_Permission::VIEW) {
     if (empty($gid)) {
       return TRUE;
     }
 
     $groups = self::ufGroup($type);
-    return in_array($gid, $groups) ? TRUE : FALSE;
+    return !empty($groups) && in_array($gid, $groups) ? TRUE : FALSE;
   }
 
+  /**
+   * @param int $type
+   *
+   * @return array
+   */
   public static function ufGroup($type = CRM_Core_Permission::VIEW) {
     $ufGroups = CRM_Core_PseudoConstant::get('CRM_Core_DAO_UFField', 'uf_group_id');
 
@@ -301,6 +327,13 @@ class CRM_Core_Permission {
     return CRM_ACL_API::group($type, NULL, 'civicrm_uf_group', $ufGroups);
   }
 
+  /**
+   * @param int $type
+   * @param null $prefix
+   * @param bool $returnUFGroupIds
+   *
+   * @return array|string
+   */
   static function ufGroupClause($type = CRM_Core_Permission::VIEW, $prefix = NULL, $returnUFGroupIds = FALSE) {
     $groups = self::ufGroup($type);
     if ($returnUFGroupIds) {
@@ -314,7 +347,19 @@ class CRM_Core_Permission {
     }
   }
 
-  public static function event($type = CRM_Core_Permission::VIEW, $eventID = NULL) {
+  /**
+   * @param int $type
+   * @param null $eventID
+   * @param string $context
+   *
+   * @return array|null
+   */
+  public static function event($type = CRM_Core_Permission::VIEW, $eventID = NULL, $context = '') {
+    if(!empty($context)) {
+      if(CRM_Core_Permission::check($context)) {
+        return TRUE;
+      }
+    }
     $events = CRM_Event_PseudoConstant::event(NULL, TRUE);
     $includeEvents = array();
 
@@ -339,6 +384,12 @@ class CRM_Core_Permission {
     return NULL;
   }
 
+  /**
+   * @param int $type
+   * @param null $prefix
+   *
+   * @return string
+   */
   static function eventClause($type = CRM_Core_Permission::VIEW, $prefix = NULL) {
     $events = self::event($type);
     if (empty($events)) {
@@ -349,6 +400,12 @@ class CRM_Core_Permission {
     }
   }
 
+  /**
+   * @param $module
+   * @param bool $checkPermission
+   *
+   * @return bool
+   */
   static function access($module, $checkPermission = TRUE) {
     $config = CRM_Core_Config::singleton();
 
@@ -372,9 +429,11 @@ class CRM_Core_Permission {
    * check permissions for delete and edit actions
    *
    * @param string $module component name.
-   * @param $action action to be check across component
+   * @param integer $action action to be check across component
    *
-   **/
+   *
+   * @return bool
+   */
   static function checkActionPermission($module, $action) {
     //check delete related permissions.
     if ($action & CRM_Core_Action::DELETE) {
@@ -402,6 +461,12 @@ class CRM_Core_Permission {
     }
   }
 
+  /**
+   * @param $args
+   * @param string $op
+   *
+   * @return bool
+   */
   static function checkMenu(&$args, $op = 'and') {
     if (!is_array($args)) {
       return $args;
@@ -418,6 +483,12 @@ class CRM_Core_Permission {
     return ($op == 'or') ? FALSE : TRUE;
   }
 
+  /**
+   * @param $item
+   *
+   * @return bool|mixed
+   * @throws Exception
+   */
   static function checkMenuItem(&$item) {
     if (!array_key_exists('access_callback', $item)) {
       CRM_Core_Error::backtrace();
@@ -467,10 +538,16 @@ class CRM_Core_Permission {
     }
   }
 
+  /**
+   * @param bool $all
+   *
+   * @return array
+   */
   static function &basicPermissions($all = FALSE) {
     static $permissions = NULL;
 
     if (!$permissions) {
+      $config = CRM_Core_Config::singleton();
       $prefix = ts('CiviCRM') . ': ';
       $permissions = self::getCorePermissions();
 
@@ -478,8 +555,6 @@ class CRM_Core_Permission {
         $permissions['administer Multiple Organizations'] = $prefix . ts('administer Multiple Organizations');
       }
 
-      $config = CRM_Core_Config::singleton();
-
       if (!$all) {
         $components = CRM_Core_Component::getEnabledComponents();
       }
@@ -498,7 +573,6 @@ class CRM_Core_Permission {
       }
 
       // Add any permissions defined in hook_civicrm_permission implementations.
-      $config = CRM_Core_Config::singleton();
       $module_permissions = $config->userPermissionClass->getAllModulePermissions();
       $permissions = array_merge($permissions, $module_permissions);
     }
@@ -506,6 +580,38 @@ class CRM_Core_Permission {
     return $permissions;
   }
 
+  /**
+   * @return array
+   */
+  static function getAnonymousPermissionsWarnings() {
+    static $permissions = array();
+    if (empty($permissions)) {
+      $permissions = array(
+        'administer CiviCRM'
+      );
+      $components = CRM_Core_Component::getComponents();
+      foreach ($components as $comp) {
+        if (!method_exists($comp, 'getAnonymousPermissionWarnings')) {
+          continue;
+        }
+        $permissions = array_merge($permissions, $comp->getAnonymousPermissionWarnings());
+      }
+    }
+    return $permissions;
+  }
+
+  /**
+   * @param $anonymous_perms
+   *
+   * @return array
+   */
+  static function validateForPermissionWarnings($anonymous_perms) {
+    return array_intersect($anonymous_perms, self::getAnonymousPermissionsWarnings());
+  }
+
+  /**
+   * @return array
+   */
   static function getCorePermissions() {
     $prefix = ts('CiviCRM') . ': ';
     $permissions = array(
@@ -600,6 +706,8 @@ class CRM_Core_Permission {
    * @param string $permission
    *
    * return string $componentName the name of component.
+   *
+   * @return int|null|string
    * @static
    */
   static function getComponentName($permission) {
@@ -654,6 +762,9 @@ class CRM_Core_Permission {
     return $config->userRoleClass->roleEmails($roleName);
   }
 
+  /**
+   * @return bool
+   */
   static function isMultisiteEnabled() {
     return CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MULTISITE_PREFERENCES_NAME,
       'is_enabled'