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
53 * The permission to check.
54 * @param int $contactID
55 * The contactID for whom the check is made.
58 * true if yes, else false
61 public static function check($str, $contactID = NULL) {
62 if ($contactID == NULL) {
63 $session = CRM_Core_Session
::singleton();
64 $contactID = $session->get('userID');
72 return CRM_ACL_BAO_ACL
::check($str, $contactID);
76 * Get the permissioned where clause for the user
79 * The type of permission needed.
80 * @param array $tables
81 * (reference ) add the tables that are needed for the select clause.
82 * @param array $whereTables
83 * (reference ) add the tables that are needed for the where clause.
84 * @param int $contactID
85 * The contactID for whom the check is made.
86 * @param bool $onlyDeleted
87 * Whether to include only deleted contacts.
88 * @param bool $skipDeleteClause
89 * Don't add delete clause if this is true,.
90 * this means it is handled by generating query
93 * the group where clause for this user
95 public static function whereClause(
100 $onlyDeleted = FALSE,
101 $skipDeleteClause = FALSE
103 // the default value which is valid for rhe final AND
104 $deleteClause = ' ( 1 ) ';
105 if (!$skipDeleteClause) {
106 if (CRM_Core_Permission
::check('access deleted contacts') and $onlyDeleted) {
107 $deleteClause = '(contact_a.is_deleted)';
111 $deleteClause = '(contact_a.is_deleted = 0)';
115 // first see if the contact has edit / view all contacts
116 if (CRM_Core_Permission
::check('edit all contacts') ||
117 ($type == self
::VIEW
&&
118 CRM_Core_Permission
::check('view all contacts')
121 return $skipDeleteClause ?
' ( 1 ) ' : $deleteClause;
124 if ($contactID == NULL) {
125 $session = CRM_Core_Session
::singleton();
126 $contactID = $session->get('userID');
134 return implode(' AND ',
136 CRM_ACL_BAO_ACL
::whereClause($type,
147 * Get all the groups the user has access to for the given operation
150 * The type of permission needed.
151 * @param int $contactID
152 * The contactID for whom the check is made.
154 * @param string $tableName
155 * @param null $allGroups
156 * @param null $includedGroups
159 * the ids of the groups for which the user has permissions
161 public static function group(
164 $tableName = 'civicrm_saved_search',
166 $includedGroups = NULL
168 if ($contactID == NULL) {
169 $session = CRM_Core_Session
::singleton();
170 $contactID = $session->get('userID');
178 return CRM_ACL_BAO_ACL
::group($type, $contactID, $tableName, $allGroups, $includedGroups);
182 * Check if the user has access to this group for operation $type
185 * The type of permission needed.
186 * @param int $groupID
187 * @param int $contactID
188 * The contactID for whom the check is made.
189 * @param string $tableName
190 * @param null $allGroups
191 * @param null $includedGroups
195 * the ids of the groups for which the user has permissions
197 public static function groupPermission(
201 $tableName = 'civicrm_saved_search',
203 $includedGroups = NULL,
207 static $cache = array();
208 //@todo this is pretty hacky!!!
209 //adding a way for unit tests to flush the cache
215 $session = CRM_Core_Session
::singleton();
217 if ($session->get('userID')) {
218 $contactID = $session->get('userID');
222 $key = "{$tableName}_{$type}_{$contactID}";
223 if (array_key_exists($key, $cache)) {
224 $groups = &$cache[$key];
227 $groups = self
::group($type, $contactID, $tableName, $allGroups, $includedGroups);
228 $cache[$key] = $groups;
231 return in_array($groupID, $groups) ?
TRUE : FALSE;