trait CRM_Core_DynamicFKAccessTrait {
/**
+ * @param string $entityName
* @param string $action
* @param array $record
- * @param int|NULL $userID
+ * @param int $userID
* @return bool
* @see CRM_Core_DAO::checkAccess
*/
- public static function _checkAccess(string $action, array $record, $userID): bool {
+ public static function _checkAccess(string $entityName, string $action, array $record, int $userID): bool {
$eid = $record['entity_id'] ?? NULL;
$table = $record['entity_table'] ?? NULL;
if (!$eid && !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);
+ $targetEntity = CRM_Core_DAO_AllCoreTables::getEntityNameForTable($table);
+ if ($targetEntity === NULL) {
+ throw new \API_Exception(sprintf('Cannot resolve permissions for dynamic foreign key in "%s". Invalid table reference "%s".',
+ static::getTableName(), $table));
+ }
+ return \Civi\Api4\Utils\CoreUtil::checkAccessDelegated($targetEntity, 'update', ['id' => $eid], $userID);
}
return TRUE;
}