Implement _checkAccess for Contact BAO and related entities (email, phone, etc.)
[civicrm-core.git] / CRM / Contact / AccessTrait.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * Trait shared with entities attached to the contact record.
20 */
21 trait CRM_Contact_AccessTrait {
22
23 /**
24 * @param string $action
25 * @param array $record
26 * @param int|NULL $userID
27 * @return bool
28 * @see CRM_Core_DAO::checkAccess
29 */
30 public static function _checkAccess(string $action, array $record, $userID) {
31 $cid = $record['contact_id'] ?? NULL;
32 if (!$cid && !empty($record['id'])) {
33 $cid = CRM_Core_DAO::getFieldValue(__CLASS__, $record['id'], 'contact_id');
34 }
35 if (!$cid) {
36 // With no contact id this must be part of an event locblock
37 return in_array(__CLASS__, ['CRM_Core_BAO_Phone', 'CRM_Core_BAO_Email', 'CRM_Core_BAO_Address']) &&
38 CRM_Core_Permission::check('edit all events', $userID);
39 }
40 return CRM_Contact_BAO_Contact::checkAccess($action, ['id' => $cid], $userID);
41 }
42
43 }