Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
6a488035 TO |
32 | */ |
33 | class CRM_ACL_API { | |
34 | ||
35 | /** | |
d2e5d2ce | 36 | * The various type of permissions. |
6a488035 TO |
37 | * |
38 | * @var int | |
39 | */ | |
7da04cde TO |
40 | const EDIT = 1; |
41 | const VIEW = 2; | |
42 | const DELETE = 3; | |
43 | const CREATE = 4; | |
44 | const SEARCH = 5; | |
45 | const ALL = 6; | |
6a488035 TO |
46 | |
47 | /** | |
100fef9d | 48 | * Given a permission string, check for access requirements |
6a488035 | 49 | * |
b758c7d5 TO |
50 | * @param string $str |
51 | * The permission to check. | |
52 | * @param int $contactID | |
53 | * The contactID for whom the check is made. | |
6a488035 | 54 | * |
acb1052e | 55 | * @return bool |
a6c01b45 | 56 | * true if yes, else false |
6a488035 | 57 | */ |
00be9182 | 58 | public static function check($str, $contactID = NULL) { |
6a488035 TO |
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 | /** | |
d2e5d2ce | 73 | * Get the permissioned where clause for the user. |
6a488035 | 74 | * |
b758c7d5 TO |
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,. | |
a1258782 | 87 | * this means it is handled by generating query |
6a488035 | 88 | * |
a6c01b45 CW |
89 | * @return string |
90 | * the group where clause for this user | |
6a488035 | 91 | */ |
e6a83034 TO |
92 | public static function whereClause( |
93 | $type, | |
6a488035 TO |
94 | &$tables, |
95 | &$whereTables, | |
100b0ec6 TO |
96 | $contactID = NULL, |
97 | $onlyDeleted = FALSE, | |
6a488035 TO |
98 | $skipDeleteClause = FALSE |
99 | ) { | |
5bd6e0a3 | 100 | // the default value which is valid for the final AND |
6a488035 TO |
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') || | |
5bd6e0a3 | 114 | ($type == self::VIEW && CRM_Core_Permission::check('view all contacts')) |
6a488035 | 115 | ) { |
5bd6e0a3 | 116 | return $deleteClause; |
6a488035 TO |
117 | } |
118 | ||
1a4651ba CW |
119 | if (!$contactID) { |
120 | $contactID = CRM_Core_Session::getLoggedInContactID(); | |
6a488035 | 121 | } |
1a4651ba | 122 | $contactID = (int) $contactID; |
6a488035 | 123 | |
1a4651ba | 124 | $where = implode(' AND ', |
6a488035 TO |
125 | array( |
126 | CRM_ACL_BAO_ACL::whereClause($type, | |
127 | $tables, | |
128 | $whereTables, | |
129 | $contactID | |
130 | ), | |
131 | $deleteClause, | |
132 | ) | |
133 | ); | |
1a4651ba CW |
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 | ) { | |
f8d66365 | 139 | $where = "(contact_a.id = $contactID OR ($where))"; |
1a4651ba CW |
140 | } |
141 | return $where; | |
6a488035 TO |
142 | } |
143 | ||
144 | /** | |
d2e5d2ce | 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 | 155 | * |
a6c01b45 CW |
156 | * @return array |
157 | * the ids of the groups for which the user has permissions | |
6a488035 TO |
158 | */ |
159 | public static function group( | |
160 | $type, | |
100b0ec6 TO |
161 | $contactID = NULL, |
162 | $tableName = 'civicrm_saved_search', | |
163 | $allGroups = NULL, | |
6a488035 TO |
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 | /** | |
100fef9d | 180 | * Check if the user has access to this group for operation $type |
6a488035 | 181 | * |
b758c7d5 TO |
182 | * @param int $type |
183 | * The type of permission needed. | |
100fef9d | 184 | * @param int $groupID |
b758c7d5 TO |
185 | * @param int $contactID |
186 | * The contactID for whom the check is made. | |
da6b46f4 EM |
187 | * @param string $tableName |
188 | * @param null $allGroups | |
189 | * @param null $includedGroups | |
190 | * @param bool $flush | |
6a488035 | 191 | * |
a6c01b45 CW |
192 | * @return array |
193 | * the ids of the groups for which the user has permissions | |
6a488035 TO |
194 | */ |
195 | public static function groupPermission( | |
196 | $type, | |
197 | $groupID, | |
100b0ec6 TO |
198 | $contactID = NULL, |
199 | $tableName = 'civicrm_saved_search', | |
200 | $allGroups = NULL, | |
90dee8d1 EM |
201 | $includedGroups = NULL, |
202 | $flush = FALSE | |
6a488035 | 203 | ) { |
6a488035 | 204 | |
90dee8d1 | 205 | static $cache = array(); |
b92e5777 | 206 | $groups = array(); |
90dee8d1 EM |
207 | //@todo this is pretty hacky!!! |
208 | //adding a way for unit tests to flush the cache | |
209 | if ($flush) { | |
210 | $cache = array(); | |
acb1052e | 211 | return NULL; |
90dee8d1 | 212 | } |
6a488035 TO |
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 | } | |
e7d6f8f8 SL |
229 | if (empty($groups)) { |
230 | return FALSE; | |
231 | } | |
6a488035 TO |
232 | |
233 | return in_array($groupID, $groups) ? TRUE : FALSE; | |
234 | } | |
96025800 | 235 | |
6a488035 | 236 | } |