-
-
Notifications
You must be signed in to change notification settings - Fork 219
/
AssetTagConditionGenerator.php
70 lines (63 loc) · 2.92 KB
/
AssetTagConditionGenerator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
namespace Neos\Media\Security\Authorization\Privilege\Doctrine;
/*
* This file is part of the Neos.Media package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
use Doctrine\Persistence\Mapping\ClassMetadata;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Query\Filter\SQLFilter as DoctrineSqlFilter;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Security\Authorization\Privilege\Entity\Doctrine\PropertyConditionGenerator;
use Neos\Flow\Security\Authorization\Privilege\Entity\Doctrine\SqlGeneratorInterface;
use Neos\Flow\Validation\Validator\UuidValidator;
/**
* Condition generator covering Asset <-> Tag relations (M:M relations are not supported by the Flow
* PropertyConditionGenerator yet)
*/
class AssetTagConditionGenerator implements SqlGeneratorInterface
{
/**
* @Flow\Inject
* @var EntityManagerInterface
*/
protected $entityManager;
/**
* @var string
*/
protected $tagLabelOrIdentifier;
/**
* @param string $tagLabelOrIdentifier
*/
public function __construct($tagLabelOrIdentifier)
{
$this->tagLabelOrIdentifier = $tagLabelOrIdentifier;
}
/**
* @param DoctrineSqlFilter $sqlFilter
* @param ClassMetadata $targetEntity Metadata object for the target entity to create the constraint for
* @param string $targetTableAlias The target table alias used in the current query
* @return string
*/
public function getSql(DoctrineSqlFilter $sqlFilter, ClassMetadata $targetEntity, $targetTableAlias)
{
$propertyConditionGenerator = new PropertyConditionGenerator('');
$tagLabelOrIdentifier = $propertyConditionGenerator->getValueForOperand($this->tagLabelOrIdentifier);
if (preg_match(UuidValidator::PATTERN_MATCH_UUID, $tagLabelOrIdentifier) === 1) {
$whereCondition = $targetTableAlias . '_t.persistence_object_identifier = ' . $this->entityManager->getConnection()->quote($tagLabelOrIdentifier);
} else {
$whereCondition = $targetTableAlias . '_t.label = ' . $this->entityManager->getConnection()->quote($tagLabelOrIdentifier);
}
return $targetTableAlias . '.persistence_object_identifier IN (
SELECT ' . $targetTableAlias . '_a.persistence_object_identifier
FROM neos_media_domain_model_asset AS ' . $targetTableAlias . '_a
LEFT JOIN neos_media_domain_model_asset_tags_join ' . $targetTableAlias . '_atj ON ' . $targetTableAlias . '_a.persistence_object_identifier = ' . $targetTableAlias . '_atj.media_asset
LEFT JOIN neos_media_domain_model_tag ' . $targetTableAlias . '_t ON ' . $targetTableAlias . '_t.persistence_object_identifier = ' . $targetTableAlias . '_atj.media_tag
WHERE ' . $whereCondition . ')';
}
}