PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/CRM/Core/Permission/Drupal.php

https://github.com/ksecor/civicrm
PHP | 258 lines | 122 code | 27 blank | 109 comment | 26 complexity | 62dd2ea436204e8990592a17219721aa MD5 | raw file
  1. <?php
  2. /*
  3. +--------------------------------------------------------------------+
  4. | CiviCRM version 3.1 |
  5. +--------------------------------------------------------------------+
  6. | Copyright CiviCRM LLC (c) 2004-2009 |
  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. |
  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 along with this program; if not, contact CiviCRM LLC |
  21. | at info[AT]civicrm[DOT]org. If you have questions about the |
  22. | GNU Affero General Public License or the licensing of CiviCRM, |
  23. | see the CiviCRM license FAQ at http://civicrm.org/licensing |
  24. +--------------------------------------------------------------------+
  25. */
  26. /**
  27. *
  28. * @package CRM
  29. * @copyright CiviCRM LLC (c) 2004-2009
  30. * $Id$
  31. *
  32. */
  33. /**
  34. *
  35. */
  36. class CRM_Core_Permission_Drupal {
  37. /**
  38. * is this user someone with access for the entire system
  39. *
  40. * @var boolean
  41. */
  42. static protected $_viewAdminUser = false;
  43. static protected $_editAdminUser = false;
  44. /**
  45. * am in in view permission or edit permission?
  46. * @var boolean
  47. */
  48. static protected $_viewPermission = false;
  49. static protected $_editPermission = false;
  50. /**
  51. * the current set of permissioned groups for the user
  52. *
  53. * @var array
  54. */
  55. static protected $_viewPermissionedGroups;
  56. static protected $_editPermissionedGroups;
  57. /**
  58. * Get all groups from database, filtered by permissions
  59. * for this user
  60. *
  61. * @param string $groupType type of group(Access/Mailing)
  62. * @param boolen $excludeHidden exclude hidden groups.
  63. *
  64. * @access public
  65. * @static
  66. *
  67. * @return array - array reference of all groups.
  68. *
  69. */
  70. public static function &group( $groupType = null, $excludeHidden = true ) {
  71. if ( ! isset( self::$_viewPermissionedGroups ) ) {
  72. self::$_viewPermissionedGroups = self::$_editPermissionedGroups = array( );
  73. $groups =& CRM_Core_PseudoConstant::allGroup( $groupType, $excludeHidden );
  74. if ( self::check( 'edit all contacts' ) ) {
  75. // this is the most powerful permission, so we return
  76. // immediately rather than dilute it further
  77. self::$_editAdminUser = self::$_viewAdminUser = true;
  78. self::$_editPermission = self::$_viewPermission = true;
  79. self::$_editPermissionedGroups = $groups;
  80. self::$_viewPermissionedGroups = $groups;
  81. return self::$_viewPermissionedGroups;
  82. } else if ( self::check( 'view all contacts' ) ) {
  83. self::$_viewAdminUser = true;
  84. self::$_viewPermission = true;
  85. self::$_viewPermissionedGroups = $groups;
  86. }
  87. require_once 'CRM/ACL/API.php';
  88. $ids = CRM_ACL_API::group( CRM_Core_Permission::VIEW, null, 'civicrm_saved_search', $groups );
  89. foreach ( array_values( $ids ) as $id ) {
  90. $title = CRM_Core_DAO::getFieldValue( 'CRM_Contact_DAO_Group', $id, 'title' );
  91. self::$_viewPermissionedGroups[$id] = $title;
  92. self::$_viewPermission = true;
  93. }
  94. $ids = CRM_ACL_API::group( CRM_Core_Permission::EDIT, null, 'civicrm_saved_search', $groups );
  95. foreach ( array_values( $ids ) as $id ) {
  96. $title = CRM_Core_DAO::getFieldValue( 'CRM_Contact_DAO_Group', $id, 'title' );
  97. self::$_editPermissionedGroups[$id] = $title;
  98. self::$_viewPermissionedGroups[$id] = $title;
  99. self::$_editPermission = true;
  100. self::$_viewPermission = true;
  101. }
  102. }
  103. return self::$_viewPermissionedGroups;
  104. }
  105. /**
  106. * Get group clause for this user
  107. *
  108. * @param int $type the type of permission needed
  109. * @param array $tables (reference ) add the tables that are needed for the select clause
  110. * @param array $whereTables (reference ) add the tables that are needed for the where clause
  111. *
  112. * @return string the group where clause for this user
  113. * @access public
  114. */
  115. public static function groupClause( $type, &$tables, &$whereTables ) {
  116. if (! isset( self::$_viewPermissionedGroups ) ) {
  117. self::group( );
  118. }
  119. if ( $type == CRM_Core_Permission::EDIT ) {
  120. if ( self::$_editAdminUser ) {
  121. $clause = ' ( 1 ) ';
  122. } else if ( empty( self::$_editPermissionedGroups ) ) {
  123. $clause = ' ( 0 ) ';
  124. } else {
  125. $clauses = array( );
  126. $groups = implode( ', ', self::$_editPermissionedGroups );
  127. $clauses[] = ' ( civicrm_group_contact.group_id IN ( ' . implode( ', ', array_keys( self::$_editPermissionedGroups ) ) .
  128. " ) AND civicrm_group_contact.status = 'Added' ) ";
  129. $tables['civicrm_group_contact'] = 1;
  130. $whereTables['civicrm_group_contact'] = 1;
  131. // foreach group that is potentially a saved search, add the saved search clause
  132. foreach ( array_keys( self::$_editPermissionedGroups ) as $id ) {
  133. $group = new CRM_Contact_DAO_Group( );
  134. $group->id = $id;
  135. if ( $group->find( true ) && $group->saved_search_id ) {
  136. require_once 'CRM/Contact/BAO/SavedSearch.php';
  137. $clause = CRM_Contact_BAO_SavedSearch::whereClause( $group->saved_search_id,
  138. $tables,
  139. $whereTables );
  140. if ( trim( $clause ) ) {
  141. $clauses[] = $clause;
  142. }
  143. }
  144. }
  145. $clause = ' ( ' . implode( ' OR ', $clauses ) . ' ) ';
  146. }
  147. } else {
  148. if ( self::$_viewAdminUser ) {
  149. $clause = ' ( 1 ) ';
  150. } else if ( empty( self::$_viewPermissionedGroups ) ) {
  151. $clause = ' ( 0 ) ';
  152. } else {
  153. $clauses = array( );
  154. $groups = implode( ', ', self::$_viewPermissionedGroups );
  155. $clauses[] = ' ( civicrm_group_contact.group_id IN (' . implode( ', ', array_keys( self::$_viewPermissionedGroups ) ) .
  156. " ) AND civicrm_group_contact.status = 'Added' ) ";
  157. $tables['civicrm_group_contact'] = 1;
  158. $whereTables['civicrm_group_contact'] = 1;
  159. // foreach group that is potentially a saved search, add the saved search clause
  160. foreach ( array_keys( self::$_viewPermissionedGroups ) as $id ) {
  161. $group = new CRM_Contact_DAO_Group( );
  162. $group->id = $id;
  163. if ( $group->find( true ) && $group->saved_search_id ) {
  164. require_once 'CRM/Contact/BAO/SavedSearch.php';
  165. $clause = CRM_Contact_BAO_SavedSearch::whereClause( $group->saved_search_id,
  166. $tables,
  167. $whereTables );
  168. if ( trim( $clause ) ) {
  169. $clauses[] = $clause;
  170. }
  171. }
  172. }
  173. $clause = ' ( ' . implode( ' OR ', $clauses ) . ' ) ';
  174. }
  175. }
  176. return $clause;
  177. }
  178. /**
  179. * get the current permission of this user
  180. *
  181. * @return string the permission of the user (edit or view or null)
  182. */
  183. public static function getPermission( ) {
  184. self::group( );
  185. if ( self::$_editPermission ) {
  186. return CRM_Core_Permission::EDIT;
  187. } else if ( self::$_viewPermission ) {
  188. return CRM_Core_Permission::VIEW;
  189. }
  190. return null;
  191. }
  192. /**
  193. * Get the permissioned where clause for the user
  194. *
  195. * @param int $type the type of permission needed
  196. * @param array $tables (reference ) add the tables that are needed for the select clause
  197. * @param array $whereTables (reference ) add the tables that are needed for the where clause
  198. *
  199. * @return string the group where clause for this user
  200. * @access public
  201. */
  202. public static function whereClause( $type, &$tables, &$whereTables ) {
  203. self::group( );
  204. return self::groupClause( $type, $tables, $whereTables );
  205. }
  206. /**
  207. * given a permission string, check for access requirements
  208. *
  209. * @param string $str the permission to check
  210. *
  211. * @return boolean true if yes, else false
  212. * @static
  213. * @access public
  214. */
  215. static function check( $str, $contactID = null ) {
  216. if ( function_exists( 'user_access' ) ) {
  217. return user_access( $str ) ? true : false;
  218. }
  219. return true;
  220. /**
  221. * lets introduce acl in 2.1
  222. static $isAdmin = null;
  223. if ( $isAdmin === null ) {
  224. $session =& CRM_Core_Session::singleton( );
  225. $isAdmin = $session->get( 'ufID' ) == 1 ? true : false;
  226. }
  227. require_once 'CRM/ACL/API.php';
  228. return ( $isAdmin) ? true : CRM_ACL_API::check( $str, $contactID );
  229. */
  230. }
  231. }