Skip to content

Commit 118f9f1

Browse files
committed
Move consent queries to existing repository
The consent queries added to the ConsentRepository.php file (deleted) should have been added to the existing DbalConsentRepository. While at it, naming conventions have been applied to the query names. And the service config was updated.
1 parent eead884 commit 118f9f1

File tree

6 files changed

+99
-129
lines changed

6 files changed

+99
-129
lines changed

src/OpenConext/EngineBlock/Authentication/Repository/ConsentRepository.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,10 @@ public function findAllFor($userId);
3737
public function deleteAllFor($userId);
3838

3939
public function deleteOneFor(string $userId, string $serviceProviderEntityId): bool;
40+
41+
public function hasConsentHash(array $parameters): bool;
42+
43+
public function storeConsentHash(array $parameters): bool;
44+
45+
public function countTotalConsent($consentUid): int;
4046
}

src/OpenConext/EngineBlock/Service/Consent/ConsentHashRepository.php

Lines changed: 0 additions & 111 deletions
This file was deleted.

src/OpenConext/EngineBlock/Service/Consent/ConsentHashService.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
namespace OpenConext\EngineBlock\Service\Consent;
2020

21-
use PDO;
21+
use OpenConext\EngineBlock\Authentication\Repository\ConsentRepository;
2222
use function array_filter;
2323
use function array_keys;
2424
use function array_values;
@@ -36,28 +36,28 @@
3636
final class ConsentHashService
3737
{
3838
/**
39-
* @var ConsentHashRepository
39+
* @var ConsentRepository
4040
*/
41-
private $consentHashRepository;
41+
private $consentRepository;
4242

43-
public function __construct(ConsentHashRepository $consentHashRepository)
43+
public function __construct(ConsentRepository $consentHashRepository)
4444
{
45-
$this->consentHashRepository = $consentHashRepository;
45+
$this->consentRepository = $consentHashRepository;
4646
}
4747

48-
public function retrieveConsentHashFromDb(PDO $dbh, array $parameters): bool
48+
public function retrieveConsentHashFromDb(array $parameters): bool
4949
{
50-
return $this->consentHashRepository->retrieveConsentHashFromDb($dbh, $parameters);
50+
return $this->consentRepository->hasConsentHash($parameters);
5151
}
5252

53-
public function storeConsentHashInDb(PDO $dbh, array $parameters): bool
53+
public function storeConsentHashInDb(array $parameters): bool
5454
{
55-
return $this->consentHashRepository->storeConsentHashInDb($dbh, $parameters);
55+
return $this->consentRepository->storeConsentHash($parameters);
5656
}
5757

58-
public function countTotalConsent(PDO $dbh, $consentUid): int
58+
public function countTotalConsent($consentUid): int
5959
{
60-
return $this->consentHashRepository->countTotalConsent($dbh, $consentUid);
60+
return $this->consentRepository->countTotalConsent($consentUid);
6161
}
6262

6363
public function getUnstableAttributesHash(array $attributes, bool $mustStoreValues): string

src/OpenConext/EngineBlockBundle/Authentication/Repository/DbalConsentRepository.php

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use OpenConext\EngineBlock\Authentication\Value\ConsentType;
2727
use OpenConext\EngineBlock\Exception\RuntimeException;
2828
use PDO;
29+
use PDOException;
2930
use Psr\Log\LoggerInterface;
3031
use function sha1;
3132

@@ -56,7 +57,7 @@ public function __construct(DbalConnection $connection, LoggerInterface $logger)
5657
*/
5758
public function findAllFor($userId)
5859
{
59-
$sql = '
60+
$sql = '
6061
SELECT
6162
service_id
6263
, consent_date
@@ -129,7 +130,8 @@ public function deleteOneFor(string $userId, string $serviceProviderEntityId): b
129130
hashed_user_id = :hashed_user_id
130131
AND
131132
service_id = :service_id
132-
AND deleted_at IS NULL
133+
AND
134+
deleted_at IS NULL
133135
';
134136
try {
135137
$result = $this->connection->executeQuery(
@@ -161,4 +163,79 @@ public function deleteOneFor(string $userId, string $serviceProviderEntityId): b
161163
);
162164
}
163165
}
166+
167+
/**
168+
* @throws RuntimeException
169+
*/
170+
public function hasConsentHash(array $parameters): bool
171+
{
172+
try {
173+
$query = "
174+
SELECT
175+
*
176+
FROM
177+
{$this->_tableName}
178+
WHERE
179+
hashed_user_id = ?
180+
AND
181+
service_id = ?
182+
AND
183+
attribute = ?
184+
AND
185+
consent_type = ?
186+
AND
187+
deleted_at IS NULL
188+
";
189+
190+
$statement = $this->connection->prepare($query);
191+
$statement->execute($parameters);
192+
$rows = $statement->fetchAll();
193+
194+
if (count($rows) < 1) {
195+
// No stored consent found
196+
return false;
197+
}
198+
199+
return true;
200+
} catch (PDOException $e) {
201+
throw new RuntimeException(sprintf('Consent retrieval failed! Error: "%s"', $e->getMessage()));
202+
}
203+
}
204+
205+
/**
206+
* @throws RuntimeException
207+
*/
208+
public function storeConsentHash(array $parameters): bool
209+
{
210+
$query = "INSERT INTO consent (hashed_user_id, service_id, attribute, consent_type, consent_date, deleted_at)
211+
VALUES (?, ?, ?, ?, NOW(), '0000-00-00 00:00:00')
212+
ON DUPLICATE KEY UPDATE attribute=VALUES(attribute), consent_type=VALUES(consent_type), consent_date=NOW()";
213+
$statement = $this->connection->prepare($query);
214+
if (!$statement) {
215+
throw new RuntimeException("Unable to create a prepared statement to insert consent?!");
216+
}
217+
218+
if (!$statement->execute($parameters)) {
219+
throw new RuntimeException(
220+
sprintf('Error storing consent: "%s"', var_export($statement->errorInfo(), true))
221+
);
222+
}
223+
224+
return true;
225+
}
226+
227+
/**
228+
* @throws RuntimeException
229+
*/
230+
public function countTotalConsent($consentUid): int
231+
{
232+
$query = "SELECT COUNT(*) FROM consent where hashed_user_id = ? AND deleted_at IS NULL";
233+
$parameters = array(sha1($consentUid));
234+
$statement = $this->connection->prepare($query);
235+
if (!$statement) {
236+
throw new RuntimeException("Unable to create a prepared statement to count consent?!");
237+
}
238+
$statement->execute($parameters);
239+
return (int)$statement->fetchColumn();
240+
}
164241
}

