3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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
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
60 public static function check($str, $contactID = NULL) {
61 if ($contactID == NULL) {
62 $session = CRM_Core_Session
::singleton();
63 $contactID = $session->get('userID');
71 return CRM_ACL_BAO_ACL
::check($str, $contactID);
75 * Get the permissioned where clause for the user.
78 * The type of permission needed.
79 * @param array $tables
80 * (reference ) add the tables that are needed for the select clause.
81 * @param array $whereTables
82 * (reference ) add the tables that are needed for the where clause.
83 * @param int $contactID
84 * The contactID for whom the check is made.
85 * @param bool $onlyDeleted
86 * Whether to include only deleted contacts.
87 * @param bool $skipDeleteClause
88 * Don't add delete clause if this is true,.
89 * this means it is handled by generating query
92 * the group where clause for this user
94 public static function whereClause(
100 $skipDeleteClause = FALSE
102 // the default value which is valid for the final AND
103 $deleteClause = ' ( 1 ) ';
104 if (!$skipDeleteClause) {
105 if (CRM_Core_Permission
::check('access deleted contacts') and $onlyDeleted) {
106 $deleteClause = '(contact_a.is_deleted)';
110 $deleteClause = '(contact_a.is_deleted = 0)';
114 // first see if the contact has edit / view all contacts
115 if (CRM_Core_Permission
::check('edit all contacts') ||
116 ($type == self
::VIEW
&& CRM_Core_Permission
::check('view all contacts'))
118 return $deleteClause;
121 $user = CRM_Core_Session
::getLoggedInContactID();
122 if ($contactID == NULL) {
123 $contactID = $user ?
$user : 0;
126 // Check if contact has permissions on self
127 if ($user && $contactID == $user) {
128 if (CRM_Core_Permission
::check('edit my contact') ||
129 ($type == self
::VIEW
&& CRM_Core_Permission
::check('view my contact'))
135 return implode(' AND ',
137 CRM_ACL_BAO_ACL
::whereClause($type,
148 * Get all the groups the user has access to for the given operation.
151 * The type of permission needed.
152 * @param int $contactID
153 * The contactID for whom the check is made.
155 * @param string $tableName
156 * @param null $allGroups
157 * @param null $includedGroups
160 * the ids of the groups for which the user has permissions
162 public static function group(
165 $tableName = 'civicrm_saved_search',
167 $includedGroups = NULL
169 if ($contactID == NULL) {
170 $session = CRM_Core_Session
::singleton();
171 $contactID = $session->get('userID');
179 return CRM_ACL_BAO_ACL
::group($type, $contactID, $tableName, $allGroups, $includedGroups);
183 * Check if the user has access to this group for operation $type
186 * The type of permission needed.
187 * @param int $groupID
188 * @param int $contactID
189 * The contactID for whom the check is made.
190 * @param string $tableName
191 * @param null $allGroups
192 * @param null $includedGroups
196 * the ids of the groups for which the user has permissions
198 public static function groupPermission(
202 $tableName = 'civicrm_saved_search',
204 $includedGroups = NULL,
208 static $cache = array();
210 //@todo this is pretty hacky!!!
211 //adding a way for unit tests to flush the cache
217 $session = CRM_Core_Session
::singleton();
219 if ($session->get('userID')) {
220 $contactID = $session->get('userID');
224 $key = "{$tableName}_{$type}_{$contactID}";
225 if (array_key_exists($key, $cache)) {
226 $groups = &$cache[$key];
229 $groups = self
::group($type, $contactID, $tableName, $allGroups, $includedGroups);
230 $cache[$key] = $groups;
232 if (empty($groups)) {
236 return in_array($groupID, $groups) ?
TRUE : FALSE;