more comment fixes
[civicrm-core.git] / CRM / ACL / API.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
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 */
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 $user = CRM_Core_Session::getLoggedInContactID();
120 if ($contactID == NULL) {
121 $contactID = $user ? $user : 0;
122 }
123
124 // Check if contact has permissions on self
125 if ($user && $contactID == $user) {
126 if (CRM_Core_Permission::check('edit my contact') ||
127 ($type == self::VIEW && CRM_Core_Permission::check('view my contact'))
128 ) {
129 return ' ( 1 ) ';
130 }
131 }
132
133 return implode(' AND ',
134 array(
135 CRM_ACL_BAO_ACL::whereClause($type,
136 $tables,
137 $whereTables,
138 $contactID
139 ),
140 $deleteClause,
141 )
142 );
143 }
144
145 /**
146 * Get all the groups the user has access to for the given operation.
147 *
148 * @param int $type
149 * The type of permission needed.
150 * @param int $contactID
151 * The contactID for whom the check is made.
152 *
153 * @param string $tableName
154 * @param null $allGroups
155 * @param null $includedGroups
156 *
157 * @return array
158 * the ids of the groups for which the user has permissions
159 */
160 public static function group(
161 $type,
162 $contactID = NULL,
163 $tableName = 'civicrm_saved_search',
164 $allGroups = NULL,
165 $includedGroups = NULL
166 ) {
167 if ($contactID == NULL) {
168 $session = CRM_Core_Session::singleton();
169 $contactID = $session->get('userID');
170 }
171
172 if (!$contactID) {
173 // anonymous user
174 $contactID = 0;
175 }
176
177 return CRM_ACL_BAO_ACL::group($type, $contactID, $tableName, $allGroups, $includedGroups);
178 }
179
180 /**
181 * Check if the user has access to this group for operation $type
182 *
183 * @param int $type
184 * The type of permission needed.
185 * @param int $groupID
186 * @param int $contactID
187 * The contactID for whom the check is made.
188 * @param string $tableName
189 * @param null $allGroups
190 * @param null $includedGroups
191 * @param bool $flush
192 *
193 * @return array
194 * the ids of the groups for which the user has permissions
195 */
196 public static function groupPermission(
197 $type,
198 $groupID,
199 $contactID = NULL,
200 $tableName = 'civicrm_saved_search',
201 $allGroups = NULL,
202 $includedGroups = NULL,
203 $flush = FALSE
204 ) {
205
206 static $cache = array();
207 $groups = array();
208 //@todo this is pretty hacky!!!
209 //adding a way for unit tests to flush the cache
210 if ($flush) {
211 $cache = array();
212 return NULL;
213 }
214 if (!$contactID) {
215 $session = CRM_Core_Session::singleton();
216 $contactID = NULL;
217 if ($session->get('userID')) {
218 $contactID = $session->get('userID');
219 }
220 }
221
222 $key = "{$tableName}_{$type}_{$contactID}";
223 if (array_key_exists($key, $cache)) {
224 $groups = &$cache[$key];
225 }
226 else {
227 $groups = self::group($type, $contactID, $tableName, $allGroups, $includedGroups);
228 $cache[$key] = $groups;
229 }
230 if (empty($groups)) {
231 return FALSE;
232 }
233
234 return in_array($groupID, $groups) ? TRUE : FALSE;
235 }
236
237 }