Merge pull request #9258 from eileenmcnaughton/group_fix
[civicrm-core.git] / CRM / ACL / API.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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
31 * @copyright CiviCRM LLC (c) 2004-2016
32 */
33 class CRM_ACL_API {
34
35 /**
36 * The various type of permissions.
37 *
38 * @var int
39 */
40 const EDIT = 1;
41 const VIEW = 2;
42 const DELETE = 3;
43 const CREATE = 4;
44 const SEARCH = 5;
45 const ALL = 6;
46
47 /**
48 * Given a permission string, check for access requirements
49 *
50 * @param string $str
51 * The permission to check.
52 * @param int $contactID
53 * The contactID for whom the check is made.
54 *
55 * @return bool
56 * true if yes, else false
57 */
58 public static function check($str, $contactID = NULL) {
59 if ($contactID == NULL) {
60 $contactID = CRM_Core_Session::getLoggedInContactID();
61 }
62
63 if (!$contactID) {
64 // anonymous user
65 $contactID = 0;
66 }
67
68 return CRM_ACL_BAO_ACL::check($str, $contactID);
69 }
70
71 /**
72 * Get the permissioned where clause for the user.
73 *
74 * @param int $type
75 * The type of permission needed.
76 * @param array $tables
77 * (reference ) add the tables that are needed for the select clause.
78 * @param array $whereTables
79 * (reference ) add the tables that are needed for the where clause.
80 * @param int $contactID
81 * The contactID for whom the check is made.
82 * @param bool $onlyDeleted
83 * Whether to include only deleted contacts.
84 * @param bool $skipDeleteClause
85 * Don't add delete clause if this is true,.
86 * this means it is handled by generating query
87 *
88 * @return string
89 * the group where clause for this user
90 */
91 public static function whereClause(
92 $type,
93 &$tables,
94 &$whereTables,
95 $contactID = NULL,
96 $onlyDeleted = FALSE,
97 $skipDeleteClause = FALSE
98 ) {
99 // the default value which is valid for the final AND
100 $deleteClause = ' ( 1 ) ';
101 if (!$skipDeleteClause) {
102 if (CRM_Core_Permission::check('access deleted contacts') and $onlyDeleted) {
103 $deleteClause = '(contact_a.is_deleted)';
104 }
105 else {
106 // CRM-6181
107 $deleteClause = '(contact_a.is_deleted = 0)';
108 }
109 }
110
111 // first see if the contact has edit / view all contacts
112 if (CRM_Core_Permission::check('edit all contacts') ||
113 ($type == self::VIEW && CRM_Core_Permission::check('view all contacts'))
114 ) {
115 return $deleteClause;
116 }
117
118 if (!$contactID) {
119 $contactID = CRM_Core_Session::getLoggedInContactID();
120 }
121 $contactID = (int) $contactID;
122
123 $where = implode(' AND ',
124 array(
125 CRM_ACL_BAO_ACL::whereClause($type,
126 $tables,
127 $whereTables,
128 $contactID
129 ),
130 $deleteClause,
131 )
132 );
133
134 // Add permission on self
135 if ($contactID && (CRM_Core_Permission::check('edit my contact') ||
136 $type == self::VIEW && CRM_Core_Permission::check('view my contact'))
137 ) {
138 $where = "(contact_a.id = $contactID OR ($where))";
139 }
140 return $where;
141 }
142
143 /**
144 * Get all the groups the user has access to for the given operation.
145 *
146 * @param int $type
147 * The type of permission needed.
148 * @param int $contactID
149 * The contactID for whom the check is made.
150 *
151 * @param string $tableName
152 * @param null $allGroups
153 * @param null $includedGroups
154 *
155 * @return array
156 * the ids of the groups for which the user has permissions
157 */
158 public static function group(
159 $type,
160 $contactID = NULL,
161 $tableName = 'civicrm_saved_search',
162 $allGroups = NULL,
163 $includedGroups = NULL
164 ) {
165 if ($contactID == NULL) {
166 $contactID = CRM_Core_Session::getLoggedInContactID();
167 }
168
169 if (!$contactID) {
170 // anonymous user
171 $contactID = 0;
172 }
173
174 return CRM_ACL_BAO_ACL::group($type, $contactID, $tableName, $allGroups, $includedGroups);
175 }
176
177 /**
178 * Check if the user has access to this group for operation $type
179 *
180 * @param int $type
181 * The type of permission needed.
182 * @param int $groupID
183 * @param int $contactID
184 * The contactID for whom the check is made.
185 * @param string $tableName
186 * @param null $allGroups
187 * @param null $includedGroups
188 * @param bool $flush
189 *
190 * @return array
191 * the ids of the groups for which the user has permissions
192 */
193 public static function groupPermission(
194 $type,
195 $groupID,
196 $contactID = NULL,
197 $tableName = 'civicrm_saved_search',
198 $allGroups = NULL,
199 $includedGroups = NULL,
200 $flush = FALSE
201 ) {
202
203 static $cache = array();
204 $groups = 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 NULL;
210 }
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 if (empty($groups)) {
228 return FALSE;
229 }
230
231 return in_array($groupID, $groups) ? TRUE : FALSE;
232 }
233
234 }