Support $contactId param in permission checks
[civicrm-core.git] / CRM / ACL / API.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
8c9251b3 6 | Copyright CiviCRM LLC (c) 2004-2018 |
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
8c9251b3 31 * @copyright CiviCRM LLC (c) 2004-2018
6a488035
TO
32 */
33class 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 59 if ($contactID == NULL) {
3bdcd4ec 60 $contactID = CRM_Core_Session::getLoggedInContactID();
6a488035
TO
61 }
62
63 if (!$contactID) {
64 // anonymous user
65 $contactID = 0;
66 }
67
68 return CRM_ACL_BAO_ACL::check($str, $contactID);
69 }
70
71 /**
d2e5d2ce 72 * Get the permissioned where clause for the user.
6a488035 73 *
b758c7d5
TO
74 * @param int $type
75 * The type of permission needed.
76 * @param array $tables
77 * (reference ) add the tables that are needed for the select clause.
78 * @param array $whereTables
79 * (reference ) add the tables that are needed for the where clause.
80 * @param int $contactID
81 * The contactID for whom the check is made.
82 * @param bool $onlyDeleted
83 * Whether to include only deleted contacts.
84 * @param bool $skipDeleteClause
85 * Don't add delete clause if this is true,.
a1258782 86 * this means it is handled by generating query
9aea8e14 87 * @param bool $skipOwnContactClause
88 * Do not add 'OR contact_id = $userID' to the where clause.
89 * This is a hideously inefficient query and should be avoided
90 * wherever possible.
6a488035 91 *
a6c01b45
CW
92 * @return string
93 * the group where clause for this user
6a488035 94 */
e6a83034
TO
95 public static function whereClause(
96 $type,
6a488035
TO
97 &$tables,
98 &$whereTables,
100b0ec6
TO
99 $contactID = NULL,
100 $onlyDeleted = FALSE,
9aea8e14 101 $skipDeleteClause = FALSE,
102 $skipOwnContactClause = FALSE
6a488035 103 ) {
5bd6e0a3 104 // the default value which is valid for the final AND
6a488035
TO
105 $deleteClause = ' ( 1 ) ';
106 if (!$skipDeleteClause) {
107 if (CRM_Core_Permission::check('access deleted contacts') and $onlyDeleted) {
108 $deleteClause = '(contact_a.is_deleted)';
109 }
110 else {
111 // CRM-6181
112 $deleteClause = '(contact_a.is_deleted = 0)';
113 }
114 }
115
116 // first see if the contact has edit / view all contacts
117 if (CRM_Core_Permission::check('edit all contacts') ||
5bd6e0a3 118 ($type == self::VIEW && CRM_Core_Permission::check('view all contacts'))
6a488035 119 ) {
5bd6e0a3 120 return $deleteClause;
6a488035
TO
121 }
122
1a4651ba
CW
123 if (!$contactID) {
124 $contactID = CRM_Core_Session::getLoggedInContactID();
6a488035 125 }
1a4651ba 126 $contactID = (int) $contactID;
6a488035 127
1a4651ba 128 $where = implode(' AND ',
6a488035
TO
129 array(
130 CRM_ACL_BAO_ACL::whereClause($type,
131 $tables,
132 $whereTables,
133 $contactID
134 ),
135 $deleteClause,
136 )
137 );
1a4651ba 138
9aea8e14 139 // Add permission on self if we really hate our server or have hardly any contacts.
140 if (!$skipOwnContactClause && $contactID && (CRM_Core_Permission::check('edit my contact') ||
141 $type == self::VIEW && CRM_Core_Permission::check('view my contact'))
1a4651ba 142 ) {
f8d66365 143 $where = "(contact_a.id = $contactID OR ($where))";
1a4651ba
CW
144 }
145 return $where;
6a488035
TO
146 }
147
148 /**
d2e5d2ce 149 * Get all the groups the user has access to for the given operation.
6a488035 150 *
b758c7d5
TO
151 * @param int $type
152 * The type of permission needed.
153 * @param int $contactID
154 * The contactID for whom the check is made.
fd31fa4c
EM
155 *
156 * @param string $tableName
157 * @param null $allGroups
158 * @param null $includedGroups
6a488035 159 *
a6c01b45
CW
160 * @return array
161 * the ids of the groups for which the user has permissions
6a488035
TO
162 */
163 public static function group(
164 $type,
100b0ec6
TO
165 $contactID = NULL,
166 $tableName = 'civicrm_saved_search',
167 $allGroups = NULL,
6a488035
TO
168 $includedGroups = NULL
169 ) {
170 if ($contactID == NULL) {
3bdcd4ec 171 $contactID = CRM_Core_Session::getLoggedInContactID();
6a488035
TO
172 }
173
174 if (!$contactID) {
175 // anonymous user
176 $contactID = 0;
177 }
178
179 return CRM_ACL_BAO_ACL::group($type, $contactID, $tableName, $allGroups, $includedGroups);
180 }
181
182 /**
100fef9d 183 * Check if the user has access to this group for operation $type
6a488035 184 *
b758c7d5
TO
185 * @param int $type
186 * The type of permission needed.
100fef9d 187 * @param int $groupID
b758c7d5
TO
188 * @param int $contactID
189 * The contactID for whom the check is made.
da6b46f4
EM
190 * @param string $tableName
191 * @param null $allGroups
192 * @param null $includedGroups
6a488035 193 *
6d054a8e 194 * @return bool
6a488035
TO
195 */
196 public static function groupPermission(
197 $type,
198 $groupID,
100b0ec6
TO
199 $contactID = NULL,
200 $tableName = 'civicrm_saved_search',
201 $allGroups = NULL,
6d054a8e 202 $includedGroups = NULL
6a488035 203 ) {
6a488035 204
6d054a8e 205 if (!isset(Civi::$statics[__CLASS__]) || !isset(Civi::$statics[__CLASS__]['group_permission'])) {
206 Civi::$statics[__CLASS__]['group_permission'] = array();
90dee8d1 207 }
6d054a8e 208
6a488035 209 if (!$contactID) {
6d054a8e 210 $contactID = CRM_Core_Session::singleton()->getLoggedInContactID();
6a488035
TO
211 }
212
213 $key = "{$tableName}_{$type}_{$contactID}";
6d054a8e 214 if (!array_key_exists($key, Civi::$statics[__CLASS__]['group_permission'])) {
215 Civi::$statics[__CLASS__]['group_permission'][$key] = self::group($type, $contactID, $tableName, $allGroups, $includedGroups);
e7d6f8f8 216 }
6a488035 217
6d054a8e 218 return in_array($groupID, Civi::$statics[__CLASS__]['group_permission'][$key]);
6a488035 219 }
96025800 220
6a488035 221}