Merge pull request #8415 from monishdeb/CRM-18159
[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 $session = CRM_Core_Session::singleton();
61 $contactID = $session->get('userID');
62 }
63
64 if (!$contactID) {
65 // anonymous user
66 $contactID = 0;
67 }
68
69 return CRM_ACL_BAO_ACL::check($str, $contactID);
70 }
71
72 /**
73 * Get the permissioned where clause for the user.
74 *
75 * @param int $type
76 * The type of permission needed.
77 * @param array $tables
78 * (reference ) add the tables that are needed for the select clause.
79 * @param array $whereTables
80 * (reference ) add the tables that are needed for the where clause.
81 * @param int $contactID
82 * The contactID for whom the check is made.
83 * @param bool $onlyDeleted
84 * Whether to include only deleted contacts.
85 * @param bool $skipDeleteClause
86 * Don't add delete clause if this is true,.
87 * this means it is handled by generating query
88 *
89 * @return string
90 * the group where clause for this user
91 */
92 public static function whereClause(
93 $type,
94 &$tables,
95 &$whereTables,
96 $contactID = NULL,
97 $onlyDeleted = FALSE,
98 $skipDeleteClause = FALSE
99 ) {
100 // the default value which is valid for the final AND
101 $deleteClause = ' ( 1 ) ';
102 if (!$skipDeleteClause) {
103 if (CRM_Core_Permission::check('access deleted contacts') and $onlyDeleted) {
104 $deleteClause = '(contact_a.is_deleted)';
105 }
106 else {
107 // CRM-6181
108 $deleteClause = '(contact_a.is_deleted = 0)';
109 }
110 }
111
112 // first see if the contact has edit / view all contacts
113 if (CRM_Core_Permission::check('edit all contacts') ||
114 ($type == self::VIEW && CRM_Core_Permission::check('view all contacts'))
115 ) {
116 return $deleteClause;
117 }
118
119 if (!$contactID) {
120 $contactID = CRM_Core_Session::getLoggedInContactID();
121 }
122 $contactID = (int) $contactID;
123
124 $where = implode(' AND ',
125 array(
126 CRM_ACL_BAO_ACL::whereClause($type,
127 $tables,
128 $whereTables,
129 $contactID
130 ),
131 $deleteClause,
132 )
133 );
134
135 // Add permission on self
136 if ($contactID && (CRM_Core_Permission::check('edit my contact') ||
137 $type == self::VIEW && CRM_Core_Permission::check('view my contact'))
138 ) {
139 $where = "(contact_a.id = $contactID OR ($where))";
140 }
141 return $where;
142 }
143
144 /**
145 * Get all the groups the user has access to for the given operation.
146 *
147 * @param int $type
148 * The type of permission needed.
149 * @param int $contactID
150 * The contactID for whom the check is made.
151 *
152 * @param string $tableName
153 * @param null $allGroups
154 * @param null $includedGroups
155 *
156 * @return array
157 * the ids of the groups for which the user has permissions
158 */
159 public static function group(
160 $type,
161 $contactID = NULL,
162 $tableName = 'civicrm_saved_search',
163 $allGroups = NULL,
164 $includedGroups = NULL
165 ) {
166 if ($contactID == NULL) {
167 $session = CRM_Core_Session::singleton();
168 $contactID = $session->get('userID');
169 }
170
171 if (!$contactID) {
172 // anonymous user
173 $contactID = 0;
174 }
175
176 return CRM_ACL_BAO_ACL::group($type, $contactID, $tableName, $allGroups, $includedGroups);
177 }
178
179 /**
180 * Check if the user has access to this group for operation $type
181 *
182 * @param int $type
183 * The type of permission needed.
184 * @param int $groupID
185 * @param int $contactID
186 * The contactID for whom the check is made.
187 * @param string $tableName
188 * @param null $allGroups
189 * @param null $includedGroups
190 * @param bool $flush
191 *
192 * @return array
193 * the ids of the groups for which the user has permissions
194 */
195 public static function groupPermission(
196 $type,
197 $groupID,
198 $contactID = NULL,
199 $tableName = 'civicrm_saved_search',
200 $allGroups = NULL,
201 $includedGroups = NULL,
202 $flush = FALSE
203 ) {
204
205 static $cache = array();
206 $groups = array();
207 //@todo this is pretty hacky!!!
208 //adding a way for unit tests to flush the cache
209 if ($flush) {
210 $cache = array();
211 return NULL;
212 }
213 if (!$contactID) {
214 $session = CRM_Core_Session::singleton();
215 $contactID = NULL;
216 if ($session->get('userID')) {
217 $contactID = $session->get('userID');
218 }
219 }
220
221 $key = "{$tableName}_{$type}_{$contactID}";
222 if (array_key_exists($key, $cache)) {
223 $groups = &$cache[$key];
224 }
225 else {
226 $groups = self::group($type, $contactID, $tableName, $allGroups, $includedGroups);
227 $cache[$key] = $groups;
228 }
229 if (empty($groups)) {
230 return FALSE;
231 }
232
233 return in_array($groupID, $groups) ? TRUE : FALSE;
234 }
235
236 }