Skip to content

Commit eead884

Browse files
Koen CornelisMKodde
authored andcommitted
Move DB logic to ConsentHashRepository
1 parent d4e1160 commit eead884

File tree

6 files changed

+185
-96
lines changed

6 files changed

+185
-96
lines changed

library/EngineBlock/Corto/Model/Consent.php

Lines changed: 27 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -66,26 +66,18 @@ class EngineBlock_Corto_Model_Consent
6666
private $_hashService;
6767

6868
/**
69-
* @param string $tableName
70-
* @param bool $mustStoreValues
71-
* @param EngineBlock_Saml2_ResponseAnnotationDecorator $response
72-
* @param array $responseAttributes
73-
* @param EngineBlock_Database_ConnectionFactory $databaseConnectionFactory
7469
* @param bool $amPriorToConsentEnabled Is the run_all_manipulations_prior_to_consent feature enabled or not
75-
* @param bool $consentEnabled Is the feature_enable_consent feature enabled or not
76-
* @param ConsentHashService $hashService
7770
*/
7871
public function __construct(
79-
$tableName,
80-
$mustStoreValues,
72+
string $tableName,
73+
bool $mustStoreValues,
8174
EngineBlock_Saml2_ResponseAnnotationDecorator $response,
8275
array $responseAttributes,
8376
EngineBlock_Database_ConnectionFactory $databaseConnectionFactory,
84-
$amPriorToConsentEnabled,
85-
$consentEnabled,
86-
$hashService
87-
)
88-
{
77+
bool $amPriorToConsentEnabled,
78+
bool $consentEnabled,
79+
ConsentHashService $hashService
80+
) {
8981
$this->_tableName = $tableName;
9082
$this->_mustStoreValues = $mustStoreValues;
9183
$this->_response = $response;
@@ -120,24 +112,15 @@ public function giveImplicitConsentFor(ServiceProvider $serviceProvider): bool
120112
$this->_storeConsent($serviceProvider, ConsentType::TYPE_IMPLICIT);
121113
}
122114

123-
/**
124-
* @throws EngineBlock_Exception
125-
*/
126115
public function countTotalConsent(): int
127116
{
128117
$dbh = $this->_getConsentDatabaseConnection();
129-
$hashedUserId = sha1($this->_getConsentUid());
130-
$query = "SELECT COUNT(*) FROM consent where hashed_user_id = ?";
131-
$parameters = array($hashedUserId);
132-
$statement = $dbh->prepare($query);
133-
if (!$statement) {
134-
throw new EngineBlock_Exception(
135-
"Unable to create a prepared statement to count consent?!", EngineBlock_Exception::CODE_ALERT
136-
);
118+
if (!$dbh) {
119+
return 0;
137120
}
138-
/** @var $statement PDOStatement */
139-
$statement->execute($parameters);
140-
return (int)$statement->fetchColumn();
121+
122+
$consentUid = $this->_getConsentUid();
123+
return $this->_hashService->countTotalConsent($dbh, $consentUid);
141124
}
142125

143126
/**
@@ -174,32 +157,14 @@ private function _storeConsent(ServiceProvider $serviceProvider, $consentType):
174157
return false;
175158
}
176159

177-
$query = "INSERT INTO consent (hashed_user_id, service_id, attribute, consent_type, consent_date, deleted_at)
178-
VALUES (?, ?, ?, ?, NOW(), '0000-00-00 00:00:00')
179-
ON DUPLICATE KEY UPDATE attribute=VALUES(attribute), consent_type=VALUES(consent_type), consent_date=NOW()";
180160
$parameters = array(
181161
sha1($this->_getConsentUid()),
182162
$serviceProvider->entityId,
183163
$this->_getStableAttributesHash($this->_responseAttributes),
184164
$consentType,
185165
);
186166

187-
$statement = $dbh->prepare($query);
188-
if (!$statement) {
189-
throw new EngineBlock_Exception(
190-
"Unable to create a prepared statement to insert consent?!",
191-
EngineBlock_Exception::CODE_CRITICAL
192-
);
193-
}
194-
195-
/** @var $statement PDOStatement */
196-
if (!$statement->execute($parameters)) {
197-
throw new EngineBlock_Corto_Module_Services_Exception(
198-
sprintf('Error storing consent: "%s"', var_export($statement->errorInfo(), true)),
199-
EngineBlock_Exception::CODE_CRITICAL
200-
);
201-
}
202-
return true;
167+
return $this->_hashService->storeConsentHashInDb($dbh, $parameters);
203168
}
204169

205170
private function _hasStoredConsent(ServiceProvider $serviceProvider, $consentType): bool
@@ -209,53 +174,26 @@ private function _hasStoredConsent(ServiceProvider $serviceProvider, $consentTyp
209174
return false;
210175
}
211176

212-
$unstableConsentHash = $this->_getAttributesHash($this->_responseAttributes);
213-
$hasUnstableConsentHash = $this->retrieveConsentHashFromDb($dbh, $serviceProvider, $consentType, $unstableConsentHash);
177+
$parameters = array(
178+
sha1($this->_getConsentUid()),
179+
$serviceProvider->entityId,
180+
$this->_getAttributesHash($this->_responseAttributes),
181+
$consentType,
182+
);
183+
184+
$hasUnstableConsentHash = $this->_hashService->retrieveConsentHashFromDb($dbh, $parameters);
214185

215186
if ($hasUnstableConsentHash) {
216187
return true;
217188
}
218189

219-
$stableConsentHash = $this->_getStableAttributesHash($this->_responseAttributes);
220-
return $this->retrieveConsentHashFromDb($dbh, $serviceProvider, $consentType, $stableConsentHash);
221-
}
222-
223-
private function retrieveConsentHashFromDb(PDO $dbh, ServiceProvider $serviceProvider, $consentType, $attributesHash): bool
224-
{
225-
try {
226-
$query = "
227-
SELECT *
228-
FROM {$this->_tableName}
229-
WHERE hashed_user_id = ?
230-
AND service_id = ?
231-
AND attribute = ?
232-
AND consent_type = ?
233-
AND deleted_at IS NULL
234-
";
235-
$hashedUserId = sha1($this->_getConsentUid());
236-
$parameters = array(
237-
$hashedUserId,
238-
$serviceProvider->entityId,
239-
$attributesHash,
240-
$consentType,
241-
);
242-
243-
/** @var $statement PDOStatement */
244-
$statement = $dbh->prepare($query);
245-
$statement->execute($parameters);
246-
$rows = $statement->fetchAll();
247-
248-
if (count($rows) < 1) {
249-
// No stored consent found
250-
return false;
251-
}
190+
$parameters[2] = array(
191+
sha1($this->_getConsentUid()),
192+
$serviceProvider->entityId,
193+
$this->_getStableAttributesHash($this->_responseAttributes),
194+
$consentType,
195+
);
252196

253-
return true;
254-
} catch (PDOException $e) {
255-
throw new EngineBlock_Corto_ProxyServer_Exception(
256-
sprintf('Consent retrieval failed! Error: "%s"', $e->getMessage()),
257-
EngineBlock_Exception::CODE_ALERT
258-
);
259-
}
197+
return $this->_hashService->retrieveConsentHashFromDb($dbh, $parameters);
260198
}
261199
}

library/EngineBlock/Corto/Model/Consent/Factory.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
* limitations under the License.
1717
*/
1818

19+
use OpenConext\EngineBlock\Service\Consent\ConsentHashService;
20+
1921
/**
2022
* @todo write a test
2123
*/
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2010 SURFnet B.V.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
namespace OpenConext\EngineBlock\Service\Consent;
20+
21+
use EngineBlock_Corto_Module_Services_Exception;
22+
use EngineBlock_Corto_ProxyServer_Exception;
23+
use EngineBlock_Exception;
24+
use PDO;
25+
use PDOException;
26+
use PDOStatement;
27+
28+
class ConsentHashRepository
29+
{
30+
/**
31+
* @throws EngineBlock_Corto_ProxyServer_Exception
32+
*/
33+
public function retrieveConsentHashFromDb(PDO $dbh, array $parameters): bool
34+
{
35+
try {
36+
$query = "
37+
SELECT *
38+
FROM {$this->_tableName}
39+
WHERE hashed_user_id = ?
40+
AND service_id = ?
41+
AND attribute = ?
42+
AND consent_type = ?
43+
AND deleted_at IS NULL
44+
";
45+
/** @var $statement PDOStatement */
46+
$statement = $dbh->prepare($query);
47+
$statement->execute($parameters);
48+
$rows = $statement->fetchAll();
49+
50+
if (count($rows) < 1) {
51+
// No stored consent found
52+
return false;
53+
}
54+
55+
return true;
56+
} catch (PDOException $e) {
57+
throw new EngineBlock_Corto_ProxyServer_Exception(
58+
sprintf('Consent retrieval failed! Error: "%s"', $e->getMessage()),
59+
EngineBlock_Exception::CODE_ALERT
60+
);
61+
}
62+
}
63+
64+
/**
65+
* @throws EngineBlock_Corto_Module_Services_Exception
66+
* @throws EngineBlock_Exception
67+
*/
68+
public function storeConsentHashInDb(PDO $dbh, array $parameters): bool
69+
{
70+
$query = "INSERT INTO consent (hashed_user_id, service_id, attribute, consent_type, consent_date, deleted_at)
71+
VALUES (?, ?, ?, ?, NOW(), '0000-00-00 00:00:00')
72+
ON DUPLICATE KEY UPDATE attribute=VALUES(attribute), consent_type=VALUES(consent_type), consent_date=NOW()";
73+
74+
$statement = $dbh->prepare($query);
75+
if (!$statement) {
76+
throw new EngineBlock_Exception(
77+
"Unable to create a prepared statement to insert consent?!",
78+
EngineBlock_Exception::CODE_CRITICAL
79+
);
80+
}
81+
82+
/** @var $statement PDOStatement */
83+
if (!$statement->execute($parameters)) {
84+
throw new EngineBlock_Corto_Module_Services_Exception(
85+
sprintf('Error storing consent: "%s"', var_export($statement->errorInfo(), true)),
86+
EngineBlock_Exception::CODE_CRITICAL
87+
);
88+
}
89+
90+
return true;
91+
}
92+
93+
/**
94+
* @throws EngineBlock_Exception
95+
*/
96+
public function countTotalConsent(PDO $dbh, $consentUid): int
97+
{
98+
$query = "SELECT COUNT(*) FROM consent where hashed_user_id = ? AND deleted_at IS NULL";
99+
$parameters = array(sha1($consentUid));
100+
$statement = $dbh->prepare($query);
101+
if (!$statement) {
102+
throw new EngineBlock_Exception(
103+
"Unable to create a prepared statement to count consent?!",
104+
EngineBlock_Exception::CODE_ALERT
105+
);
106+
}
107+
/** @var $statement PDOStatement */
108+
$statement->execute($parameters);
109+
return (int)$statement->fetchColumn();
110+
}
111+
}

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

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

