3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
20 * The various type of permissions.
32 * Given a permission string, check for access requirements
35 * The permission to check.
36 * @param int $contactID
37 * The contactID for whom the check is made.
40 * true if yes, else false
42 public static function check($str, $contactID = NULL) {
43 if ($contactID == NULL) {
44 $contactID = CRM_Core_Session
::getLoggedInContactID();
52 return CRM_ACL_BAO_ACL
::check($str, $contactID);
56 * Get the permissioned where clause for the user.
59 * The type of permission needed.
60 * @param array $tables
61 * (reference ) add the tables that are needed for the select clause.
62 * @param array $whereTables
63 * (reference ) add the tables that are needed for the where clause.
64 * @param int $contactID
65 * The contactID for whom the check is made.
66 * @param bool $onlyDeleted
67 * Whether to include only deleted contacts.
68 * @param bool $skipDeleteClause
69 * Don't add delete clause if this is true,.
70 * this means it is handled by generating query
71 * @param bool $skipOwnContactClause
72 * Do not add 'OR contact_id = $userID' to the where clause.
73 * This is a hideously inefficient query and should be avoided
77 * the group where clause for this user
79 public static function whereClause(
85 $skipDeleteClause = FALSE,
86 $skipOwnContactClause = FALSE
88 // the default value which is valid for the final AND
89 $deleteClause = ' ( 1 ) ';
90 if (!$skipDeleteClause) {
91 if (CRM_Core_Permission
::check('access deleted contacts') and $onlyDeleted) {
92 $deleteClause = '(contact_a.is_deleted)';
96 $deleteClause = '(contact_a.is_deleted = 0)';
101 $contactID = CRM_Core_Session
::getLoggedInContactID();
103 $contactID = (int) $contactID;
105 // first see if the contact has edit / view all permission
106 if (CRM_Core_Permission
::check('edit all contacts', $contactID) ||
107 ($type == self
::VIEW
&& CRM_Core_Permission
::check('view all contacts', $contactID))
109 return $deleteClause;
112 $whereClause = CRM_ACL_BAO_ACL
::whereClause($type,
117 $where = implode(' AND ', [$whereClause, $deleteClause]);
119 // Add permission on self if we really hate our server or have hardly any contacts.
120 if (!$skipOwnContactClause && $contactID && (CRM_Core_Permission
::check('edit my contact') ||
121 $type == self
::VIEW
&& CRM_Core_Permission
::check('view my contact'))
123 $where = "(contact_a.id = $contactID OR ($where))";
129 * Get all the groups the user has access to for the given operation.
132 * The type of permission needed.
133 * @param int $contactID
134 * The contactID for whom the check is made.
136 * @param string $tableName
137 * @param null $allGroups
138 * @param null $includedGroups
141 * the ids of the groups for which the user has permissions
143 public static function group(
146 $tableName = 'civicrm_saved_search',
148 $includedGroups = NULL
150 if ($contactID == NULL) {
151 $contactID = CRM_Core_Session
::getLoggedInContactID();
159 return CRM_ACL_BAO_ACL
::group($type, $contactID, $tableName, $allGroups, $includedGroups);
163 * Check if the user has access to this group for operation $type
166 * The type of permission needed.
167 * @param int $groupID
168 * @param int $contactID
169 * The contactID for whom the check is made.
170 * @param string $tableName
171 * @param null $allGroups
172 * @param null $includedGroups
176 public static function groupPermission(
180 $tableName = 'civicrm_saved_search',
182 $includedGroups = NULL
185 if (!isset(Civi
::$statics[__CLASS__
]) ||
!isset(Civi
::$statics[__CLASS__
]['group_permission'])) {
186 Civi
::$statics[__CLASS__
]['group_permission'] = [];
190 $contactID = CRM_Core_Session
::singleton()->getLoggedInContactID();
193 $key = "{$tableName}_{$type}_{$contactID}";
194 if (!array_key_exists($key, Civi
::$statics[__CLASS__
]['group_permission'])) {
195 Civi
::$statics[__CLASS__
]['group_permission'][$key] = self
::group($type, $contactID, $tableName, $allGroups, $includedGroups);
198 return in_array($groupID, Civi
::$statics[__CLASS__
]['group_permission'][$key]);