Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ root = true
[*]
indent_style = space
indent_size = 4
charset = "utf-8"
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[*.bat]
end_of_line = crlf

[*.yml]
indent_style = space
indent_size = 2
indent_size = 2
5 changes: 4 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# Remove files for archives generated using `git archive`
CONTRIBUTING.md export-ignore
.editorconfig export-ignore
.gitattributes export-ignore
.gitignore export-ignore
.semver export-ignore
phpunit.xml.dist export-ignore
.travis.yml export-ignore
.scrutinizer.yml export-ignore
tests export-ignore
docs export-ignore
.github export-ignore
10 changes: 7 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/composer.lock
/plugins
/vendor
*.pyc
docs/_build
phpunit.xml
vendor/
composer.lock
tmp
.phpunit.result.cache
6 changes: 0 additions & 6 deletions .semver

This file was deleted.

11 changes: 11 additions & 0 deletions .sticker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
linters:
phpcs:
standard: CakePHP4
extensions: 'php'
fixer: true
files:
ignore:
- 'vendor/*'
fixers:
enable: true
workflow: commit
49 changes: 31 additions & 18 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,52 +1,65 @@
language: php

php:
- 5.6
- 7.2
- 7.3

services:
- mysql
- postgresql
- 7.4

env:
matrix:
- DB=sqlite db_dsn='sqlite:///:memory:'
- DB=mysql db_dsn='mysql://root@127.0.0.1/cakephp_test?init[]=SET sql_mode = "STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"'
- DB=mysql db_dsn='mysql://root@127.0.0.1/cakephp_test'
- DB=pgsql db_dsn='postgres://postgres@127.0.0.1/cakephp_test'
- DB=sqlite db_dsn='sqlite:///:memory:'

global:
- DEFAULT=1

services:
- mysql
- postgresql

matrix:
fast_finish: true

include:
- php: 7.3
- php: 7.2
env: PHPCS=1 DEFAULT=0

- php: 5.6
env: PREFER_LOWEST=1
- php: 7.2
env: STATIC_ANALYSIS=1 DEFAULT=0

before_script:
- if [[ $TRAVIS_PHP_VERSION != 7.2 ]]; then phpenv config-rm xdebug.ini; fi

- if [[ $PREFER_LOWEST != 1 ]]; then composer install --no-interaction; fi
- if [[ $PREFER_LOWEST != 1 ]]; then composer update --no-interaction; fi
- if [[ $PREFER_LOWEST == 1 ]]; then composer update --no-interaction --prefer-lowest --prefer-stable; fi

- if [[ $DB == 'mysql' ]]; then mysql -e 'CREATE DATABASE cakephp_test;'; fi
- if [[ $DB == 'pgsql' ]]; then psql -c 'CREATE DATABASE cakephp_test;' -U postgres; fi
- if [[ $DB = 'mysql' ]]; then mysql -u root -e 'CREATE DATABASE cakephp_test;'; fi
- if [[ $DB = 'pgsql' ]]; then psql -c 'CREATE DATABASE cakephp_test;' -U postgres; fi

- if [[ $PHPCS == '1' ]]; then composer require cakephp/cakephp-codesniffer:^3.0; fi
- if [[ $PHPCS = 1 ]]; then composer require cakephp/cakephp-codesniffer:^4.0; fi
- if [[ $STATIC_ANALYSIS = 1 ]]; then composer require --dev phpstan/phpstan:^0.12 psalm/phar:^3.7; fi

script:
- if [[ $DEFAULT == 1 && $TRAVIS_PHP_VERSION == 7.2 ]]; then vendor/bin/phpunit --coverage-clover=clover.xml; fi
- if [[ $DEFAULT == 1 && $TRAVIS_PHP_VERSION != 7.2 ]]; then vendor/bin/phpunit; fi
- |
if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION = 7.2 ]]; then
mkdir -p build/logs
vendor/bin/phpunit --coverage-clover=build/logs/clover.xml
fi

- if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION != 7.2 ]]; then vendor/bin/phpunit; fi

- if [[ $PHPCS = 1 ]]; then vendor/bin/phpcs -p --standard=vendor/cakephp/cakephp-codesniffer/CakePHP ./src ./tests; fi

- if [[ $PHPCS == 1 ]]; then vendor/bin/phpcs -p --extensions=php --standard=vendor/cakephp/cakephp-codesniffer/CakePHP ./src ./tests; fi
- if [[ $STATIC_ANALYSIS = 1 ]]; then vendor/bin/phpstan.phar analyse src && vendor/bin/psalm.phar --show-info=false; fi

after_success:
- if [[ $DEFAULT == 1 && $TRAVIS_PHP_VERSION == 7.2 ]]; then bash <(curl -s https://codecov.io/bash); fi
- |
if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION = 7.2 ]]; then
wget https://github.com/php-coveralls/php-coveralls/releases/download/v2.1.0/php-coveralls.phar
chmod +x php-coveralls.phar
./php-coveralls.phar
fi

