-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'notifyTokenExpiration330-736' into 'stable-3_3_0'
Notifica administrador quando token está próximo de expirar - 330 See merge request softwares-pkp/plugins_ojs/dataverse!173
- Loading branch information
Showing
9 changed files
with
200 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
import('lib.pkp.classes.scheduledTask.ScheduledTask'); | ||
import('lib.pkp.classes.mail.MailTemplate'); | ||
import('plugins.generic.dataverse.dataverseAPI.DataverseClient'); | ||
|
||
class NotifyDataverseTokenExpiration extends ScheduledTask | ||
{ | ||
public function executeActions() | ||
{ | ||
$dataverseClient = new DataverseClient(); | ||
$tokenExpirationDate = $dataverseClient->getDataverseCollectionActions()->getApiTokenExpirationDate(); | ||
|
||
if (empty($tokenExpirationDate)) { | ||
return false; | ||
} | ||
|
||
$momentsToSendNotification = ['4 weeks', '3 weeks', '2 weeks', '1 week', '1 day']; | ||
$today = date('Y-m-d'); | ||
|
||
foreach ($momentsToSendNotification as $moment) { | ||
$momentDate = date('Y-m-d', strtotime($tokenExpirationDate . " -$moment")); | ||
|
||
if ($today == $momentDate) { | ||
$this->sendNotificationEmail($dataverseClient, $tokenExpirationDate); | ||
break; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
private function sendNotificationEmail($dataverseClient, $tokenExpirationDate) | ||
{ | ||
$context = Application::get()->getRequest()->getContext(); | ||
|
||
$admin = $this->getAdminUser($context->getId()); | ||
if (!$admin) { | ||
return; | ||
} | ||
|
||
$email = new MailTemplate('DATAVERSE_TOKEN_EXPIRATION', null, $context, false); | ||
$email->setFrom($context->getData('contactEmail'), $context->getData('contactName')); | ||
$email->setRecipients([['name' => $admin->getFullName(), 'email' => $admin->getEmail()]]); | ||
|
||
$dataverseCollection = $dataverseClient->getDataverseCollectionActions()->get(); | ||
$email->sendWithParams([ | ||
'contextName' => $context->getLocalizedName(), | ||
'dataverseName' => $dataverseCollection->getName(), | ||
'keyExpirationDate' => $tokenExpirationDate | ||
]); | ||
} | ||
|
||
private function getAdminUser($contextId) | ||
{ | ||
$applicationName = Application::get()->getName(); | ||
$adminEnAbbrev = ($applicationName == 'ojs2' ? 'jm' : 'psm'); | ||
|
||
$adminUserGroup = $this->getUserGroupByAbbrev($contextId, $adminEnAbbrev); | ||
if (!$adminUserGroup) { | ||
return null; | ||
} | ||
|
||
$adminUsers = Services::get('user')->getMany([ | ||
'contextId' => $contextId, | ||
'userGroupIds' => [$adminUserGroup->getId()] | ||
]); | ||
|
||
return $adminUsers->current(); | ||
} | ||
|
||
private function getUserGroupByAbbrev(int $contextId, string $abbrev) | ||
{ | ||
$contextUserGroups = DAORegistry::getDAO('UserGroupDAO')->getByContextId($contextId)->toArray(); | ||
|
||
foreach ($contextUserGroups as $userGroup) { | ||
$userGroupAbbrev = strtolower($userGroup->getData('abbrev', 'en_US')); | ||
|
||
if ($userGroupAbbrev === $abbrev) { | ||
return $userGroup; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<!DOCTYPE scheduled_tasks SYSTEM "../../../lib/pkp/dtd/scheduledTasks.dtd"> | ||
|
||
<scheduled_tasks> | ||
<task class="plugins.generic.dataverse.classes.tasks.NotifyDataverseTokenExpiration"> | ||
<descr>Notify the administrator when the Dataverse Token is close to expiration</descr> | ||
<frequency hour="3"/> | ||
</task> | ||
</scheduled_tasks> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters