Skip to content

Commit 5aa6d57

Browse files
authored
Merge pull request #16 from UseMuffin/uuid-obfuscation
Add `UuidStrategy` for obfuscation
2 parents f0c02ef + 0fb809d commit 5aa6d57

File tree

3 files changed

+90
-7
lines changed

3 files changed

+90
-7
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
namespace Muffin\Obfuscate\Model\Behavior\Strategy;
3+
4+
use Cake\ORM\Table;
5+
6+
/**
7+
* Class UuidStrategy
8+
*
9+
*/
10+
class UuidStrategy implements StrategyInterface
11+
{
12+
13+
/**
14+
* UUID field to use.
15+
*
16+
* @var string
17+
*/
18+
protected $_field;
19+
20+
/**
21+
* Table using this strategy.
22+
*
23+
* @var Table
24+
*/
25+
protected $_table;
26+
27+
/**
28+
* Constructor.
29+
*
30+
* @param Table $table Instance of the table using the strategy.
31+
* @param string $field Name of the UUID field on the table.
32+
*/
33+
public function __construct($table, $field = 'uuid')
34+
{
35+
$this->_table = $table;
36+
$this->_field = $field;
37+
}
38+
39+
/**
40+
* {@inheritdoc}
41+
*
42+
* @param string $str String to obfuscate.
43+
* @return string
44+
*/
45+
public function obfuscate($str)
46+
{
47+
$record = $this->_table
48+
->find()
49+
->where([$this->_table->getPrimaryKey() => $str])
50+
->select([$this->_field])
51+
->firstOrFail();
52+
53+
return $record->{$this->_field};
54+
}
55+
56+
/**
57+
* {@inheritdoc}
58+
*
59+
* @param string $str String to elucidate.
60+
* @return string
61+
*/
62+
public function elucidate($str)
63+
{
64+
$pk = $this->_table->getPrimaryKey();
65+
66+
$record = $this->_table
67+
->find()
68+
->where([$this->_field => $str])
69+
->select([$pk])
70+
->firstOrFail();
71+
72+
return $record->$pk;
73+
}
74+
}

tests/Fixture/AuthorsFixture.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class AuthorsFixture extends TestFixture
1414
*/
1515
public $fields = [
1616
'id' => ['type' => 'integer'],
17+
'uuid' => ['type' => 'string'],
1718
'name' => ['type' => 'string', 'null' => false],
1819
'created' => ['type' => 'datetime', 'null' => true],
1920
'modified' => ['type' => 'datetime', 'null' => true],
@@ -26,8 +27,8 @@ class AuthorsFixture extends TestFixture
2627
* @var array
2728
*/
2829
public $records = [
29-
['name' => 'John'],
30-
['name' => 'Jane'],
30+
['name' => 'John', 'uuid' => 'f1f88079-ec15-4863-ad41-7e85cfa98f3d'],
31+
['name' => 'Jane', 'uuid' => 'a2234d7c-6de7-4b28-aa34-39d57f3d35e3'],
3132
];
3233

3334
public function init()

tests/TestCase/Model/Behavior/ObfuscateBehaviorTest.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Cake\ORM\TableRegistry;
66
use Cake\TestSuite\TestCase;
77
use Muffin\Obfuscate\Model\Behavior\Strategy\TinyStrategy;
8+
use Muffin\Obfuscate\Model\Behavior\Strategy\UuidStrategy;
89

910
class ObfuscateBehaviorTest extends TestCase
1011
{
@@ -33,15 +34,17 @@ public function setUp()
3334
{
3435
parent::setUp();
3536

36-
$strategy = new TinyStrategy('5SX0TEjkR1mLOw8Gvq2VyJxIFhgCAYidrclDWaM3so9bfzZpuUenKtP74QNH6B');
37-
3837
$this->Authors = TableRegistry::get('Muffin/Obfuscate.Authors', ['table' => 'obfuscate_authors']);
39-
$this->Authors->addBehavior('Muffin/Obfuscate.Obfuscate', compact('strategy'));
38+
$this->Authors->addBehavior('Muffin/Obfuscate.Obfuscate', [
39+
'strategy' => new UuidStrategy($this->Authors),
40+
]);
4041

4142
$this->Comments = TableRegistry::get('Muffin/Obfuscate.Comments', ['table' => 'obfuscate_comments']);
4243

4344
$this->Tags = TableRegistry::get('Muffin/Obfuscate.Tags', ['table' => 'obfuscate_tags']);
44-
$this->Tags->addBehavior('Muffin/Obfuscate.Obfuscate', compact('strategy'));
45+
$this->Tags->addBehavior('Muffin/Obfuscate.Obfuscate', [
46+
'strategy' => new TinyStrategy('5SX0TEjkR1mLOw8Gvq2VyJxIFhgCAYidrclDWaM3so9bfzZpuUenKtP74QNH6B'),
47+
]);
4548

4649
$this->Articles = TableRegistry::get('Muffin/Obfuscate.Articles', ['table' => 'obfuscate_articles']);
4750
$this->Articles->addBehavior('Muffin/Obfuscate.Obfuscate', ['strategy' => new TinyStrategy('5SX0TEjkR1mLOw8Gvq2VyJxIFhgCAYidrclDWaM3so9bfzZpuUenKtP74QNH6B')]);
@@ -91,7 +94,7 @@ public function testFindObfuscate()
9194
$this->Tags->getAlias(),
9295
])->first();
9396

94-
$this->assertEquals('S', $result['author']['id']);
97+
$this->assertEquals('f1f88079-ec15-4863-ad41-7e85cfa98f3d', $result['author']['id']);
9598
$this->assertEquals('S', $result['tags'][0]['id']);
9699
$this->assertEquals(1, $result['comments'][0]['id']);
97100
$this->assertEquals(2, $result['comments'][1]['id']);
@@ -125,6 +128,11 @@ public function testFindObfuscated()
125128
->where(['id' => 'S'])
126129
->toArray();
127130
$this->assertEquals('1', $results[0]['id']);
131+
132+
$results = $this->Authors->find('obfuscated')
133+
->where(['id' => 'f1f88079-ec15-4863-ad41-7e85cfa98f3d'])
134+
->toArray();
135+
$this->assertEquals('1', $results[0]['id']);
128136
}
129137

130138
/**

0 commit comments

Comments
 (0)