Skip to content

Commit

Permalink
Added new ignore Filter param to allow contextual bypass of multi-t…
Browse files Browse the repository at this point in the history
…enancy
  • Loading branch information
oojacoboo committed Jun 4, 2024
1 parent 30dc83b commit d2cbe55
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 11 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ use Rentpost\Doctrine\MultiTenancy\Attribute\MultiTenancy;
new MuiltiTenancy\Filter(
context: ['admin']
where: '$this.company_id = {companyId}'),
new MultiTenancy\Filter(
context: ['other'],
ignore: true,
),
new MultiTenancy\Filter(
context: ['visitor'],
where: '$this.id IN(
Expand All @@ -207,6 +211,11 @@ class Product
}
```

One other thing to note about the above example is the `ignore` parameter. This allows for you to
specify a contact where no filters will be applied. This can be expecially useful in combination
with the `FilterStrategy::FirstMatch` strategy. The combination of these two allows you to entirely,
or selectively, ignore all multi-tenancy for an entity - for a given context, that is.

## Issues / Bugs / Questions

Please feel free to raise an issue against this repository if you have any questions or problems.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
}
],
"require": {
"php": ">=7.4",
"php": ">=8.2",
"doctrine/orm": "^2.10"
},
"autoload": {
Expand Down
6 changes: 3 additions & 3 deletions src/Attribute/MultiTenancy.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ final class MultiTenancy
* @param MultiTenancy\Filter[] $filters
*/
public function __construct(
private bool $enable = true,
private array $filters = [],
private FilterStrategy $strategy = FilterStrategy::AnyMatch,
private readonly bool $enable = true,
private readonly array $filters = [],
private readonly FilterStrategy $strategy = FilterStrategy::AnyMatch,
) {}


Expand Down
18 changes: 15 additions & 3 deletions src/Attribute/MultiTenancy/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ final class Filter
/**
* Constructor
*
* @param string[] $context
* @param string[] $context The contexts for which to apply the filter
* @param string $where The SQL where clause (use $this, for the current Entity's table)
* @param bool $ignore Whether to ignore this filter and not apply any conditions
*/
public function __construct(
private array $context = [],
private string $where = '',
private readonly array $context = [],
private readonly string $where = '',
private bool $ignore = false,
) {}


Expand All @@ -46,4 +49,13 @@ public function getWhereClause(): string
{
return $this->where;
}


/**
* Whether this filter is ignored and won't apply any conditions
*/
public function isIgnored(): bool
{
return $this->ignore;
}
}
8 changes: 6 additions & 2 deletions src/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
class Filter extends SQLFilter
{

protected ?Listener $listener = null;
protected ?EntityManagerInterface $entityManager = null;
private ?Listener $listener = null;
private ?EntityManagerInterface $entityManager = null;


/**
Expand Down Expand Up @@ -212,6 +212,10 @@ public function addFilterConstraint(ClassMetadata $targetEntity, $targetTableAli
continue;
}

if ($filter->isIgnored()) {
continue;
}

[$identifiers, $values] = $this->getMergedMaps(
$defaultMap,
...$this->getValueHolderIdentifiersAndValues($filter),
Expand Down
7 changes: 5 additions & 2 deletions src/Listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
class Listener implements EventSubscriber
{

/** @var ValueHolderInterface */
protected array $valueHolders = [];

/** @var ContextProviderInterface */
protected array $contextProviders = [];


Expand Down Expand Up @@ -59,7 +62,7 @@ public function addValueHolder(ValueHolderInterface $valueHolder): void
/**
* Gets all of the ValueHolders
*
* @return string[]
* @return array<ValueHolderInterface>
*/
public function getValueHolders(): array
{
Expand Down Expand Up @@ -92,7 +95,7 @@ public function addContextProvider(ContextProviderInterface $contextProvider): v
/**
* Gets all of the ContextProviders
*
* @return string[]
* @return array<ContextProviderInterface>
*/
public function getContextProviders(): array
{
Expand Down

0 comments on commit d2cbe55

Please sign in to comment.