Skip to content
This repository was archived by the owner on Sep 19, 2022. It is now read-only.

Commit 7ef157e

Browse files
author
Dominik Frantisek Bucik
committed
fix: Refactor ForcAup filter
- only one of perunAups and voAups attr is needed to be configured - the latest AUP from both voAups and perunAups is forced to be signed - naming adjustments
1 parent 3a01054 commit 7ef157e

File tree

1 file changed

+105
-72
lines changed

1 file changed

+105
-72
lines changed

lib/Auth/Process/ForceAup.php

Lines changed: 105 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ class ForceAup extends ProcessingFilter
4444

4545
public const PERUN_FACILITY_VO_SHORT_NAMES_ATTR = 'perunFacilityVoShortNamesAttr';
4646

47+
private const DATETIME_FORMAT = 'Y-m-d';
48+
4749
private $uidAttr;
4850

4951
private $perunAupsAttr;
@@ -52,7 +54,7 @@ class ForceAup extends ProcessingFilter
5254

5355
private $perunVoAupAttr;
5456

55-
private $perunFacilityReqAupsAttr;
57+
private $perunFacilityRequestedAupsAttr;
5658

5759
private $perunFacilityVoShortNames;
5860

@@ -70,33 +72,31 @@ public function __construct($config, $reserved)
7072
'perun:ForceAup: missing mandatory configuration option \'' . self::UID_ATTR . '\'.'
7173
);
7274
}
73-
if (! isset($config[self::PERUN_AUPS_ATTR])) {
75+
if (! isset($config[self::PERUN_AUPS_ATTR]) && ! isset($config[self::PERUN_VO_AUP_ATTR])) {
7476
throw new Exception(
75-
'perun:ForceAup: missing mandatory configuration option \'' . self::PERUN_AUPS_ATTR . '\'.'
77+
'perun:ForceAup: missing at least one of mandatory configuration options \''
78+
. self::PERUN_AUPS_ATTR . '\' or \'' . self::PERUN_VO_AUP_ATTR . '\'.'
7679
);
7780
}
7881
if (! isset($config[self::PERUN_USER_AUP_ATTR])) {
7982
throw new Exception(
8083
'perun:ForceAup: missing mandatory configuration option \'' . self::PERUN_USER_AUP_ATTR . '\'.'
8184
);
8285
}
83-
if (! isset($config[self::PERUN_VO_AUP_ATTR])) {
84-
throw new Exception(
85-
'perun:ForceAup: missing mandatory configuration option \'' . self::PERUN_VO_AUP_ATTR . '\'.'
86-
);
87-
}
8886
if (! isset($config[self::INTERFACE_PROPNAME])) {
8987
$config[self::INTERFACE_PROPNAME] = Adapter::RPC;
9088
}
9189

9290
$this->uidAttr = (string) $config[self::UID_ATTR];
93-
$this->perunAupsAttr = (string) $config[self::PERUN_AUPS_ATTR];
91+
$this->perunAupsAttr = isset($config[self::PERUN_AUPS_ATTR]) ?
92+
(string) $config[self::PERUN_AUPS_ATTR] : null;
93+
$this->perunVoAupAttr = isset($config[self::PERUN_VO_AUP_ATTR]) ?
94+
(string) $config[self::PERUN_VO_AUP_ATTR] : null;
9495
$this->perunUserAupAttr = (string) $config[self::PERUN_USER_AUP_ATTR];
95-
$this->perunVoAupAttr = (string) $config[self::PERUN_VO_AUP_ATTR];
9696
$interface = (string) $config[self::INTERFACE_PROPNAME];
9797
$this->adapter = Adapter::getInstance($interface);
9898

99-
$this->perunFacilityReqAupsAttr = (string) $config[self::PERUN_FACILITY_REQ_AUPS_ATTR];
99+
$this->perunFacilityRequestedAupsAttr = (string) $config[self::PERUN_FACILITY_REQ_AUPS_ATTR];
100100
$this->perunFacilityVoShortNames = (string) $config[self::PERUN_FACILITY_VO_SHORT_NAMES_ATTR];
101101
}
102102

@@ -129,18 +129,18 @@ public function process(&$request)
129129
return;
130130
}
131131

132-
$requiredAups = [];
132+
$requestedAups = [];
133133
$voShortNames = [];
134134

135135
$facilityAttrValues = $this->adapter->getFacilityAttributesValues(
136136
$facility,
137-
[$this->perunFacilityReqAupsAttr, $this->perunFacilityVoShortNames]
137+
[$this->perunFacilityRequestedAupsAttr, $this->perunFacilityVoShortNames]
138138
);
139139

