Skip to content

Commit

Permalink
pkp#229 OrcidProfilePlugin
Browse files Browse the repository at this point in the history
  • Loading branch information
withanage committed Feb 8, 2023
1 parent 7dbbd98 commit 8d66d49
Show file tree
Hide file tree
Showing 15 changed files with 808 additions and 401 deletions.
161 changes: 89 additions & 72 deletions pages/OrcidHandler.inc.php → OrcidProfileHandler.php
Original file line number Diff line number Diff line change
@@ -1,33 +1,44 @@
<?php

/**
* @file pages/OrcidHandler.inc.php
* @file OrcidProfileHandler.inc.php
*
* Copyright (c) 2015-2019 University of Pittsburgh
* Copyright (c) 2014-2020 Simon Fraser University
* Copyright (c) 2003-2020 John Willinsky
* Distributed under the GNU GPL v2 or later. For full terms see the file docs/COPYING.
*
* @class OrcidHandler
* @class OrcidProfileHandler
* @ingroup plugins_generic_orcidprofile
*
* @brief Pass off internal ORCID API requests to ORCID
*/

namespace APP\plugins\generic\orcidProfile;


use APP\core\Application;
use APP\core\Request;
use APP\facades\Repo;
use APP\handler\Handler;
use APP\template\TemplateManager;
use Carbon\Carbon;
use PKP\core\Core;
use PKP\plugins\PluginRegistry;
use PKP\security\authorization\PKPSiteAccessPolicy;
use PKP\security\authorization\UserRequiredPolicy;
use PKP\session\SessionManager;
use PKP\submission\PKPSubmission;

class OrcidHandler extends Handler
class OrcidProfileHandler extends Handler
{
public const TEMPLATE = 'orcidVerify.tpl';
const ORCIDPROFILEPLUGIN = 'orcidprofileplugin';


public function __construct()
{
}

/**
* @copydoc PKPHandler::authorize()
Expand All @@ -45,7 +56,6 @@ public function authorize($request, &$args, $roleAssignments)
$targetOp = $request->getUserVar('targetOp');
if ($op === 'orcidAuthorize' && in_array($targetOp, ['profile', 'submit'])) {
// ... but user must be logged in for orcidAuthorize with profile or submit
import('lib.pkp.classes.security.authorization.UserRequiredPolicy');
$this->addPolicy(new UserRequiredPolicy($request));
}

Expand All @@ -68,7 +78,7 @@ public function orcidAuthorize($args, $request)
{
$context = $request->getContext();
$op = $request->getRequestedOp();
$plugin = PluginRegistry::getPlugin('generic', 'orcidprofileplugin');
$plugin = PluginRegistry::getPlugin('generic', self::ORCIDPROFILEPLUGIN);
$contextId = ($context == null) ? \PKP\core\PKPApplication::CONTEXT_ID_NONE : $context->getId();
$httpClient = Application::get()->getHttpClient();

Expand Down Expand Up @@ -128,7 +138,7 @@ public function orcidAuthorize($args, $request)
]
);
if ($response->getStatusCode() != 200) {
error_log('ORCID employments URL error: ' . $response->getStatusCode() . ' (' . __FILE__ . ' line ' . __LINE__ . ', URL ' . $url . ')');
error_log('ORCID deployments URL error: ' . $response->getStatusCode() . ' (' . __FILE__ . ' line ' . __LINE__ . ', URL ' . $url . ')');
$employmentJson = null;
} else {
$employmentJson = json_decode($response->getBody(), true);
Expand Down Expand Up @@ -162,7 +172,8 @@ public function orcidAuthorize($args, $request)
</script></body></html>
';
break;
default: assert(false);
default:
assert(false);
}
}

Expand All @@ -177,17 +188,19 @@ public function orcidVerify($args, $request)
$templateMgr = TemplateManager::getManager($request);
$context = $request->getContext();
$contextId = $context == null ? \PKP\core\PKPApplication::CONTEXT_ID_NONE : $context->getId();
$plugin = PluginRegistry::getPlugin('generic', self::ORCIDPROFILEPLUGIN);

$plugin = PluginRegistry::getPlugin('generic', 'orcidprofileplugin');
$templatePath = $plugin->getTemplateResource(self::TEMPLATE);


$publicationId = $request->getUserVar('publicationId');
$publicationId = $request->getUserVar('state');
$authors = Repo::author()
->getCollector()
->filterByPublicationIds([$publicationId])
->getMany();

$isSandBox = $plugin->getSetting($contextId, 'orcidProfileAPIPath') == ORCID_API_URL_MEMBER_SANDBOX ||
$plugin->getSetting($contextId, 'orcidProfileAPIPath') == ORCID_API_URL_PUBLIC_SANDBOX;
$publication = Repo::publication()->get($publicationId);

$authorToVerify = null;
Expand All @@ -213,7 +226,7 @@ public function orcidVerify($args, $request)

if ($authorToVerify == null) {
// no Author exists in the database with the supplied orcidEmailToken
$plugin->logError('OrcidHandler::orcidverify - No author found with supplied token');
$plugin->logError('OrcidProfileHandler::orcidverify - No author found with supplied token');
$templateMgr->assign('verifySuccess', false);
$templateMgr->display($templatePath);
return;
Expand All @@ -230,7 +243,7 @@ public function orcidVerify($args, $request)
$authorToVerify->setData('orcidAccessExpiresOn', null);
$authorToVerify->setData('orcidEmailToken', null);
Repo::author()->dao->update($authorToVerify);
$plugin->logError('OrcidHandler::orcidverify - ORCID access denied. Error description: ' . $request->getUserVar('error_description'));
$plugin->logError('OrcidProfileHandler::orcidverify - ORCID access denied. Error description: ' . $request->getUserVar('error_description'));
$templateMgr->assign('denied', true);
$templateMgr->display($templatePath);
return;
Expand All @@ -251,80 +264,82 @@ public function orcidVerify($args, $request)
$plugin->logInfo('POST ' . $url);
$plugin->logInfo('Request header: ' . var_export($header, true));
$plugin->logInfo('Request body: ' . http_build_query($postData));
try {
$response = $httpClient->request(
'POST',
$url,
[
'headers' => $header,
'form_params' => $postData,
]
);
if ($response->getStatusCode() != 200) {
$plugin->logError('OrcidProfileHandler::orcidverify - unexpected response: ' . $response->getStatusCode());
$templateMgr->assign('authFailure', true);

$response = $httpClient->request(
'POST',
$url,
[
'headers' => $header,
'form_params' => $postData,
]
);
if ($response->getStatusCode() != 200) {
$plugin->logError('OrcidHandler::orcidverify - unexpected response: ' . $response->getStatusCode());
$templateMgr->assign('authFailure', true);
$templateMgr->display($templatePath);
return;
}
$response = json_decode($response->getBody(), true);
}
$response = json_decode($response->getBody(), true);

$plugin->logInfo('Response body: ' . print_r($response, true));
if (($response['error'] ?? null) === 'invalid_grant') {
$plugin->logError('Authorization code invalid, maybe already used');
$templateMgr->assign('authFailure', true);
$templateMgr->display($templatePath);
return;
}
if (isset($response['error'])) {
$plugin->logError("Invalid ORCID response: " . $response['error']);
$templateMgr->assign('authFailure', true);
$templateMgr->display($templatePath);
}
// Set the orcid id using the full https uri
$orcidUri = ($plugin->getSetting($contextId, 'isSandBox') == true ? ORCID_URL_SANDBOX : ORCID_URL) . $response['orcid'];
if (!empty($authorToVerify->getOrcid()) && $orcidUri != $authorToVerify->getOrcid()) {
// another ORCID id is stored for the author
$templateMgr->assign('duplicateOrcid', true);
$templateMgr->display($templatePath);
return;
}
$authorToVerify->setOrcid($orcidUri);
if (in_array($plugin->getSetting($contextId, 'orcidProfileAPIPath'), [ORCID_API_URL_MEMBER_SANDBOX, ORCID_API_URL_PUBLIC_SANDBOX])) {
// Set a flag to mark that the stored orcid id and access token came form the sandbox api
$authorToVerify->setData('orcidSandbox', true);
$templateMgr->assign('orcid', ORCID_URL_SANDBOX . $response['orcid']);
} else {
$templateMgr->assign('orcid', $orcidUri);
}
$plugin->logInfo('Response body: ' . print_r($response, true));
if (($response['error'] ?? null) === 'invalid_grant') {
$plugin->logError('Authorization code invalid, maybe already used');
$templateMgr->assign('authFailure', true);

// remove the email token
$authorToVerify->setData('orcidEmailToken', null);
$this->_setOrcidData($authorToVerify, $orcidUri, $response);
Repo::author()->dao->update($authorToVerify);
if ($plugin->isMemberApiEnabled($contextId)) {
if ($publication->getData('status') == PKPSubmission::STATUS_PUBLISHED) {
$templateMgr->assign('sendSubmission', true);
$sendResult = $plugin->sendSubmissionToOrcid($publication, $request);
if ($sendResult === true || (is_array($sendResult) && $sendResult[$response['orcid']])) {
$templateMgr->assign('sendSubmissionSuccess', true);
}
if (isset($response['error'])) {
$plugin->logError("Invalid ORCID response: " . $response['error']);
$templateMgr->assign('authFailure', true);
}
// Set the orcid id using the full https uri
$orcidUri = ($plugin->getSetting($contextId, 'isSandBox') == true ? ORCID_URL_SANDBOX : ORCID_URL) . $response['orcid'];
if (!empty($authorToVerify->getOrcid()) && $orcidUri != $authorToVerify->getOrcid()) {
// another ORCID id is stored for the author
$templateMgr->assign('duplicateOrcid', true);
}
$authorToVerify->setOrcid($orcidUri);
if (in_array($plugin->getSetting($contextId, 'orcidProfileAPIPath'), [ORCID_API_URL_MEMBER_SANDBOX, ORCID_API_URL_PUBLIC_SANDBOX])) {
// Set a flag to mark that the stored orcid id and access token came form the sandbox api
$authorToVerify->setData('orcidSandbox', true);
$templateMgr->assign('orcid', ORCID_URL_SANDBOX . $response['orcid']);
} else {
$templateMgr->assign('submissionNotPublished', true);
$templateMgr->assign('orcid', $orcidUri);
}
}

$templateMgr->assign([
'verifySuccess' => true,
'orcidIcon' => $plugin->getIcon()
]);
// remove the email token
$authorToVerify->setData('orcidEmailToken', null);
$this->_setOrcidData($authorToVerify, $orcidUri, $response);
Repo::author()->dao->update($authorToVerify);
if ($plugin->isMemberApiEnabled($contextId)) {
if ($publication->getData('status') == PKPSubmission::STATUS_PUBLISHED) {
$templateMgr->assign('sendSubmission', true);
$sendResult = $plugin->sendSubmissionToOrcid($publication, $request);
if ($sendResult === true || (is_array($sendResult) && $sendResult[$response['orcid']])) {
$templateMgr->assign('sendSubmissionSuccess', true);
}
} else {
$templateMgr->assign('submissionNotPublished', true);
}
}

$templateMgr->assign([
'verifySuccess' => true,
'orcidIcon' => $plugin->getIcon()
]);


} catch (\GuzzleHttp\Exception\ClientException $exception) {
$reason = $exception->getResponse()->getBody(false);
$plugin->logInfo("Publication fail: ${reason}");

}
$templateMgr->assign('authFailure', true);
$templateMgr->display($templatePath);
}

public function _setOrcidData($userOrAuthor, $orcidUri, $orcidResponse)
{
// Save the access token
$orcidAccessExpiresOn = Carbon\Carbon::now();
$orcidAccessExpiresOn = Carbon::now();
// expires_in field from the response contains the lifetime in seconds of the token
// See https://members.orcid.org/api/get-oauthtoken
$orcidAccessExpiresOn->addSeconds($orcidResponse['expires_in']);
Expand All @@ -348,9 +363,11 @@ public function about($args, $request)
$context = $request->getContext();
$contextId = $context == null ? \PKP\core\PKPApplication::CONTEXT_ID_NONE : $context->getId();
$templateMgr = TemplateManager::getManager($request);
$plugin = PluginRegistry::getPlugin('generic', 'orcidprofileplugin');
$plugin = PluginRegistry::getPlugin('generic', self::ORCIDPROFILEPLUGIN);
$templateMgr->assign('orcidIcon', $plugin->getIcon());
$templateMgr->assign('isMemberApi', $plugin->isMemberApiEnabled($contextId));
$templateMgr->display($plugin->getTemplateResource('orcidAbout.tpl'));
}


}
Loading

0 comments on commit 8d66d49

Please sign in to comment.