3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
38 * The various type of permissions
50 * Given a permission string, check for access requirements
52 * @param string $str the permission to check
53 * @param int $contactID the contactID for whom the check is made
55 * @return boolean true if yes, else false
58 public static function check($str, $contactID = NULL) {
59 if ($contactID == NULL) {
60 $session = CRM_Core_Session
::singleton();
61 $contactID = $session->get('userID');
69 return CRM_ACL_BAO_ACL
::check($str, $contactID);
73 * Get the permissioned where clause for the user
75 * @param int $type the type of permission needed
76 * @param array $tables (reference ) add the tables that are needed for the select clause
77 * @param array $whereTables (reference ) add the tables that are needed for the where clause
78 * @param int $contactID the contactID for whom the check is made
79 * @param bool $onlyDeleted whether to include only deleted contacts
80 * @param bool $skipDeleteClause don't add delete clause if this is true,
81 * this means it is handled by generating query
83 * @return string the group where clause for this user
85 public static function whereClause($type,
90 $skipDeleteClause = FALSE
92 // the default value which is valid for rhe final AND
93 $deleteClause = ' ( 1 ) ';
94 if (!$skipDeleteClause) {
95 if (CRM_Core_Permission
::check('access deleted contacts') and $onlyDeleted) {
96 $deleteClause = '(contact_a.is_deleted)';
100 $deleteClause = '(contact_a.is_deleted = 0)';
104 // first see if the contact has edit / view all contacts
105 if (CRM_Core_Permission
::check('edit all contacts') ||
106 ($type == self
::VIEW
&&
107 CRM_Core_Permission
::check('view all contacts')
110 return $skipDeleteClause ?
' ( 1 ) ' : $deleteClause;
113 if ($contactID == NULL) {
114 $session = CRM_Core_Session
::singleton();
115 $contactID = $session->get('userID');
123 return implode(' AND ',
125 CRM_ACL_BAO_ACL
::whereClause($type,
136 * Get all the groups the user has access to for the given operation
138 * @param int $type the type of permission needed
139 * @param int $contactID the contactID for whom the check is made
141 * @param string $tableName
142 * @param null $allGroups
143 * @param null $includedGroups
145 * @return array the ids of the groups for which the user has permissions
147 public static function group(
150 $tableName = 'civicrm_saved_search',
152 $includedGroups = NULL
154 if ($contactID == NULL) {
155 $session = CRM_Core_Session
::singleton();
156 $contactID = $session->get('userID');
164 return CRM_ACL_BAO_ACL
::group($type, $contactID, $tableName, $allGroups, $includedGroups);
168 * Check if the user has access to this group for operation $type
170 * @param int $type the type of permission needed
171 * @param int $groupID
172 * @param int $contactID the contactID for whom the check is made
174 * @param string $tableName
175 * @param null $allGroups
176 * @param null $includedGroups
179 * @return array the ids of the groups for which the user has permissions
181 public static function groupPermission(
185 $tableName = 'civicrm_saved_search',
187 $includedGroups = NULL,
191 static $cache = array();
192 //@todo this is pretty hacky!!!
193 //adding a way for unit tests to flush the cache
199 $session = CRM_Core_Session
::singleton();
201 if ($session->get('userID')) {
202 $contactID = $session->get('userID');
206 $key = "{$tableName}_{$type}_{$contactID}";
207 if (array_key_exists($key, $cache)) {
208 $groups = &$cache[$key];
211 $groups = self
::group($type, $contactID, $tableName, $allGroups, $includedGroups);
212 $cache[$key] = $groups;
215 return in_array($groupID, $groups) ?
TRUE : FALSE;