3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
36 * The various type of permissions.
48 * Given a permission string, check for access requirements
51 * The permission to check.
52 * @param int $contactID
53 * The contactID for whom the check is made.
56 * 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.
76 * The type of permission needed.
77 * @param array $tables
78 * (reference ) add the tables that are needed for the select clause.
79 * @param array $whereTables
80 * (reference ) add the tables that are needed for the where clause.
81 * @param int $contactID
82 * The contactID for whom the check is made.
83 * @param bool $onlyDeleted
84 * Whether to include only deleted contacts.
85 * @param bool $skipDeleteClause
86 * Don't add delete clause if this is true,.
87 * this means it is handled by generating query
90 * the group where clause for this user
92 public static function whereClause(
98 $skipDeleteClause = FALSE
100 // the default value which is valid for the final AND
101 $deleteClause = ' ( 1 ) ';
102 if (!$skipDeleteClause) {
103 if (CRM_Core_Permission
::check('access deleted contacts') and $onlyDeleted) {
104 $deleteClause = '(contact_a.is_deleted)';
108 $deleteClause = '(contact_a.is_deleted = 0)';
112 // first see if the contact has edit / view all contacts
113 if (CRM_Core_Permission
::check('edit all contacts') ||
114 ($type == self
::VIEW
&& CRM_Core_Permission
::check('view all contacts'))
116 return $deleteClause;
120 $contactID = CRM_Core_Session
::getLoggedInContactID();
122 $contactID = (int) $contactID;
124 $where = implode(' AND ',
126 CRM_ACL_BAO_ACL
::whereClause($type,
135 // Add permission on self
136 if ($contactID && (CRM_Core_Permission
::check('edit my contact') ||
137 $type == self
::VIEW
&& CRM_Core_Permission
::check('view my contact'))
139 $where = "(contact_a.id = $contactID OR ($where))";
145 * Get all the groups the user has access to for the given operation.
148 * The type of permission needed.
149 * @param int $contactID
150 * The contactID for whom the check is made.
152 * @param string $tableName
153 * @param null $allGroups
154 * @param null $includedGroups
157 * the ids of the groups for which the user has permissions
159 public static function group(
162 $tableName = 'civicrm_saved_search',
164 $includedGroups = NULL
166 if ($contactID == NULL) {
167 $session = CRM_Core_Session
::singleton();
168 $contactID = $session->get('userID');
176 return CRM_ACL_BAO_ACL
::group($type, $contactID, $tableName, $allGroups, $includedGroups);
180 * Check if the user has access to this group for operation $type
183 * The type of permission needed.
184 * @param int $groupID
185 * @param int $contactID
186 * The contactID for whom the check is made.
187 * @param string $tableName
188 * @param null $allGroups
189 * @param null $includedGroups
193 * the ids of the groups for which the user has permissions
195 public static function groupPermission(
199 $tableName = 'civicrm_saved_search',
201 $includedGroups = NULL,
205 static $cache = array();
207 //@todo this is pretty hacky!!!
208 //adding a way for unit tests to flush the cache
214 $session = CRM_Core_Session
::singleton();
216 if ($session->get('userID')) {
217 $contactID = $session->get('userID');
221 $key = "{$tableName}_{$type}_{$contactID}";
222 if (array_key_exists($key, $cache)) {
223 $groups = &$cache[$key];
226 $groups = self
::group($type, $contactID, $tableName, $allGroups, $includedGroups);
227 $cache[$key] = $groups;
229 if (empty($groups)) {
233 return in_array($groupID, $groups) ?
TRUE : FALSE;