Skip to content
This repository has been archived by the owner on May 16, 2024. It is now read-only.

Commit

Permalink
Merge pull request #115 from atrocore/i-649167848910911d9
Browse files Browse the repository at this point in the history
Added validation on type for Assets
  • Loading branch information
rratsun authored Aug 1, 2023
2 parents 15268ad + 325babd commit 6bbd860
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions app/Services/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,46 @@ public function createEntity($data)
return parent::createEntity($data);
}

protected function beforeCreateEntity(Entity $entity, $data)
{
$this->validateAttachment($entity, $data);
parent::beforeCreateEntity($entity, $data);
}

protected function beforeUpdateEntity(Entity $entity, $data)
{
$this->validateAttachment($entity, $data);
parent::beforeUpdateEntity($entity, $data);
}

protected function validateAttachment($entity, $data)
{
if (!empty($entity->get('fileId')) && property_exists($data, 'type')) {
$this->getInjection(AssetValidator::class)->validateViaTypes($this->getTypeValue($data->type), $this->getEntityManager()->getEntity('Attachment', $entity->get('fileId')));
}
}

public function getTypeValue($type)
{
if (empty($type)) {
return [];
}

$defs = $this->getMetadata()->get(['entityDefs', $this->entityName, 'fields', 'type'], []);
$options = [];
foreach ($type as $optionId) {
$key = array_search($optionId, $defs['optionsIds']);
if ($key === false) {
// for create or massupload
$options[] = $optionId;
} else {
// for update
$options[] = $defs['options'][$key];
}
}
return $options;
}

/**
* @param \Dam\Entities\Asset $asset
*/
Expand Down

0 comments on commit 6bbd860

Please sign in to comment.