Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cake5 #20

Merged
merged 11 commits into from
Sep 27, 2023
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ root = true
[*]
indent_style = space
indent_size = 4
charset = "utf-8"
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
Expand Down
99 changes: 8 additions & 91 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,102 +4,19 @@ on:
push:
branches:
- master
- cake5
pull_request:
branches:
- '*'

permissions:
contents: read

jobs:
testsuite:
runs-on: ubuntu-18.04
strategy:
fail-fast: false
matrix:
php-version: ['7.2', '7.4', '8.0']
db-type: [mysql, pgsql, sqlite]
prefer-lowest: ['']
include:
- php-version: '7.2'
db-type: 'sqlite'
prefer-lowest: 'prefer-lowest'

services:
postgres:
image: postgres
ports:
- 5432:5432
env:
POSTGRES_PASSWORD: postgres

steps:
- uses: actions/checkout@v1
with:
fetch-depth: 1

- name: Setup Service
if: matrix.db-type == 'mysql'
run: |
sudo service mysql start
mysql -h 127.0.0.1 -u root -proot -e 'CREATE DATABASE cakephp;'

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
extensions: mbstring, intl, pdo_${{ matrix.db-type }}
coverage: pcov

- name: Composer install
run: |
composer --version
if ${{ matrix.prefer-lowest == 'prefer-lowest' }}; then
composer update --prefer-lowest --prefer-stable
else
composer install
fi

- name: Run PHPUnit
run: |
if [[ ${{ matrix.db-type }} == 'sqlite' ]]; then export DB_URL='sqlite:///:memory:'; fi
if [[ ${{ matrix.db-type }} == 'mysql' ]]; then export DB_URL='mysql://root:[email protected]/cakephp'; fi
if [[ ${{ matrix.db-type }} == 'pgsql' ]]; then export DB_URL='postgres://postgres:[email protected]/postgres'; fi

if [[ ${{ matrix.php-version }} == '7.2' && ${{ matrix.db-type }} == 'mysql' ]]; then
vendor/bin/phpunit --coverage-clover=coverage.xml
else
vendor/bin/phpunit
fi

- name: Code Coverage Report
if: success() && matrix.php-version == '7.2' && matrix.db-type == 'mysql'
uses: codecov/codecov-action@v1
uses: cakephp/.github/.github/workflows/[email protected]
secrets: inherit

cs-stan:
name: Coding Standard & Static Analysis
runs-on: ubuntu-18.04

steps:
- uses: actions/checkout@v1
with:
fetch-depth: 1

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '7.4'
extensions: mbstring, intl
coverage: none
tools: psalm:^4.4, phpstan:^0.12

- name: Composer Install
run: composer require --dev cakephp/cakephp-codesniffer:^4.2

- name: Run phpcs
run: vendor/bin/phpcs src/ tests/

- name: Run psalm
if: success() || failure()
run: psalm --output-format=github

