commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-new / civicrm / CRM / ACL / API.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
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
53 * The permission to check.
54 * @param int $contactID
55 * The contactID for whom the check is made.
56 *
57 * @return bool
58 * true if yes, else false
59 */
60 public static function check($str, $contactID = NULL) {
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 *
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,.
89 * this means it is handled by generating query
90 *
91 * @return string
92 * the group where clause for this user
93 */
94 public static function whereClause(
95 $type,
96 &$tables,
97 &$whereTables,
98 $contactID = NULL,
99 $onlyDeleted = FALSE,
100 $skipDeleteClause = FALSE
101 ) {
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)';
107 }
108 else {
109 // CRM-6181
110 $deleteClause = '(contact_a.is_deleted = 0)';
111 }
112 }
113
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'))
117 ) {
118 return $deleteClause;
119 }
120
121 if (!$contactID) {
122 $contactID = CRM_Core_Session::getLoggedInContactID();
123 }
124 $contactID = (int) $contactID;
125
126 $where = implode(' AND ',
127 array(
128 CRM_ACL_BAO_ACL::whereClause($type,
129 $tables,
130 $whereTables,
131 $contactID
132 ),
133 $deleteClause,
134 )
135 );
136
137 // Add permission on self
138 if ($contactID && (CRM_Core_Permission::check('edit my contact') ||
139 $type == self::VIEW && CRM_Core_Permission::check('view my contact'))
140 ) {
141 $where = "(contact_a.id = $contactID OR ($where))";
142 }
143 return $where;
144 }
145
146 /**
147 * Get all the groups the user has access to for the given operation.
148 *
149 * @param int $type
150 * The type of permission needed.
151 * @param int $contactID
152 * The contactID for whom the check is made.
153 *
154 * @param string $tableName
155 * @param null $allGroups
156 * @param null $includedGroups
157 *
158 * @return array
159 * the ids of the groups for which the user has permissions
160 */
161 public static function group(
162 $type,
163 $contactID = NULL,
164 $tableName = 'civicrm_saved_search',
165 $allGroups = NULL,
166 $includedGroups = NULL
167 ) {
168 if ($contactID == NULL) {
169 $session = CRM_Core_Session::singleton();
170 $contactID = $session->get('userID');
171 }
172
173 if (!$contactID) {
174 // anonymous user
175 $contactID = 0;
176 }
177
178 return CRM_ACL_BAO_ACL::group($type, $contactID, $tableName, $allGroups, $includedGroups);
179 }
180
181 /**
182 * Check if the user has access to this group for operation $type
183 *
184 * @param int $type
185 * The type of permission needed.
186 * @param int $groupID
187 * @param int $contactID
188 * The contactID for whom the check is made.
189 * @param string $tableName
190 * @param null $allGroups
191 * @param null $includedGroups
192 * @param bool $flush
193 *
194 * @return array
195 * the ids of the groups for which the user has permissions
196 */
197 public static function groupPermission(
198 $type,
199 $groupID,
200 $contactID = NULL,
201 $tableName = 'civicrm_saved_search',
202 $allGroups = NULL,
203 $includedGroups = NULL,
204 $flush = FALSE
205 ) {
206
207 static $cache = array();
208 $groups = array();
209 //@todo this is pretty hacky!!!
210 //adding a way for unit tests to flush the cache
211 if ($flush) {
212 $cache = array();
213 return NULL;
214 }
215 if (!$contactID) {
216 $session = CRM_Core_Session::singleton();
217 $contactID = NULL;
218 if ($session->get('userID')) {
219 $contactID = $session->get('userID');
220 }
221 }
222
223 $key = "{$tableName}_{$type}_{$contactID}";
224 if (array_key_exists($key, $cache)) {
225 $groups = &$cache[$key];
226 }
227 else {
228 $groups = self::group($type, $contactID, $tableName, $allGroups, $includedGroups);
229 $cache[$key] = $groups;
230 }
231 if (empty($groups)) {
232 return FALSE;
233 }
234
235 return in_array($groupID, $groups) ? TRUE : FALSE;
236 }
237
238 }