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 \CRM_Core_Error
::deprecatedWarning(__CLASS__
. '::' . __FUNCTION__
. ' is deprecated.');
44 if ($contactID == NULL) {
45 $contactID = CRM_Core_Session
::getLoggedInContactID();
53 return CRM_ACL_BAO_ACL
::check($str, $contactID);
57 * Get the permissioned where clause for the user.
60 * The type of permission needed.
61 * @param array $tables
62 * (reference ) add the tables that are needed for the select clause.
63 * @param array $whereTables
64 * (reference ) add the tables that are needed for the where clause.
65 * @param int $contactID
66 * The contactID for whom the check is made.
67 * @param bool $onlyDeleted
68 * Whether to include only deleted contacts.
69 * @param bool $skipDeleteClause
70 * Don't add delete clause if this is true,.
71 * this means it is handled by generating query
72 * @param bool $skipOwnContactClause
73 * Do not add 'OR contact_id = $userID' to the where clause.
74 * This is a hideously inefficient query and should be avoided
78 * the group where clause for this user
80 public static function whereClause(
86 $skipDeleteClause = FALSE,
87 $skipOwnContactClause = FALSE
89 // the default value which is valid for the final AND
90 $deleteClause = ' ( 1 ) ';
91 if (!$skipDeleteClause) {
92 if (CRM_Core_Permission
::check('access deleted contacts')) {
94 $deleteClause = '(contact_a.is_deleted)';
98 // Exclude deleted contacts due to permissions
99 $deleteClause = '(contact_a.is_deleted = 0)';
104 $contactID = CRM_Core_Session
::getLoggedInContactID();
106 $contactID = (int) $contactID;
108 // first see if the contact has edit / view all permission
109 if (CRM_Core_Permission
::check('edit all contacts', $contactID) ||
110 ($type == self
::VIEW
&& CRM_Core_Permission
::check('view all contacts', $contactID))
112 return $deleteClause;
115 $whereClause = CRM_ACL_BAO_ACL
::whereClause($type,
120 $where = implode(' AND ', [$whereClause, $deleteClause]);
122 // Add permission on self if we really hate our server or have hardly any contacts.
123 if (!$skipOwnContactClause && $contactID && (CRM_Core_Permission
::check('edit my contact') ||
124 $type == self
::VIEW
&& CRM_Core_Permission
::check('view my contact'))
126 $where = "(contact_a.id = $contactID OR ($where))";
132 * Get all the groups the user has access to for the given operation.
135 * The type of permission needed.
136 * @param int $contactID
137 * The contactID for whom the check is made.
139 * @param string $tableName
140 * @param null $allGroups
141 * @param null $includedGroups
144 * the ids of the groups for which the user has permissions
146 public static function group(
149 $tableName = 'civicrm_saved_search',
151 $includedGroups = NULL
153 if ($contactID == NULL) {
154 $contactID = CRM_Core_Session
::getLoggedInContactID();
162 return CRM_ACL_BAO_ACL
::group($type, $contactID, $tableName, $allGroups, $includedGroups);
166 * Check if the user has access to this group for operation $type
169 * The type of permission needed.
170 * @param int $groupID
171 * @param int $contactID
172 * The contactID for whom the check is made.
173 * @param string $tableName
174 * @param null $allGroups
175 * @param null $includedGroups
179 public static function groupPermission(
183 $tableName = 'civicrm_saved_search',
185 $includedGroups = NULL
188 if (!isset(Civi
::$statics[__CLASS__
]) ||
!isset(Civi
::$statics[__CLASS__
]['group_permission'])) {
189 Civi
::$statics[__CLASS__
]['group_permission'] = [];
193 $contactID = CRM_Core_Session
::getLoggedInContactID();
196 $key = "{$tableName}_{$type}_{$contactID}";
197 if (!array_key_exists($key, Civi
::$statics[__CLASS__
]['group_permission'])) {
198 Civi
::$statics[__CLASS__
]['group_permission'][$key] = self
::group($type, $contactID, $tableName, $allGroups, $includedGroups);
201 return in_array($groupID, Civi
::$statics[__CLASS__
]['group_permission'][$key]);