140-
if (isset($this->perunFacilityReqAupsAttr, $facilityAttrValues) &&
141-
is_array($facilityAttrValues[$this->perunFacilityReqAupsAttr])) {
142-
foreach (array_values($facilityAttrValues[$this->perunFacilityReqAupsAttr]) as $facilityAup) {
143-
array_push($requiredAups, $facilityAup);
140+
if (isset($this->perunFacilityRequestedAupsAttr, $facilityAttrValues) &&
141+
is_array($facilityAttrValues[$this->perunFacilityRequestedAupsAttr])) {
142+
foreach (array_values($facilityAttrValues[$this->perunFacilityRequestedAupsAttr]) as $facilityAup) {
143+
array_push($requestedAups, $facilityAup);
144144
}
145145
}
146146

@@ -151,21 +151,14 @@ public function process(&$request)
151151
}
152152
}
153153

154-
if (empty($requiredAups) && empty($voShortNames)) {
154+
if (empty($requestedAups) && empty($voShortNames)) {
155155
Logger::debug(
156-
'Perun.ForceAup - No required Aups for facility with EntityId: ' .
156+
'Perun.ForceAup - No AUPs to be approved have been requested by facility with EntityId: ' .
157157
$request['SPMetadata']['entityid']
158158
);
159159
return;
160160
}
161161

162-
$perunAupsAttr = $this->adapter->getEntitylessAttribute($this->perunAupsAttr);
163-
164-
$perunAups = [];
165-
foreach ($perunAupsAttr as $key => $attr) {
166-
$perunAups[$key] = $attr['value'];
167-
}
168-
169162
$userAups = $this->adapter->getUserAttributesValues(
170163
$user,
171164
[$this->perunUserAupAttr]
@@ -175,55 +168,21 @@ public function process(&$request)
175168
$userAups = [];
176169
}
177170

171+
$perunAups = $this->getPerunAups();
178172
$voAups = $this->getVoAups($voShortNames);
179173

180-
$newAups = [];
181-
182-
if (! empty($perunAups)) {
183-
foreach ($requiredAups as $requiredAup) {
184-
$aups = json_decode($perunAups[$requiredAup]);
185-
$latest_aup = $this->getLatestAup($aups);
186-
187-
if (array_key_exists($requiredAup, $userAups)) {
188-
$userAupsList = json_decode($userAups[$requiredAup]);
189-
$userLatestAup = $this->getLatestAup($userAupsList);
190-
191-
if ($latest_aup->date === $userLatestAup->date) {
192-
continue;
193-
}
194-
}
195-
$newAups[$requiredAup] = $latest_aup;
196-
}
197-
}
198-
199-
if (! empty($voAups)) {
200-
foreach ($voAups as $voShortName => $voAup) {
201-
$voAupsList = json_decode($voAup);
202-
$latest_aup = $this->getLatestAup($voAupsList);
203-
204-
if (array_key_exists($voShortName, $userAups)) {
205-
$userAupsList = json_decode($userAups[$voShortName]);
206-
$userLatestAup = $this->getLatestAup($userAupsList);
207-
208-
if ($latest_aup->date === $userLatestAup->date) {
209-
continue;
210-
}
211-
}
212-
213-
$newAups[$voShortName] = $latest_aup;
214-
}
215-
}
174+
$aupsToBeApproved = $this->getAupsToBeApproved($perunAups, $voAups, $requestedAups, $userAups);
216175
} catch (\Exception $ex) {
217176
Logger::warning('perun:ForceAup - ' . $ex->getMessage());
218-
$newAups = [];
177+
$aupsToBeApproved = [];
219178
}
220179

221-
Logger::debug('perun:ForceAup - NewAups: ' . json_encode($newAups));
180+
Logger::debug('perun:ForceAup - NewAups: ' . json_encode($aupsToBeApproved));
222181

