Skip to content
This repository was archived by the owner on Jun 17, 2025. It is now read-only.

Commit 2983bfc

Browse files
authored
Merge pull request #1 from webfactory/development
Entwicklung eines Sichtbarkeits-Filterungs-Systems für wf-Anwendungen
2 parents 3e424e5 + 0edf839 commit 2983bfc

30 files changed

+1220
-0
lines changed

.github/workflows/depedencies.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
on: [push]
2+
name: Dependencies
3+
env:
4+
PHP_VERSION: 7.2
5+
6+
jobs:
7+
composer-require-checker:
8+
name: Check missing composer requirements
9+
runs-on: ubuntu-18.04
10+
steps:
11+
- uses: webfactory/ssh-agent@master
12+
with:
13+
ssh-private-key: ${{ secrets.ORG_SSH_PRIVATE_KEY }}
14+
- uses: actions/checkout@v2
15+
- name: Configure Composer
16+
run: |
17+
mkdir -p ~/.composer/cache
18+
umask 077 && cat > ~/.composer/auth.json <<< '${{ secrets.ORG_COMPOSER_AUTH_JSON }}'
19+
- name: Konfiguriere PHP-Version und -Einstellungen im Worker-Node
20+
uses: shivammathur/setup-php@v2
21+
with:
22+
php-version: ${{ env.PHP_VERSION }}
23+
tools: composer:v2
24+
ini-values: variables_order=EGPCS
25+
# Falls spezielle Extensions installiert werden müssen:
26+
# extensions: intl, mbstring
27+
- name: Cache Composer Dependencies
28+
uses: actions/cache@v1
29+
with:
30+
path: vendor/
31+
key: composer-${{ env.PHP_VERSION }}-${{ hashFiles('composer.*') }}
32+
restore-keys: |
33+
composer-${{ env.PHP_VERSION }}-${{ github.ref }}
34+
composer-${{ env.PHP_VERSION }}-
35+
- run: |
36+
composer install --no-interaction --no-scripts --no-progress --no-suggest
37+
composer show
38+
- name: ComposerRequireChecker
39+
uses: docker://webfactory/composer-require-checker:2.1.0

.github/workflows/fix-cs-php.yml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Update this by running
2+
# curl https://gist.githubusercontent.com/mpdude/ca93a185bcbf56eb7e341632ad4f8263/raw/fix-cs-php.yml > .github/workflows/fix-cs-php.yml
3+
4+
on:
5+
push:
6+
branches:
7+
- master
8+
pull_request:
9+
10+
name: Coding Standards
11+
12+
jobs:
13+
open-pr-for-cs-violations:
14+
name: PHP-CS-Fixer
15+
runs-on: ubuntu-18.04
16+
steps:
17+
18+
- name: Checkout code
19+
uses: actions/checkout@v2
20+
with:
21+
ref: ${{ github.head_ref }}
22+
23+
- name: Run PHP-CS-Fixer
24+
uses: docker://oskarstark/php-cs-fixer-ga:2.16.7
25+
26+
- name: Create PR for CS fixups
27+
uses: peter-evans/create-pull-request@c7f493a8000b8aeb17a1332e326ba76b57cb83eb
28+
id: create-pull-request
29+
with:
30+
token: ${{ secrets.GITHUB_TOKEN }}
31+
title: Fix coding standards in ${{ github.ref }}
32+
branch: php-cs-fixer/${{ github.ref }}
33+
assignees: ${{ github.actor }}
34+
labels: php-cs-fixer
35+
body: Please merge these changes into the ${{ github.ref }} branch to fix coding standard violations.
36+
commit-message: Apply php-cs-fixer changes as of ${{ github.sha }}
37+
38+
- name: Leave a notice in the discussion when fixing code in a Pull Request
39+
uses: docker://mpdude/comment-on-pr:v1.2.0
40+
env:
41+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
42+
if: steps.create-pull-request.outputs.pr_number # only if PR was created in the previous step
43+
continue-on-error: true # continue on failure - necessary when the current branch does not have a pending PR
44+
with:
45+
msg: |
46+
@${{ github.actor }} please apply the coding standard fixes from #${{ steps.create-pull-request.outputs.pr_number }}
47+
48+
- name: Fail the workflow when necessary CS fixes were detected
49+
run: echo "Failing workflow run because CS violations were detected." && exit 1
50+
if: steps.create-pull-request.outputs.pr_number