notifications:
email: false
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ Load the plugin by either running this console command:
bin/cake plugin load Muffin/Obfuscate
```

or by manually adding the following line to `src/Application.php`:

```php
$this->addPlugin('Muffin/Obfuscate');
```

Lastly, composer install (any combination of) the obfuscation libraries you
want to use in your application:

Expand Down
1 change: 0 additions & 1 deletion VERSION

This file was deleted.

9 changes: 6 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,14 @@
"source": "https://github.com/usemuffin/obfuscate"
},
"require": {
"cakephp/orm": "^3.5"
"cakephp/orm": "^4.0"
},
"require-dev": {
"cakephp/cakephp": "^3.5",
"phpunit/phpunit": "^5.7.14|^6.0",
"cakephp/cakephp": "^4.0",
"cakephp/cakephp-codesniffer": "^4.0",
"phpstan/phpstan": "^0.12",
"phpunit/phpunit": "~8.5.0",
"psalm/phar": "^3.9",
"zackkitzmiller/tiny": "^1.2",
"jenssegers/optimus": "^0.2",
"hashids/hashids": "^1.0.5"
Expand Down
2 changes: 2 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
parameters:
ignoreErrors: []
7 changes: 7 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
includes:
- phpstan-baseline.neon

parameters:
level: 6
checkGenericClassInNonGenericObjectType: false
checkMissingIterableValueType: false
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
bootstrap="./tests/bootstrap.php"
colors="true"
stopOnFailure="false"
convertDeprecationsToExceptions="false"
>

<testsuites>
Expand Down
29 changes: 29 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0"?>
<psalm
totallyTyped="false"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd">
<projectFiles>
<directory name="src" />
<ignoreFiles>
<directory name="vendor" />
</ignoreFiles>
</projectFiles>

<issueHandlers>
<MissingClosureReturnType errorLevel="info" />

<PropertyNotSetInConstructor errorLevel="info" />
<MissingConstructor errorLevel="info" />
<MissingClosureParamType errorLevel="info" />

<DocblockTypeContradiction errorLevel="info" />
<RedundantConditionGivenDocblockType errorLevel="info" />

<UndefinedPropertyFetch errorLevel="info" />
<UndefinedThisPropertyFetch errorLevel="info" />
<UndefinedPropertyAssignment errorLevel="info" />
</issueHandlers>
</psalm>
35 changes: 22 additions & 13 deletions src/Model/Behavior/ObfuscateBehavior.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
<?php
declare(strict_types=1);

namespace Muffin\Obfuscate\Model\Behavior;

use ArrayObject;
use Cake\Core\Exception\Exception;
use Cake\Database\Expression\Comparison;
use Cake\Datasource\EntityInterface;
use Cake\Event\Event;
use Cake\Event\EventInterface;
use Cake\ORM\Behavior;
use Cake\ORM\Query;
use Cake\ORM\ResultSet;
use Muffin\Obfuscate\Model\Behavior\Strategy\StrategyInterface;
use RuntimeException;

/**
* Class ObfuscateBehavior
*
*/
class ObfuscateBehavior extends Behavior
{

/**
* {@inheritdoc}
*/
Expand All @@ -38,7 +40,7 @@ class ObfuscateBehavior extends Behavior
* @param array $config Behavior's configuration.
* @return void
*/
public function initialize(array $config)
public function initialize(array $config): void
{
$this->verifyConfig();
}
Expand All @@ -48,10 +50,10 @@ public function initialize(array $config)
*
* @return void
*/
public function verifyConfig()
public function verifyConfig(): void
{
$strategy = $this->getConfig('strategy');
if (!$strategy) {
if (empty($strategy)) {
throw new Exception('Missing required obfuscation strategy.');
}

Expand All @@ -67,37 +69,44 @@ public function verifyConfig()
/**
* Callback to obfuscate the record(s)' primary key returned after a save operation.
*
* @param \Cake\ORM\Behavior\Event $event Event.
* @param \Cake\ORM\Behavior\EntityInterface $entity Entity.
* @param \Cake\Event\EventInterface $event EventInterface.
* @param \Cake\Datasource\EntityInterface $entity Entity.
* @param \ArrayObject $options Options.
* @return void
*/
public function afterSave(Event $event, EntityInterface $entity, ArrayObject $options)
public function afterSave(EventInterface $event, EntityInterface $entity, ArrayObject $options)
{
$pk = $this->_table->getPrimaryKey();
if (is_array($pk)) {
throw new RuntimeException('Composite primary keys are not supported.');
}
$entity->set($pk, $this->obfuscate($entity->{$pk}));
$entity->setDirty($pk, false);
}

/**
* Callback to set the `obfuscated` finder on all associations.
*
* @param \Cake\ORM\Behavior\Event $event Event.
* @param \Cake\Event\EventInterface $event EventInterface.
* @param \Cake\ORM\Query $query Query.
* @param \ArrayObject $options Options.
* @param bool $primary True if this is the primary table.
* @return void
*/
public function beforeFind(Event $event, Query $query, ArrayObject $options, $primary)
public function beforeFind(EventInterface $event, Query $query, ArrayObject $options, $primary)
{
if (empty($options['obfuscate']) || !$primary) {
return;
}

$query->traverseExpressions(function ($expression) {
$pk = $this->_table->getPrimaryKey();
if (is_array($pk)) {
throw new RuntimeException('Composite primary keys are not supported.');
}

if (
method_exists($expression, 'getField')
$expression instanceof Comparison
&& in_array($expression->getField(), [$pk, $this->_table->aliasField($pk)])
) {
$expression->setValue($this->elucidate($expression->getValue()));
Expand Down Expand Up @@ -173,7 +182,7 @@ public function elucidate($str)
/**
* Get the configured strategy.
*
* @return \Muffin\Obfuscate\Model\Behavior\ObfuscateStrategy\StrategyInterface
* @return \Muffin\Obfuscate\Model\Behavior\Strategy\StrategyInterface
*/
public function strategy()
{
Expand Down
Loading