src/OpenConext/EngineBlockBundle/Resources/config/services.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -367,10 +367,7 @@ services:
367367
tags:
368368
- { name: 'twig.extension' }
369369

370-
engineblock.service.consent.ConsentHashRepository:
371-
class: OpenConext\EngineBlock\Service\Consent\ConsentHashRepository
372-
373370
engineblock.service.consent.ConsentHashService:
374371
class: OpenConext\EngineBlock\Service\Consent\ConsentHashService
375372
arguments:
376-
- "engineblock.service.consent.ConsentHashRepository"
373+
- "@engineblock.repository.consent"

tests/unit/OpenConext/EngineBlock/Service/Consent/ConsentHashServiceTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
use Mockery as m;
2222
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
23+
use OpenConext\EngineBlock\Authentication\Repository\ConsentRepository;
2324
use PHPUnit\Framework\TestCase;
2425

2526
class ConsentHashServiceTest extends TestCase
@@ -33,7 +34,7 @@ class ConsentHashServiceTest extends TestCase
3334

3435
public function setUp()
3536
{
36-
$mockConsentHashRepository = m::mock('OpenConext\EngineBlock\Service\Consent\ConsentHashRepository');
37+
$mockConsentHashRepository = m::mock(ConsentRepository::class);
3738
$this->chs = new ConsentHashService($mockConsentHashRepository);
3839
}
3940

0 commit comments

Comments
 (0)