.github/workflows/tests.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
on: [push]
2+
name: Tests
3+
env:
4+
APP_ENV: test
5+
PHP_VERSION: 7.2
6+
TEMP: ${{ github.workspace }}/tmp
7+
8+
jobs:
9+
PHPUnit:
10+
name: PHPUnit
11+
runs-on: ubuntu-18.04
12+
steps:
13+
- uses: webfactory/ssh-agent@master
14+
with:
15+
ssh-private-key: ${{ secrets.ORG_SSH_PRIVATE_KEY }}
16+
- uses: actions/checkout@v2
17+
- name: Configure Composer
18+
run: |
19+
mkdir -p ~/.composer/cache
20+
umask 077 && cat > ~/.composer/auth.json <<< '${{ secrets.ORG_COMPOSER_AUTH_JSON }}'
21+
- run: mkdir --mode=777 -p $GITHUB_WORKSPACE/{tmp,logs}
22+
- name: Konfiguriere PHP-Version und -Einstellungen im Worker-Node
23+
uses: shivammathur/setup-php@v2
24+
with:
25+
php-version: ${{ env.PHP_VERSION }}
26+
tools: composer:v2
27+
ini-values: variables_order=EGPCS
28+
# Falls spezielle Extensions installiert werden müssen:
29+
# extensions: intl, mbstring
30+
- name: Cache Composer Dependencies
31+
uses: actions/cache@v1
32+
with:
33+
path: vendor/
34+
key: composer-${{ env.PHP_VERSION }}-${{ hashFiles('composer.*') }}
35+
restore-keys: |
36+
composer-${{ env.PHP_VERSION }}-${{ github.ref }}
37+
composer-${{ env.PHP_VERSION }}-
38+
- run: |
39+
composer install --no-interaction --no-scripts --no-progress --no-suggest
40+
composer show
41+
- run: vendor/bin/phpunit -vvv

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var/
2+
vendor/
3+
composer.lock
4+
.phpunit.*
5+
.php_cs.cache

.php_cs.dist

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
return PhpCsFixer\Config::create()
4+
->setRules([
5+
'@Symfony' => true,
6+
'@Symfony:risky' => true,
7+
'array_syntax' => array('syntax' => 'short'),
8+
'no_unreachable_default_argument_value' => false,
9+
'braces' => array('allow_single_line_closure' => true),
10+
'heredoc_to_nowdoc' => false,
11+
'phpdoc_annotation_without_dot' => false,
12+
])
13+
->setRiskyAllowed(true)
14+
->setFinder(
15+
PhpCsFixer\Finder::create()
16+
->in(__DIR__)
17+
->notPath('conf/')
18+
->notPath('tmp/')
19+
->notPath('node_modules/')
20+
->notPath('var/cache')
21+
->notPath('vendor/')
22+
->notPath('www')
23+
)
24+
;

LICENSE

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2021 webfactory GmbH, Bonn ([email protected])
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to
7+
deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in
14+
all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21+
FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
THE SOFTWARE.

README.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# VisibilityFilterBundle – A centralised approach to visibility of Doctrine Entities
2+
3+
This bundle provides a Doctrine Filter which handles visibility filtering for Entities transparently for a whole
4+
application, removing the need to repeatedly phrase the filtering in every repository method of an Entity. Most notably,
5+
the filtering also applies to Doctrine queries that bypass the repository, like relationships declared in the entity
6+
mapping.
7+
8+
## Getting started
9+
10+
First, you need to declare this bundle as a composer dependency.
11+
12+
```shell
13+
composer require webfactory/visibility-filter-bundle
14+
```
15+
16+
Next, the bundle needs to be registered to Symfony. Depending on your Symfony version, this might look like that:
17+
18+
```php
19+
# src/bundles.php
20+
21+
return [
22+
# ...
23+
Webfactory\VisibilityFilterBundle\VisibilityFilterBundle::class => ['all' => true],
24+
# ...
25+
];
26+
```
27+
28+
The filter class needs to be registered manually.
29+
30+
```yaml
31+
# src/config.yml
32+
doctrine:
33+
orm:
34+
filters:
35+
visibility: Webfactory\VisibilityFilterBundle\Filter\VisibilityColumnConsideringSQLFilter
36+
```
37+
38+
Important: The YAML key of the filter needs to `visibility`, otherwise the filter won't be activated on requests.
39+
40+
## Configuring the visibility column
41+
42+
This bundle assumes that the visibility determination is going to be based on a specific field in the Entity containing
43+
visibility information; e.g. functioning as a "visibility switch" containing "yes" or "no" or containing a visibility
44+
grade on a scale, based on which the visibility of the object will be determined.
45+
46+
**Currently, only entities that have a visibility column configured will be filtered at all.**
47+
48+
All you need to configure on your entity is *which* of its fields will be the one with the visibility information.
49+
You can do that by Adding the `VisibilityColumn()` annotation to that field.
50+
51+
```php
52+
53+
use Doctrine\ORM\Mapping as ORM;
54+
use Webfactory\VisibilityFilterBundle\Annotation\VisibilityColumn;
55+
56+
/**
57+
* @ORM\Entity()
58+
*/
59+
class EntityWithVisibilityColumn
60+
{
61+
// ...
62+
/**
63+
* @VisibilityColumn()
64+
*
65+
* @ORM\Column(type="string")
66+
*
67+
* @var string
68+
*/
69+
private $visibilityColumn;
70+
// ...
71+
}
72+
```
73+
74+
Please note that configuring more than one field as visibility column will throw an exception.
75+
76+
## Replacing the filter strategy
77+
78+
By default, the library makes you application only query entities from the database that have the string `y` in their
79+
visibility column. You can change this behaviour by overwriting the service
80+
`Webfactory\VisibilityFilterBundle\Filter\FilterStrategy` with your own implementation.
81+
82+
Your implementation needs to implement the `FilterStrategy` interface. If you only want to change the `y` string to
83+
something different, you can use the `Webfactory\VisibilityFilterBundle\Filter\Strategy\ValueInField` implementation
84+
and provide it with a different `visibleValue` in its constructor.