223-
if (! empty($newAups)) {
182+
if (! empty($aupsToBeApproved)) {
224183
$request[self::UID_ATTR] = $this->uidAttr;
225184
$request[self::PERUN_USER_AUP_ATTR] = $this->perunUserAupAttr;
226-
$request['newAups'] = $newAups;
185+
$request['newAups'] = $aupsToBeApproved;
227186
$id = State::saveState($request, 'perun:forceAup');
228187
$url = Module::getModuleURL('perun/force_aup_page.php');
229188
HTTP::redirectTrustedURL($url, [
@@ -236,22 +195,25 @@ public function process(&$request)
236195
* @param array $aups
237196
* @return aup with the latest date
238197
*/
239-
public function getLatestAup(&$aups)
198+
public function getLatestAup($aups)
240199
{
241-
$latest_aup = $aups[0];
200+
$latestAup = $aups[0];
201+
$latestDate = \DateTime::createFromFormat(self::DATETIME_FORMAT, $latestAup->date);
242202
foreach ($aups as $aup) {
243-
if (new \DateTime($latest_aup->date) < new \DateTime($aup->date)) {
244-
$latest_aup = $aup;
203+
$aupDate = \DateTime::createFromFormat(self::DATETIME_FORMAT, $aup->date);
204+
if ($latestDate < $aupDate) {
205+
$latestAup = $aup;
206+
$latestDate = $aupDate;
245207
}
246208
}
247-
return $latest_aup;
209+
return $latestAup;
248210
}
249211

250212
/**
251213
* @param string[] $voShortNames
252214
* @return array
253215
*/
254-
public function getVoAups(&$voShortNames)
216+
public function getVoAups($voShortNames)
255217
{
256218
$vos = [];
257219
foreach ($voShortNames as $voShortName) {
@@ -271,4 +233,75 @@ public function getVoAups(&$voShortNames)
271233

272234
return $voAups;
273235
}
236+
237+
private function getPerunAups()
238+
{
239+
$perunAupsAttr = [];
240+
if ($this->perunAupsAttr !== null) {
241+
$perunAupsAttr = $this->adapter->getEntitylessAttribute($this->perunAupsAttr);
242+
}
243+
244+
$perunAups = [];
245+
foreach ($perunAupsAttr as $key => $attr) {
246+
$perunAups[$key] = $attr['value'];
247+
}
248+
return $perunAups;
249+
}
250+
251+
private function getAupsToBeApproved($perunAups, $voAups, $requestedAups, $userAups)
252+
{
253+
$perunAupsToBeApproved = [];
254+
if (! empty($perunAups)) {
255+
$perunAupsToBeApproved = $this->fillAupsToBeApproved($requestedAups, $perunAups, $userAups);
256+
}
257+
258+
$voAupsToBeApproved = [];
259+
if (! empty($voAups)) {
260+
$voAupsToBeApproved = $this->fillAupsToBeApproved($requestedAups, $voAups, $userAups);
261+
}
262+
return $this->mergeAupsToBeApproved($perunAupsToBeApproved, $voAupsToBeApproved);
263+
}
264+
265+
private function fillAupsToBeApproved($requestedAups, $aups, $userApprovedAups)
266+
{
267+
$aupsToBeApproved = [];
268+
foreach ($requestedAups as $requestedAup) {
269+
$decodedAups = json_decode($aups[$requestedAup]);
270+
$latestAup = $this->getLatestAup($decodedAups);
271+
272+
if (array_key_exists($requestedAup, $userApprovedAups)) {
273+
$userAupsList = json_decode($userApprovedAups[$requestedAup]);
274+
$userLatestAup = $this->getLatestAup($userAupsList);
275+
$latestDate = \DateTime::createFromFormat(self::DATETIME_FORMAT, $latestAup->date);
276+
$userLatestDate = \DateTime::createFromFormat(self::DATETIME_FORMAT, $userLatestAup->date);
277+
if ($userLatestDate >= $latestDate) {
278+
continue;
279+
}
280+
}
281+
$aupsToBeApproved[$requestedAup] = $latestAup;
282+
}
283+
return $aupsToBeApproved;
284+
}
285+
286+
private function mergeAupsToBeApproved(array $perunAupsToBeApproved, array $voAupsToBeApproved)
287+
{
288+
$resultAups = $perunAupsToBeApproved;
289+
foreach ($voAupsToBeApproved as $aupKey => $voAup) {
290+
if (array_key_exists($aupKey, $resultAups)) {
291+
$voLatestDate = \DateTime::createFromFormat(self::DATETIME_FORMAT, $voAup->date);
292+
$perunLatestDate = \DateTime::createFromFormat(
293+
self::DATETIME_FORMAT,
294+
$perunAupsToBeApproved[$aupKey]->date
295+
);
296+
if ($voLatestDate >= $perunLatestDate) {
297+
$resultAups[$aupKey] = $voLatestDate;
298+
} else {
299+
$resultAups[$aupKey] = $perunLatestDate;
300+
}
301+
} else {
302+
$resultAups[$aupKey] = $voAup;
303+
}
304+
}
305+
return $resultAups;
306+
}
274307
}

0 commit comments

Comments
 (0)