/drupal/sites/all/modules/civicrm/CRM/ACL/API.php

https://github.com/michaelmcandrew/th · PHP · 187 lines · 83 code · 22 blank · 82 comment · 16 complexity · c14f6dd9078a5a1630ca4e3d527cd3cb MD5 · raw file

  1. <?php
  2. /*
  3. +--------------------------------------------------------------------+
  4. | CiviCRM version 4.0 |
  5. +--------------------------------------------------------------------+
  6. | Copyright CiviCRM LLC (c) 2004-2011 |
  7. +--------------------------------------------------------------------+
  8. | This file is a part of CiviCRM. |
  9. | |
  10. | CiviCRM is free software; you can copy, modify, and distribute it |
  11. | under the terms of the GNU Affero General Public License |
  12. | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
  13. | |
  14. | CiviCRM is distributed in the hope that it will be useful, but |
  15. | WITHOUT ANY WARRANTY; without even the implied warranty of |
  16. | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
  17. | See the GNU Affero General Public License for more details. |
  18. | |
  19. | You should have received a copy of the GNU Affero General Public |
  20. | License and the CiviCRM Licensing Exception along |
  21. | with this program; if not, contact CiviCRM LLC |
  22. | at info[AT]civicrm[DOT]org. If you have questions about the |
  23. | GNU Affero General Public License or the licensing of CiviCRM, |
  24. | see the CiviCRM license FAQ at http://civicrm.org/licensing |
  25. +--------------------------------------------------------------------+
  26. */
  27. /**
  28. *
  29. * @package CRM
  30. * @copyright CiviCRM LLC (c) 2004-2011
  31. * $Id$
  32. *
  33. */
  34. class CRM_ACL_API {
  35. /**
  36. * The various type of permissions
  37. *
  38. * @var int
  39. */
  40. const
  41. EDIT = 1,
  42. VIEW = 2,
  43. DELETE = 3,
  44. CREATE = 4,
  45. SEARCH = 5,
  46. ALL = 6;
  47. /**
  48. * given a permission string, check for access requirements
  49. *
  50. * @param string $str the permission to check
  51. * @param int $contactID the contactID for whom the check is made
  52. *
  53. * @return boolean true if yes, else false
  54. * @static
  55. * @access public
  56. */
  57. static function check( $str, $contactID = null ) {
  58. if ( $contactID == null ) {
  59. $session = CRM_Core_Session::singleton( );
  60. $contactID = $session->get( 'userID' );
  61. }
  62. if ( ! $contactID ) {
  63. $contactID = 0; // anonymous user
  64. }
  65. require_once 'CRM/ACL/BAO/ACL.php';
  66. return CRM_ACL_BAO_ACL::check( $str, $contactID );
  67. }
  68. /**
  69. * Get the permissioned where clause for the user
  70. *
  71. * @param int $type the type of permission needed
  72. * @param array $tables (reference ) add the tables that are needed for the select clause
  73. * @param array $whereTables (reference ) add the tables that are needed for the where clause
  74. * @param int $contactID the contactID for whom the check is made
  75. * @param bool $onlyDeleted whether to include only deleted contacts
  76. * @param bool $skipDeleteClause don't add delete clause if this is true,
  77. * this means it is handled by generating query
  78. *
  79. * @return string the group where clause for this user
  80. * @access public
  81. */
  82. public static function whereClause( $type,
  83. &$tables,
  84. &$whereTables,
  85. $contactID = null,
  86. $onlyDeleted = false,
  87. $skipDeleteClause = false ) {
  88. // the default value which is valid for rhe final AND
  89. $deleteClause = ' ( 1 ) ';
  90. if ( ! $skipDeleteClause ) {
  91. if (CRM_Core_Permission::check('access deleted contacts') and $onlyDeleted) {
  92. $deleteClause = '(contact_a.is_deleted)';
  93. } else {
  94. // CRM-6181
  95. $deleteClause = '(contact_a.is_deleted = 0)';
  96. }
  97. }
  98. // first see if the contact has edit / view all contacts
  99. if ( CRM_Core_Permission::check( 'edit all contacts' ) ||
  100. ( $type == self::VIEW &&
  101. CRM_Core_Permission::check( 'view all contacts' ) ) ) {
  102. return $skipDeleteClause ? ' ( 1 ) ' : $deleteClause;
  103. }
  104. if ( $contactID == null ) {
  105. $session = CRM_Core_Session::singleton( );
  106. $contactID = $session->get( 'userID' );
  107. }
  108. if ( ! $contactID ) {
  109. $contactID = 0; // anonymous user
  110. }
  111. require_once 'CRM/ACL/BAO/ACL.php';
  112. return implode( ' AND ',
  113. array( CRM_ACL_BAO_ACL::whereClause( $type,
  114. $tables,
  115. $whereTables,
  116. $contactID ),
  117. $deleteClause ) );
  118. }
  119. /**
  120. * get all the groups the user has access to for the given operation
  121. *
  122. * @param int $type the type of permission needed
  123. * @param int $contactID the contactID for whom the check is made
  124. *
  125. * @return array the ids of the groups for which the user has permissions
  126. * @access public
  127. */
  128. public static function group( $type, $contactID = null,
  129. $tableName = 'civicrm_saved_search',
  130. $allGroups = null,
  131. $includedGroups = null ) {
  132. if ( $contactID == null ) {
  133. $session = CRM_Core_Session::singleton( );
  134. $contactID = $session->get( 'userID' );
  135. }
  136. if ( ! $contactID ) {
  137. $contactID = 0; // anonymous user
  138. }
  139. require_once 'CRM/ACL/BAO/ACL.php';
  140. return CRM_ACL_BAO_ACL::group( $type, $contactID, $tableName, $allGroups, $includedGroups );
  141. }
  142. /**
  143. * check if the user has access to this group for operation $type
  144. *
  145. * @param int $type the type of permission needed
  146. * @param int $contactID the contactID for whom the check is made
  147. *
  148. * @return array the ids of the groups for which the user has permissions
  149. * @access public
  150. */
  151. public static function groupPermission( $type, $groupID, $contactID = null,
  152. $tableName = 'civicrm_saved_search',
  153. $allGroups = null,
  154. $includedGroups = null ) {
  155. static $cache = array( );
  156. $key = "{$tableName}_{$type}_{$contactID}";
  157. if ( array_key_exists( $key, $cache ) ) {
  158. $groups =& $cache[$key];
  159. } else {
  160. $groups =& self::group( $type, $contactID, $tableName, $allGroups, $includedGroups );
  161. $cache[$key] = $groups;
  162. }
  163. return in_array( $groupID, $groups ) ? true : false;
  164. }
  165. }