composer.json

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "webfactory/visibility-filter-bundle",
3+
"description": "Symfony Bundle that filters out invisible Doctrine entities in a centralised way",
4+
"license": "MIT",
5+
"autoload": {
6+
"psr-4": {
7+
"Webfactory\\VisibilityFilterBundle\\": "src"
8+
}
9+
},
10+
"autoload-dev": {
11+
"psr-4": {
12+
"Webfactory\\VisibilityFilterBundle\\Tests\\": "tests"
13+
}
14+
},
15+
"require": {
16+
"php": "^7.2",
17+
"doctrine/annotations": "^1.12",
18+
"doctrine/doctrine-bundle": "^1.12.13",
19+
"doctrine/orm": "^2.7.2",
20+
"symfony/config": "^4.4",
21+
"symfony/dependency-injection": "^3.4|^4.4",
22+
"symfony/event-dispatcher": "^3.4|^4.4",
23+
"symfony/http-kernel": "^3.4|^4.4"
24+
},
25+
"config": {
26+
"sort-packages": true,
27+
"platform": {
28+
"php": "7.2.24"
29+
}
30+
},
31+
"require-dev": {
32+
"dama/doctrine-test-bundle": "^6.5",
33+
"phpunit/phpunit": "^8.5",
34+
"symfony/framework-bundle": "^4.4",
35+
"symfony/yaml": "^3.4"
36+
}
37+
}

phpunit.xml.dist

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/8.5/phpunit.xsd"
5+
bootstrap="vendor/autoload.php" colors="true">
6+
7+
<php>
8+
<ini name="error_reporting" value="-1" />
9+
<env name="SYMFONY_DEPRECATIONS_HELPER" value="max[self]=0" />
10+
</php>
11+
12+
<testsuites>
13+
<testsuite name="Project Test Suite">
14+
<directory>tests</directory>
15+
</testsuite>
16+
</testsuites>
17+
18+
<!-- Filter for code coverage -->
19+
<filter>
20+
<whitelist addUncoveredFilesFromWhitelist="false">
21+
<directory suffix=".php">src/</directory>
22+
<exclude>
23+
<directory>vendor</directory>
24+
</exclude>
25+
</whitelist>
26+
</filter>
27+
28+
<extensions>
29+
<extension class="DAMA\DoctrineTestBundle\PHPUnit\PHPUnitExtension"/>
30+
</extensions>
31+
</phpunit>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Webfactory\VisibilityFilterBundle\Annotation;
4+
5+
use Webfactory\VisibilityFilterBundle\Filter\Strategy\FilterStrategy;
6+
7+
/**
8+
* This annotation marks a field of an entity as the field containing visibility information (e.g. "y" for visible,
9+
* "n" for invisible, or a more complicated system). If the VisibilityFilterBundle is set up, it will be interpreted
10+
* by an implementation of @see FilterStrategy.
11+
*
12+
* For more information on visibility filtering, consider reading the README.md file of this bundle.
13+
*
14+
* @Annotation
15+
* @Target("PROPERTY")
16+
*/
17+
final class VisibilityColumn
18+
{
19+
}

0 commit comments

Comments
 (0)