Skip to content

Commit

Permalink
[GT-184] Fix displaying months and dryrun issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Sae126V committed Sep 14, 2023
1 parent cd82c9e commit 0d1ec9a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
6 changes: 3 additions & 3 deletions lib/Gocdb_Services/APIAuthenticationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ private function validate($data, $identifier, $type)
* @param \APIAuthentication $authEntity Entity to update.
* @param \User $user Owning user.
* @param bool $isRenewalRequest A boolean indicating
* if it's a renewal request.
* if it's a renewal request or NOT.
*/
private function handleRenewalRequest(
\APIAuthentication $authEntity,
Expand All @@ -302,7 +302,7 @@ private function handleRenewalRequest(
* @param array $newValues An array containing data for
* updating the APIAuthentication entity.
* @param bool $isRenewalRequest A boolean indicating
* if it's a renewal request.
* if it's a renewal request or NOT.
*
* @throws \Exception Throws an exception if the identifier is empty.
*/
Expand Down Expand Up @@ -347,7 +347,7 @@ private function handleEditRequest(
* @param \APIAuthentication $authEntity Entity to update.
* @param \User $user Owning user.
* @param bool $isRenewalRequest A boolean indicating
* if it's a renewal request.
* if it's a renewal request or NOT.
*/
private function updateLastRenewTime(
\APIAuthentication $authEntity,
Expand Down
41 changes: 33 additions & 8 deletions resources/ManageAPICredentials/ManageAPICredentialsActions.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ public function getCreds($threshold, $propertyName)
* @param int $deleteThreshold The number of months of no-use which will trigger deletion
* @param bool $isRenewalRequest Flag indicating the
* presence or absence of the
* `r` command-line argument.
* `r` or `renewals`
* command-line argument.
* @return array Credentials which were not deleted.
*/
public function deleteCreds($creds, $deleteThreshold, $isRenewalRequest)
Expand Down Expand Up @@ -103,7 +104,7 @@ public function deleteCreds($creds, $deleteThreshold, $isRenewalRequest)
}
}
if ($this->dryRun) {
$this->reportDryRun($deletedCreds, "deleting");
$this->reportDryRun($deletedCreds, "deleting", $isRenewalRequest);
}

return array_udiff($creds, $deletedCreds, array($this, 'compareCredIds'));
Expand All @@ -123,8 +124,9 @@ public function deleteCreds($creds, $deleteThreshold, $isRenewalRequest)
* @param int $deleteThreshold The number of months of no-use which will trigger deletion
* @param string $fromEmail Email address to use as sender's (From:) address
* @param string $replyToEmail Email address for replies (Reply-To:)
* @param bool $isRenewalRequest Flag indicating the presence
* or absence of the `r`
* @param bool $isRenewalRequest Flag indicating the
* presence or absence of the
* `r` or `renewals`
* command-line argument.
* @return array [] An Array of credentials identifed
* for sending warning emails.
Expand Down Expand Up @@ -154,8 +156,8 @@ public function warnUsers(
$lastUseOrRenewTime = $isRenewalRequest
? $apiCred->getLastRenewTime()
: $apiCred->getLastUseTime();
$elapsedMonths = $this->baseTime->diff($lastUseOrRenewTime)
->format('%m');
$diffTime = $this->baseTime->diff($lastUseOrRenewTime);
$elapsedMonths = ($diffTime->y * 12) + $diffTime->m;

if (!$this->dryRun) {
$this->sendWarningEmail(
Expand All @@ -173,7 +175,11 @@ public function warnUsers(
}

if ($this->dryRun) {
$this->reportDryRun($warnedCreds, "sending warning emails");
$this->reportDryRun(
$warnedCreds,
"sending warning emails",
$isRenewalRequest
);
}

return array_udiff($creds, $warnedCreds, array($this, 'compareCredIds'));
Expand Down Expand Up @@ -277,9 +283,12 @@ private function sendWarningEmail(
* @param array $creds Array of API credential objects to be summarised.
* @param string $text Brief description of the operation which would have been
* performed without dry-run to be included in the report.
* @param bool $isRenewalRequest Flag indicating the presence or absence
* of the `r` or `renewals`
* command-line argument.
* @return void
*/
private function reportDryRun(array $creds, $text)
private function reportDryRun(array $creds, $text, $isRenewalRequest)
{
if (count($creds) == 0) {
print("Dry run: No matching credentials found for $text.\n");
Expand All @@ -289,6 +298,22 @@ private function reportDryRun(array $creds, $text)
print("Dry run: Found " . count($creds) . " credentials for $text.\n");

foreach ($creds as $api) {
if ($isRenewalRequest) {
print(
"Dry run: Processing credential id "
. $api->getId() . "\n"
. " Identifier: " . $api->getIdentifier() . "\n"
. " User email: "
. $api->getUser()->getEmail() . "\n"
. " Site: "
. $api->getParentSite()->getShortName() . "\n"
. " Last Renewed: "
. $api->getLastRenewTime()->format("Y-m-d\\TH:i:sO") . "\n"
);

continue;
}

print("Dry run: Processing credential id " . $api->getId() . "\n" .
" Identifier: " . $api->getIdentifier() . "\n" .
" User email: " . $api->getUser()->getEmail() . "\n" .
Expand Down

0 comments on commit 0d1ec9a

Please sign in to comment.