Skip to content

Commit

Permalink
pkp#229 OrcidProfilePlugin-705
Browse files Browse the repository at this point in the history
  • Loading branch information
withanage committed Jan 22, 2023
1 parent 7dbbd98 commit d94d133
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 102 deletions.
246 changes: 149 additions & 97 deletions OrcidProfilePlugin.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
define('ORCID_PROFILE_URL', 'person');
define('ORCID_EMAIL_URL', 'email');
define('ORCID_WORK_URL', 'work');
define('ORCID_REVIEW_URL', 'peer-review');

use APP\core\Application;
use APP\decision\Decision;
Expand Down Expand Up @@ -130,6 +131,8 @@ public function register($category, $path, $mainContextId = null)

Hook::add('Publication::publish', [$this, 'handlePublicationStatusChange']);

Hook::add('ThankReviewerForm::thankReviewer', [$this, 'handleThankReviewer']);

// Add more ORCiD fields to author Schema
Hook::add('Schema::get::author', function ($hookName, $args) {
$schema = $args[0];
Expand Down Expand Up @@ -192,23 +195,21 @@ public function getHandlerPath()
return "{$this->getPluginPath()}/pages";
}


/**
* Hook callback: register pages for each sushi-lite method
* This URL is of the form: orcidapi/{$orcidrequest}
*
* @see PKPPageRouter::route()
* @param $hookName
* @param $args
*/
public function setupCallbackHandler($hookName, $params)
{
$page = $params[0];
if ($this->getEnabled() && $page == 'orcidapi') {
$this->import('pages/OrcidHandler');
define('HANDLER_CLASS', 'OrcidHandler');
return true;
function handleThankReviewer($hookName, $args) {
$request = PKPApplication::get()->getRequest();
$context = $request->getContext();
$newPublication =& $args[0];
if ($this->getSetting($context->getId(), 'country') && $this->getSetting($context->getId(), 'city')) {
$this->publishReviewerWorkToOrcid($newPublication, $request);
}
return false;
}


/**
* Load a setting for a specific journal or load it from the config.inc.php if it is specified there.
*
Expand All @@ -230,6 +231,12 @@ public function getSetting($contextId, $name)
case 'orcidClientSecret':
$config_value = Config::getVar('orcid', 'client_secret');
break;
case 'country':
$config_value = Config::getVar('orcid', 'country');
break;
case 'city':
$config_value = Config::getVar('orcid', 'city');
break;
default:
return parent::getSetting($contextId, $name);
}
Expand All @@ -238,19 +245,35 @@ public function getSetting($contextId, $name)
}

/**
* Check if there exist a valid orcid configuration section in the global config.inc.php of OJS.
* Hook callback: register pages for each sushi-lite method
* This URL is of the form: orcidapi/{$orcidrequest}
*
* @return bool True, if the config file has api_url, client_id and client_secret set in an [orcid] section
* @see PKPPageRouter::route()
*/
public function isGloballyConfigured()
public function setupCallbackHandler($hookName, $params)
{
$apiUrl = Config::getVar('orcid', 'api_url');
$clientId = Config::getVar('orcid', 'client_id');
$clientSecret = Config::getVar('orcid', 'client_secret');
return isset($apiUrl) && trim($apiUrl) && isset($clientId) && trim($clientId) &&
isset($clientSecret) && trim($clientSecret);
$page = $params[0];
if ($this->getEnabled() && $page == 'orcidapi') {
$this->import('pages/OrcidHandler');
define('HANDLER_CLASS', 'OrcidHandler');
return true;
}
return false;
}

/**
* Check if there exist a valid orcid configuration section in the global config.inc.php of OJS.
* @return boolean True, if the config file has api_url, client_id and client_secret set in an [orcid] section
*/
function isGloballyConfigured() {
$apiUrl = Config::getVar('orcid', 'api_url');
$clientId = Config::getVar('orcid', 'client_id');
$clientSecret = Config::getVar('orcid', 'client_secret');
return isset($apiUrl) && trim($apiUrl) && isset($clientId) && trim($clientId) &&
isset($clientSecret) && trim($clientSecret);
}


/**
* Hook callback to handle form display.
* Registers output filter for public user profile and author form.
Expand All @@ -265,6 +288,7 @@ public function isGloballyConfigured()
*/
public function handleFormDisplay($hookName, $args)
{
//TODO orcid
$request = Application::get()->getRequest();
$templateMgr = TemplateManager::getManager($request);
switch ($hookName) {
Expand Down Expand Up @@ -303,6 +327,7 @@ public function handleFormDisplay($hookName, $args)
*/
public function handleTemplateDisplay($hookName, $args)
{
//TODO orcid
$templateMgr = & $args[0];
$template = & $args[1];
$request = Application::get()->getRequest();
Expand All @@ -324,15 +349,7 @@ public function handleTemplateDisplay($hookName, $args)
return false;
}

/**
* Return the OAUTH path (prod or sandbox) based on the current API configuration
*
* @return string
*/
public function getOauthPath()
{
return $this->getOrcidUrl() . 'oauth/';
}


/**
* Return the ORCID website url (prod or sandbox) based on the current API configuration
Expand Down Expand Up @@ -389,6 +406,44 @@ public function buildOAuthUrl($handlerMethod, $redirectParams)
);
}

/**
* Return a string of the ORCiD SVG icon
*
* @return string
*/
function getIcon() {
$path = Core::getBaseDir() . '/' . $this->getPluginPath() . '/templates/images/orcid.svg';
return file_exists($path) ? file_get_contents($path) : '';
}
/**
* @return bool True if the ORCID Member API has been selected in this context.
*/
public function isMemberApiEnabled($contextId) {
$apiUrl = $this->getSetting($contextId, 'orcidProfileAPIPath');
if ($apiUrl === ORCID_API_URL_MEMBER || $apiUrl === ORCID_API_URL_MEMBER_SANDBOX) {
return true;
} else {
return false;
}
}

/**
* Return the OAUTH path (prod or sandbox) based on the current API configuration
*
* @return string
*/
public function getOauthPath()
{
return $this->getOrcidUrl() . 'oauth/';
}

public function isSandbox() {

$apiUrl = $this->getSetting($this->getCurrentContextId(), 'orcidProfileAPIPath');
return ($apiUrl == ORCID_API_URL_MEMBER_SANDBOX);

}

/**
* Output filter adds ORCiD interaction to registration form.
*
Expand Down Expand Up @@ -457,6 +512,7 @@ public function handleUserPublicProfileDisplay($hookName, $params)
return true;
}


/**
* Output filter adds ORCiD interaction to contributors metadata add/edit form.
*
Expand Down Expand Up @@ -508,6 +564,68 @@ public function handleAuthorFormExecute($hookname, $args)
}
}


/**
* Send mail with ORCID authorization link to the e-mail address of the supplied Author object.
*
* @param Author $author
* @param bool $updateAuthor If true update the author fields in the database.
* Use this only if not called from a function, which does this anyway.
*/
public function sendAuthorMail($author, $updateAuthor = false)
{
$request = Application::get()->getRequest();
$context = $request->getContext();

// This should only ever happen within a context, never site-wide.
if ($context != null) {
$contextId = $context->getId();
$publicationId = $author->getData('publicationId');
$publication = Repo::publication()->get($publicationId);
$submission = Repo::submission()->get($publication->getData('submissionId'));

$emailToken = md5(microtime() . $author->getEmail());
$author->setData('orcidEmailToken', $emailToken);
$oauthUrl = $this->buildOAuthUrl('orcidVerify', ['token' => $emailToken, 'publicationId' => $publicationId]);

if ($this->isMemberApiEnabled($contextId)) {
$mailable = new OrcidRequestAuthorAuthorization($context, $submission, $oauthUrl);
} else {
$mailable = new OrcidCollectAuthorId($context, $submission, $oauthUrl);
}

// Set From to primary journal contact
$mailable->from($context->getData('contactEmail'), $context->getData('contactName'));

// Send to author
$mailable->recipients([$author]);
Mail::send($mailable);

if ($updateAuthor) {
Repo::author()->dao->update($author);
}
}
}
/**
* Remove all data fields, which belong to an ORCID access token from the
* given Author object. Also updates fields in the db.
*
* @param Author $author object with ORCID access token
*/
public function removeOrcidAccessToken($author, $saveAuthor = true)
{
$author->setData('orcidAccessToken', null);
$author->setData('orcidAccessScope', null);
$author->setData('orcidRefreshToken', null);
$author->setData('orcidAccessExpiresOn', null);
$author->setData('orcidSandbox', null);

if ($saveAuthor) {
Repo::author()->dao->update($author);
}
}


/**
* Collect the ORCID when registering a user.
*
Expand Down Expand Up @@ -669,6 +787,8 @@ public function getActions($request, $actionArgs)
/**
* @see Plugin::manage()
*/


public function manage($args, $request)
{
switch ($request->getUserVar('verb')) {
Expand Down Expand Up @@ -726,47 +846,6 @@ public function getIcon()
return file_exists($path) ? file_get_contents($path) : '';
}

/**
* Send mail with ORCID authorization link to the e-mail address of the supplied Author object.
*
* @param Author $author
* @param bool $updateAuthor If true update the author fields in the database.
* Use this only if not called from a function, which does this anyway.
*/
public function sendAuthorMail($author, $updateAuthor = false)
{
$request = Application::get()->getRequest();
$context = $request->getContext();

// This should only ever happen within a context, never site-wide.
if ($context != null) {
$contextId = $context->getId();
$publicationId = $author->getData('publicationId');
$publication = Repo::publication()->get($publicationId);
$submission = Repo::submission()->get($publication->getData('submissionId'));

$emailToken = md5(microtime() . $author->getEmail());
$author->setData('orcidEmailToken', $emailToken);
$oauthUrl = $this->buildOAuthUrl('orcidVerify', ['token' => $emailToken, 'publicationId' => $publicationId]);

if ($this->isMemberApiEnabled($contextId)) {
$mailable = new OrcidRequestAuthorAuthorization($context, $submission, $oauthUrl);
} else {
$mailable = new OrcidCollectAuthorId($context, $submission, $oauthUrl);
}

// Set From to primary journal contact
$mailable->from($context->getData('contactEmail'), $context->getData('contactName'));

// Send to author
$mailable->recipients([$author]);
Mail::send($mailable);

if ($updateAuthor) {
Repo::author()->dao->update($author);
}
}
}

/**
* handlePublishIssue sends all submissions for which the authors hava an ORCID and access token
Expand Down Expand Up @@ -1265,24 +1344,6 @@ private function buildOrcidContributors($authors, $context, $publication)
return $contributors;
}

/**
* Remove all data fields, which belong to an ORCID access token from the
* given Author object. Also updates fields in the db.
*
* @param Author $author object with ORCID access token
*/
public function removeOrcidAccessToken($author, $saveAuthor = true)
{
$author->setData('orcidAccessToken', null);
$author->setData('orcidAccessScope', null);
$author->setData('orcidRefreshToken', null);
$author->setData('orcidAccessExpiresOn', null);
$author->setData('orcidSandbox', null);

if ($saveAuthor) {
Repo::author()->dao->update($author);
}
}

/**
* @return string Path to a custom ORCID log file.
Expand Down Expand Up @@ -1337,15 +1398,6 @@ public function setCurrentContextId($contextId)
$this->currentContextId = $contextId;
}

/**
* @return bool True if the ORCID Member API has been selected in this context.
*/
public function isMemberApiEnabled($contextId)
{
$apiUrl = $this->getSetting($contextId, 'orcidProfileAPIPath');
return in_array($apiUrl, [ORCID_API_URL_MEMBER, ORCID_API_URL_MEMBER_SANDBOX]);
}

/**
* Add mailable to the list of mailables in the application
*/
Expand Down
6 changes: 3 additions & 3 deletions emailTemplates.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
<!--
* emailTemplates.xml
*
* Copyright (c) 2015-2022 University of Pittsburgh
* Copyright (c) 2013-2022 Simon Fraser University
* Copyright (c) 2003-2022 John Willinsky
* Copyright (c) 2015-2019 University of Pittsburgh
* Copyright (c) 2013-2020 Simon Fraser University
* Copyright (c) 2003-2020 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* Email templates XML file.
Expand Down
4 changes: 2 additions & 2 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
* @file index.php
*
* Copyright (c) 2015-2019 University of Pittsburgh
* Copyright (c) 2014-2020 Simon Fraser University
* Copyright (c) 2003-2020 John Willinsky
* Copyright (c) 2014-2021 Simon Fraser University
* Copyright (c) 2003-2021 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @ingroup plugins_generic_orcidProfile
Expand Down

0 comments on commit d94d133

Please sign in to comment.