- name: Run phpstan
if: success() || failure()
run: phpstan analyse
uses: cakephp/.github/.github/workflows/[email protected]
secrets: inherit
5 changes: 5 additions & 0 deletions .phive/phars.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<phive xmlns="https://phar.io/phive">
<phar name="phpstan" version="1.10.32" installed="1.10.32" location="./tools/phpstan" copy="false"/>
<phar name="psalm" version="5.15.0" installed="5.15.0" location="./tools/psalm" copy="false"/>
</phive>
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[![Total Downloads](https://img.shields.io/packagist/dt/muffin/obfuscate.svg?style=flat-square)](https://packagist.org/packages/muffin/obfuscate)
[![License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](LICENSE)

Primary key obfuscation for CakePHP using HashIds, Optimus, Tiny and/or custom obfuscation strategies.
Primary key obfuscation for CakePHP using HashIds, Optimus, Base62 and/or custom obfuscation strategies.

## Installation

Expand Down Expand Up @@ -33,7 +33,7 @@ want to use in your application:
```
composer require hashids/hashids
composer require jenssegers/optimus
composer require zackkitzmiller/tiny
composer require tuupola/base62
```

## Built-in obfuscation strategies
Expand All @@ -48,7 +48,7 @@ Use the [OptimusStrategy](https://github.com/jenssegers/optimus) if you want to:
- obfuscate your primary keys with integers based on Knuth's integer hash
- present record ids like 347 as integers like 372555994

Use the [TinyStrategy](https://github.com/zackkitzmiller/tiny-php) if you want to:
Use the [Base62Strategy](https://github.com/tuupola/base62) if you want to:

- obfuscate your primary keys with base62 strings and integers
- present record ids like 347 as strings like "vk"
Expand Down Expand Up @@ -90,12 +90,12 @@ $this->addBehavior('Muffin/Obfuscate.Obfuscate', [
```

```php
use Muffin\Obfuscate\Model\Behavior\Strategy\TinyStrategy;
use Muffin\Obfuscate\Model\Behavior\Strategy\Base62Strategy;

$this->addBehavior('Muffin/Obfuscate.Obfuscate', [
// Strategy constructor parameters:
// $set - Random alpha-numeric set where each character must only be used exactly once
'strategy' => new TinyStrategy('5SX0TEjkR1mLOw8Gvq2VyJxIFhgCAYidrclDWaM3so9bfzZpuUenKtP74QNH6B')
'strategy' => new Base62Strategy('5SX0TEjkR1mLOw8Gvq2VyJxIFhgCAYidrclDWaM3so9bfzZpuUenKtP74QNH6B')
]);
```

Expand Down
34 changes: 27 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,18 @@
"source": "https://github.com/usemuffin/obfuscate"
},
"require": {
"cakephp/orm": "^4.1"
"cakephp/orm": "^5.0.0"
},
"require-dev": {
"cakephp/cakephp": "^4.1",
"phpunit/phpunit": "~8.5.0",
"zackkitzmiller/tiny": "^1.2",
"jenssegers/optimus": "^0.2",
"hashids/hashids": "^1.0.5"
"cakephp/cakephp": "^5.0.0",
"phpunit/phpunit": "^10.1.0",
"cakephp/cakephp-codesniffer": "^5.0",
"jenssegers/optimus": "^1.1.1",
"hashids/hashids": "^1.0.5",
"tuupola/base62": "^2.1"
},
"suggest": {
"zackkitzmiller/tiny": "A reversible base62 ID obfuscater",
"tuupola/base62": "Base62 encoder and decoder for arbitrary data",
"jenssegers/optimus": "Id obfuscation based on Knuth's multiplicative hashing method",
"hashids/hashids": "Generate hashids like YouTube or Bitly from numbers to obfuscate your database primary ids, or navigate to the right shard"
},
Expand All @@ -54,5 +55,24 @@
"psr-4": {
"Muffin\\Obfuscate\\Test\\": "tests"
}
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
},
"scripts": {
"cs-check": "phpcs --colors --parallel=16 -p src/ tests/",
"cs-fix": "phpcbf --colors --parallel=16 -p src/ tests/",
"phpstan": "tools/phpstan analyse",
"psalm": "tools/psalm --show-info=false",
"stan": [
"@phpstan",
"@psalm"
],
"stan-baseline": "tools/phpstan --generate-baseline",
"psalm-baseline": "tools/psalm --set-baseline=psalm-baseline.xml",
"stan-setup": "phive install",
"test": "phpunit"
}
}
2 changes: 0 additions & 2 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<?xml version="1.0"?>
<ruleset name="Muffin/Obfuscate">
<config name="installed_paths" value="../../cakephp/cakephp-codesniffer" />

<rule ref="CakePHP" />

<arg value="s"/>
</ruleset>
41 changes: 16 additions & 25 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,31 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
bootstrap="./tests/bootstrap.php"
colors="true"
stopOnFailure="false"
convertDeprecationsToExceptions="false"
>

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" colors="true" processIsolation="false" stopOnFailure="false" bootstrap="tests/bootstrap.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.2/phpunit.xsd" cacheDirectory=".phpunit.cache">
<coverage/>
<php>
<ini name="memory_limit" value="-1"/>
<ini name="apc.enable_cli" value="1"/>
<env name="FIXTURE_SCHEMA_METADATA" value="./tests/schema.php"/>
</php>
<testsuites>
<testsuite name="Obfuscate Test Cases">
<directory>./tests/</directory>
<directory>tests/TestCase/</directory>
</testsuite>
</testsuites>

<!-- Setup a listener for fixtures -->
<listeners>
<listener
class="\Cake\TestSuite\Fixture\FixtureInjector"
file="./vendor/cakephp/cakephp/src/TestSuite/Fixture/FixtureInjector.php">
<arguments>
<object class="\Cake\TestSuite\Fixture\FixtureManager" />
</arguments>
</listener>
</listeners>

<filter>
<whitelist>
<directory suffix=".php">./src/</directory>
</whitelist>
</filter>
<extensions>
<bootstrap class="Cake\TestSuite\Fixture\Extension\PHPUnitExtension"/>
</extensions>
<source>
<include>
<directory suffix=".php">src/</directory>
</include>
</source>
</phpunit>
32 changes: 15 additions & 17 deletions src/Model/Behavior/ObfuscateBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Cake\Datasource\EntityInterface;
use Cake\Event\EventInterface;
use Cake\ORM\Behavior;
use Cake\ORM\Query;
use Cake\ORM\Query\SelectQuery;
use InvalidArgumentException;
use Muffin\Obfuscate\Model\Behavior\Strategy\StrategyInterface;
use RuntimeException;
Expand All @@ -19,9 +19,9 @@
class ObfuscateBehavior extends Behavior
{
/**
* @inheritDoc
* @var array<string, mixed>
*/
protected $_defaultConfig = [
protected array $_defaultConfig = [
'strategy' => null,
'implementedFinders' => [
'obfuscated' => 'findObfuscated',
Expand Down Expand Up @@ -73,7 +73,7 @@ public function verifyConfig(): void
* @param \ArrayObject $options Options.
* @return void
*/
public function afterSave(EventInterface $event, EntityInterface $entity, ArrayObject $options)
public function afterSave(EventInterface $event, EntityInterface $entity, ArrayObject $options): void
{
$pk = $this->_table->getPrimaryKey();
if (is_array($pk)) {
Expand All @@ -87,12 +87,12 @@ public function afterSave(EventInterface $event, EntityInterface $entity, ArrayO
* Callback to set the `obfuscated` finder on all associations.
*
* @param \Cake\Event\EventInterface $event EventInterface.
* @param \Cake\ORM\Query $query Query.
* @param \Cake\ORM\Query\SelectQuery $query Query.
* @param \ArrayObject $options Options.
* @param bool $primary True if this is the primary table.
* @return void
*/
public function beforeFind(EventInterface $event, Query $query, ArrayObject $options, bool $primary)
public function beforeFind(EventInterface $event, SelectQuery $query, ArrayObject $options, bool $primary): void
{
if (empty($options['obfuscate']) || !$primary) {
return;
Expand Down Expand Up @@ -124,23 +124,21 @@ public function beforeFind(EventInterface $event, Query $query, ArrayObject $opt
/**
* Custom finder to search for records using an obfuscated primary key.
*
* @param \Cake\ORM\Query $query Query.
* @param array $options Options.
* @return \Cake\ORM\Query
* @param \Cake\ORM\Query\SelectQuery $query Query.
* @return \Cake\ORM\Query\SelectQuery
*/
public function findObfuscated(Query $query, array $options)
public function findObfuscated(SelectQuery $query): SelectQuery
{
return $query->applyOptions(['obfuscate' => true]);
}

/**
* Custom finder that obfuscates primary keys in returned result set.
*
* @param \Cake\ORM\Query $query Query.
* @param array $options Options.
* @return \Cake\ORM\Query
* @param \Cake\ORM\Query\SelectQuery $query Query.
* @return \Cake\ORM\Query\SelectQuery
*/
public function findObfuscate(Query $query, array $options)
public function findObfuscate(SelectQuery $query): SelectQuery
{
$query->applyOptions(['obfuscate' => true]);

Expand All @@ -162,18 +160,18 @@ public function findObfuscate(Query $query, array $options)
* @param string|int $str String to obfuscate.
* @return string
*/
public function obfuscate($str): string
public function obfuscate(string|int $str): string
{
return $this->strategy()->obfuscate($str);
}

/**
* Proxy to the obfuscating strategy's `elucidate()`.
*
* @param int|string $str String to elucidate.
* @param string|int $str String to elucidate.
* @return int
*/
public function elucidate($str): int
public function elucidate(string|int $str): int
{
return $this->strategy()->elucidate($str);
}
Expand Down
Loading
Loading