Skip to content

Commit

Permalink
Implement _getAccess for EntityTags and Notes
Browse files Browse the repository at this point in the history
  • Loading branch information
colemanw committed May 8, 2021
1 parent f976685 commit 8c39d4a
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 4 deletions.
2 changes: 1 addition & 1 deletion CRM/Contact/AccessTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static function _checkAccess(string $action, array $record, $userID) {
return in_array(__CLASS__, ['CRM_Core_BAO_Phone', 'CRM_Core_BAO_Email', 'CRM_Core_BAO_Address']) &&
CRM_Core_Permission::check('edit all events', $userID);
}
return CRM_Contact_BAO_Contact::checkAccess($action, ['id' => $cid], $userID);
return CRM_Contact_BAO_Contact::checkAccess(CRM_Core_Permission::EDIT, ['id' => $cid], $userID);
}

}
1 change: 1 addition & 0 deletions CRM/Core/BAO/EntityTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* @copyright CiviCRM LLC https://civicrm.org/licensing
*/
class CRM_Core_BAO_EntityTag extends CRM_Core_DAO_EntityTag {
use CRM_Core_DynamicFKAccessTrait;

/**
* Given a contact id, it returns an array of tag id's the contact belongs to.
Expand Down
1 change: 1 addition & 0 deletions CRM/Core/BAO/Note.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* BAO object for crm_note table.
*/
class CRM_Core_BAO_Note extends CRM_Core_DAO_Note {
use CRM_Core_DynamicFKAccessTrait;

/**
* Const the max number of notes we display at any given time.
Expand Down
43 changes: 43 additions & 0 deletions CRM/Core/DynamicFKAccessTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/

/**
*
* @package CRM
* @copyright CiviCRM LLC https://civicrm.org/licensing
*/

/**
* Trait for with entities with an entity_table + entity_id dynamic FK.
*/
trait CRM_Core_DynamicFKAccessTrait {

/**
* @param string $action
* @param array $record
* @param $userID
*/
public static function _checkAccess(string $action, array $record, $userID) {
$eid = $record['entity_id'] ?? NULL;
$table = $record['entity_table'] ?? NULL;
if (!$eid && !empty($record['id'])) {
$eid = CRM_Core_DAO::getFieldValue(__CLASS__, $record['id'], 'entity_id');
}
if ($eid && !$table && !empty($record['id'])) {
$table = CRM_Core_DAO::getFieldValue(__CLASS__, $record['id'], 'entity_table');
}
if ($eid && $table) {
$bao = CRM_Core_DAO_AllCoreTables::getBAOClassName(CRM_Core_DAO_AllCoreTables::getClassForTable($table));
return $bao::checkAccess(CRM_Core_Permission::EDIT, ['id' => $eid], $userID);
}
}

}
3 changes: 0 additions & 3 deletions tests/phpunit/api/v3/ACLPermissionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,6 @@ public function testRelatedEntityPermissions($version) {
]);
$this->assertGreaterThan(0, $results['count']);
}
if ($version == 4) {
$this->markTestIncomplete('Skipping entity_id related perms in api4 for now.');
}
$newTag = civicrm_api3('Tag', 'create', [
'name' => 'Foo123',
]);
Expand Down

0 comments on commit 8c39d4a

Please sign in to comment.