Merge pull request #4892 from colemanw/INFRA-132
[civicrm-core.git] / CRM / ACL / API.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
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 and the CiviCRM Licensing Exception. |
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 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 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35class CRM_ACL_API {
36
37 /**
38 * The various type of permissions
39 *
40 * @var int
41 */
7da04cde
TO
42 const EDIT = 1;
43 const VIEW = 2;
44 const DELETE = 3;
45 const CREATE = 4;
46 const SEARCH = 5;
47 const ALL = 6;
6a488035
TO
48
49 /**
100fef9d 50 * Given a permission string, check for access requirements
6a488035 51 *
b758c7d5
TO
52 * @param string $str
53 * The permission to check.
54 * @param int $contactID
55 * The contactID for whom the check is made.
6a488035
TO
56 *
57 * @return boolean true if yes, else false
58 * @static
6a488035 59 */
00be9182 60 public static function check($str, $contactID = NULL) {
6a488035
TO
61 if ($contactID == NULL) {
62 $session = CRM_Core_Session::singleton();
63 $contactID = $session->get('userID');
64 }
65
66 if (!$contactID) {
67 // anonymous user
68 $contactID = 0;
69 }
70
71 return CRM_ACL_BAO_ACL::check($str, $contactID);
72 }
73
74 /**
75 * Get the permissioned where clause for the user
76 *
b758c7d5
TO
77 * @param int $type
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,.
6a488035
TO
89 * this means it is handled by generating query
90 *
91 * @return string the group where clause for this user
6a488035 92 */
e6a83034
TO
93 public static function whereClause(
94 $type,
6a488035
TO
95 &$tables,
96 &$whereTables,
100b0ec6
TO
97 $contactID = NULL,
98 $onlyDeleted = FALSE,
6a488035
TO
99 $skipDeleteClause = FALSE
100 ) {
101 // the default value which is valid for rhe final AND
102 $deleteClause = ' ( 1 ) ';
103 if (!$skipDeleteClause) {
104 if (CRM_Core_Permission::check('access deleted contacts') and $onlyDeleted) {
105 $deleteClause = '(contact_a.is_deleted)';
106 }
107 else {
108 // CRM-6181
109 $deleteClause = '(contact_a.is_deleted = 0)';
110 }
111 }
112
113 // first see if the contact has edit / view all contacts
114 if (CRM_Core_Permission::check('edit all contacts') ||
115 ($type == self::VIEW &&
116 CRM_Core_Permission::check('view all contacts')
117 )
118 ) {
119 return $skipDeleteClause ? ' ( 1 ) ' : $deleteClause;
120 }
121
122 if ($contactID == NULL) {
123 $session = CRM_Core_Session::singleton();
124 $contactID = $session->get('userID');
125 }
126
127 if (!$contactID) {
128 // anonymous user
129 $contactID = 0;
130 }
131
132 return implode(' AND ',
133 array(
134 CRM_ACL_BAO_ACL::whereClause($type,
135 $tables,
136 $whereTables,
137 $contactID
138 ),
139 $deleteClause,
140 )
141 );
142 }
143
144 /**
100fef9d 145 * Get all the groups the user has access to for the given operation
6a488035 146 *
b758c7d5
TO
147 * @param int $type
148 * The type of permission needed.
149 * @param int $contactID
150 * The contactID for whom the check is made.
fd31fa4c
EM
151 *
152 * @param string $tableName
153 * @param null $allGroups
154 * @param null $includedGroups
6a488035
TO
155 *
156 * @return array the ids of the groups for which the user has permissions
6a488035
TO
157 */
158 public static function group(
159 $type,
100b0ec6
TO
160 $contactID = NULL,
161 $tableName = 'civicrm_saved_search',
162 $allGroups = NULL,
6a488035
TO
163 $includedGroups = NULL
164 ) {
165 if ($contactID == NULL) {
166 $session = CRM_Core_Session::singleton();
167 $contactID = $session->get('userID');
168 }
169
170 if (!$contactID) {
171 // anonymous user
172 $contactID = 0;
173 }
174
175 return CRM_ACL_BAO_ACL::group($type, $contactID, $tableName, $allGroups, $includedGroups);
176 }
177
178 /**
100fef9d 179 * Check if the user has access to this group for operation $type
6a488035 180 *
b758c7d5
TO
181 * @param int $type
182 * The type of permission needed.
100fef9d 183 * @param int $groupID
b758c7d5
TO
184 * @param int $contactID
185 * The contactID for whom the check is made.
da6b46f4
EM
186 *
187 * @param string $tableName
188 * @param null $allGroups
189 * @param null $includedGroups
190 * @param bool $flush
6a488035
TO
191 *
192 * @return array the ids of the groups for which the user has permissions
6a488035
TO
193 */
194 public static function groupPermission(
195 $type,
196 $groupID,
100b0ec6
TO
197 $contactID = NULL,
198 $tableName = 'civicrm_saved_search',
199 $allGroups = NULL,
90dee8d1
EM
200 $includedGroups = NULL,
201 $flush = FALSE
6a488035 202 ) {
6a488035 203
90dee8d1
EM
204 static $cache = array();
205 //@todo this is pretty hacky!!!
206 //adding a way for unit tests to flush the cache
207 if ($flush) {
208 $cache = array();
209 return;
210 }
6a488035
TO
211 if (!$contactID) {
212 $session = CRM_Core_Session::singleton();
213 $contactID = NULL;
214 if ($session->get('userID')) {
215 $contactID = $session->get('userID');
216 }
217 }
218
219 $key = "{$tableName}_{$type}_{$contactID}";
220 if (array_key_exists($key, $cache)) {
221 $groups = &$cache[$key];
222 }
223 else {
224 $groups = self::group($type, $contactID, $tableName, $allGroups, $includedGroups);
225 $cache[$key] = $groups;
226 }
227
228 return in_array($groupID, $groups) ? TRUE : FALSE;
229 }
230}