1919
namespace OpenConext\EngineBlock\Service\Consent;
2020

21+
use PDO;
2122
use function array_filter;
2223
use function array_keys;
2324
use function array_values;
@@ -34,7 +35,32 @@
3435

3536
final class ConsentHashService
3637
{
37-
public function getUnstableAttributesHash(array $attributes,bool $mustStoreValues): string
38+
/**
39+
* @var ConsentHashRepository
40+
*/
41+
private $consentHashRepository;
42+
43+
public function __construct(ConsentHashRepository $consentHashRepository)
44+
{
45+
$this->consentHashRepository = $consentHashRepository;
46+
}
47+
48+
public function retrieveConsentHashFromDb(PDO $dbh, array $parameters): bool
49+
{
50+
return $this->consentHashRepository->retrieveConsentHashFromDb($dbh, $parameters);
51+
}
52+
53+
public function storeConsentHashInDb(PDO $dbh, array $parameters): bool
54+
{
55+
return $this->consentHashRepository->storeConsentHashInDb($dbh, $parameters);
56+
}
57+
58+
public function countTotalConsent(PDO $dbh, $consentUid): int
59+
{
60+
return $this->consentHashRepository->countTotalConsent($dbh, $consentUid);
61+
}
62+
63+
public function getUnstableAttributesHash(array $attributes, bool $mustStoreValues): string
3864
{
3965
$hashBase = null;
4066
if ($mustStoreValues) {
@@ -51,7 +77,9 @@ public function getUnstableAttributesHash(array $attributes,bool $mustStoreValue
5177
public function getStableAttributesHash(array $attributes, bool $mustStoreValues) : string
5278
{
5379
$lowerCasedAttributes = $this->caseNormalizeStringArray($attributes);
54-
$hashBase = $mustStoreValues ? $this->createHashBaseWithValues($lowerCasedAttributes) : $this->createHashBaseWithoutValues($lowerCasedAttributes);
80+
$hashBase = $mustStoreValues
81+
? $this->createHashBaseWithValues($lowerCasedAttributes)
82+
: $this->createHashBaseWithoutValues($lowerCasedAttributes);
5583

5684
return sha1($hashBase);
5785
}
@@ -86,7 +114,7 @@ private function sortArray(array $sortMe): array
86114
$sortFunction = 'ksort';
87115
$copy = $this->removeEmptyAttributes($copy);
88116

89-
if($this->isSequentialArray($copy)){
117+
if ($this->isSequentialArray($copy)) {
90118
$sortFunction = 'sort';
91119
$copy = $this->renumberIndices($copy);
92120
}
@@ -126,7 +154,7 @@ private function removeEmptyAttributes(array $array): array
126154
$copy = unserialize(serialize($array));
127155

128156
foreach ($copy as $key => $value) {
129-
if ($this->is_blank($value)) {
157+
if ($this->isBlank($value)) {
130158
unset($copy[$key]);
131159
}
132160
}
@@ -141,7 +169,8 @@ private function removeEmptyAttributes(array $array): array
141169
* - "0"
142170
* @param $value array|string|integer|float
143171
*/
144-
private function is_blank($value): bool {
172+
private function isBlank($value): bool
173+
{
145174
return empty($value) && !is_numeric($value);
146175
}
147176
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,10 @@ services:
367367
tags:
368368
- { name: 'twig.extension' }
369369

370+
engineblock.service.consent.ConsentHashRepository:
371+
class: OpenConext\EngineBlock\Service\Consent\ConsentHashRepository
372+
370373
engineblock.service.consent.ConsentHashService:
371374
class: OpenConext\EngineBlock\Service\Consent\ConsentHashService
372-
public: false
375+
arguments:
376+
- "engineblock.service.consent.ConsentHashRepository"

0 commit comments

Comments
 (0)