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