Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port to OMP #22

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
235 changes: 217 additions & 18 deletions OrcidProfilePlugin.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
define('ORCID_API_URL_MEMBER_SANDBOX', 'https://api.sandbox.orcid.org/');

define('OAUTH_TOKEN_URL', 'oauth/token');
define('ORCID_API_VERSION_URL', 'v2.0/');
//define('ORCID_API_VERSION_URL', 'v2.0/');
define('ORCID_API_VERSION_URL', 'v1.2/');
define('ORCID_PROFILE_URL', 'orcid-profile');
define('ORCID_BIO_URL', 'orcid-bio');

class OrcidProfilePlugin extends GenericPlugin {
/**
Expand Down Expand Up @@ -99,16 +101,159 @@ function handleTemplateDisplay($hookName, $args) {
);

switch ($template) {
case 'frontend/pages/userRegister.tpl':
case 'user/register.tpl':
$templateMgr->register_outputfilter(array(&$this, 'registrationFilter'));
break;
case 'author/submit/step3.tpl':
$templateMgr->register_outputfilter(array(&$this, 'submitFilter'));
break;
case 'frontend/pages/userRegister.tpl': // OMP
$templateMgr->register_outputfilter(array($this, 'registrationFilter'));
break;
case 'user/publicProfileForm.tpl':
$templateMgr->register_outputfilter(array($this, 'profileFilter'));
break;
case 'frontend/pages/userLogin.tpl': // OMP
$templateMgr->register_outputfilter(array(&$this, 'loginFilterOMP'));
break;
case 'controllers/grid/users/author/form/authorForm.tpl': // OMP
$templateMgr->register_outputfilter(array(&$this, 'addCheckOrcidButton'));
break;

}
return false;
}

/**
* Output filter adds ORCiD interaction to OJS login form.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this refer to OJS specifically?

* @param $output string
* @param $templateMgr TemplateManager
* @return $string
*/
function addCheckOrcidButton($output, &$templateMgr) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally speaking, references on objects aren't needed any more; I'd suggest removing throughout unless they're needed.

$sessionManager = SessionManager::getManager();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space-based indents.

$userSession = $sessionManager->getUserSession();

$templateMgr->register_function('plugin_url', array($this, 'smartyPluginUrl'));

if (preg_match('/<form.*id="editAuthor"[^>]+>/', $output, $matches, PREG_OFFSET_CAPTURE)) {
$match = $matches[0][0];
$offset = $matches[0][1];
$context = Request::getContext();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's possible, I'd suggest exposing the $request object to this function rather than calling Request::(whatever) statically. Static calls to request functions are deprecated.

$templateMgr->assign(array(
'targetOp' => 'login',
'orcidProfileOauthPath' => $this->getOauthPath(),
'orcidClientId' => $this->getSetting($context->getId(), 'orcidClientId'),
));
$newOutput = substr($output, 0, $offset);
$newOutput .= $templateMgr->fetch($this->getTemplatePath() . 'orcidLoginCheck.tpl');
$newOutput .= substr($output, $offset);
$output = $newOutput;
}
$templateMgr->unregister_outputfilter('addCheckOrcidButton');
return $output;
}

function smartyPluginUrl($params, &$smarty) {
$request = PKPApplication::getRequest();
$dispatcher = $request->getDispatcher();
return $dispatcher->url($request, ROUTE_PAGE, null, 'plugins', 'generic', array_merge(array('plugin', $this->getName(), isset($params['path'])?$params['path']:array())));
}

/**
* Search for author information in ORCiD registry.
* @param $args array
* @param $request PKPRequest
*/
function orcidSearch($args, $request) {
$plugin =& PluginRegistry::getPlugin('generic', 'orcidprofileplugin');
$templateMgr =& TemplateManager::getManager($request);

$authorIndex = Request::getUserVar('authorIndex');
$orcidButtonId = Request::getUserVar('orcidButtonId');
$orcidInputId = Request::getUserVar('orcidInputId');
$templateMgr->assign_by_ref('authorIndex', $authorIndex);
$templateMgr->assign_by_ref('orcidButtonId', $orcidButtonId);
$templateMgr->assign_by_ref('orcidInputId', $orcidInputId);

switch (Request::getUserVar('targetOp')) {
case 'form':
$templateMgr->display($plugin->getTemplatePath() . 'orcidProfileSearchForm.tpl');
break;
case 'search':
$journal = Request::getJournal(); //
$name = $request->getUserVar('search-orcid-name');
$lastname = $request->getUserVar('search-orcid-lastname');
$email = $request->getUserVar('search-orcid-email');
$orcidSearchResults = array();
if (($name && $lastname) || $email) {
// Obtaining a search token
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_FAILONERROR => true,
CURLOPT_URL => $url = $plugin->getSetting($journal->getId(), 'orcidProfileAPIPath').OAUTH_TOKEN_URL,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array('Accept: application/json'),
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query(array(
'scope' => '/read-public',
'grant_type' => 'client_credentials',
'client_id' => $plugin->getSetting($journal->getId(), 'orcidClientId'),
'client_secret' => $plugin->getSetting($journal->getId(), 'orcidClientSecret')
))
));
$result = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);
if ($result) {
$response = json_decode($result, true);
$query = '?q=';
if ($name && $lastname && $email) {
$query .= '(given-names:' . $name . 'ANDfamily-name:' . $lastname . ')ORemail:' . $email;
} elseif ($name && $lastname) {
$query .= 'given-names:' . $name . 'ANDfamily-name:' . $lastname;
} else {
$query .= 'email:' . $email;
}

// Performing search
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_FAILONERROR => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array('Accept: application/json',
'Content-Type: application/orcidxml',
'Authorization: Bearer ' . $response['access_token']),
CURLOPT_POST => false,
CURLOPT_URL => $url = $plugin->getSetting($journal->getId(), 'orcidProfileAPIPath') . ORCID_API_VERSION_URL . 'search/' . ORCID_BIO_URL . '/' . $query,
));
$result = curl_exec($curl);
if ($result) {
// Processing results
$response = json_decode($result, true);
if ($response['orcid-search-results']['num-found'] > 0) {
foreach($response['orcid-search-results']['orcid-search-result'] as $resultItem) {
$name = $resultItem['orcid-profile']['orcid-bio']['personal-details']['given-names']['value'];
$lastname = $resultItem['orcid-profile']['orcid-bio']['personal-details']['family-name']['value'];
$email = $resultItem['orcid-profile']['orcid-bio']['contact-details']['email'][0]['value'];
$orcidiD = $resultItem['orcid-profile']['orcid-identifier']['uri'];
$orcidSearchResults[] = array('name' => $name,
'lastname' => $lastname,
'email' => $email,
'orcidiD' => $orcidiD);
}
}
}
}

}
$templateMgr->assign_by_ref('orcidSearchResults', $orcidSearchResults);
$templateMgr->display($plugin->getTemplatePath() . 'orcidProfileSearchResults.tpl');
break;
default: assert(false);
}
}

/**
* Return the OAUTH path (prod or sandbox) based on the current API configuration
* @return $string
Expand All @@ -125,6 +270,60 @@ function getOauthPath() {
}
}

/**
* Output filter adds ORCiD interaction to OJS login form.
* @param $output string
* @param $templateMgr TemplateManager
* @return $string
*/
function loginFilter($output, &$templateMgr) {
$sessionManager = SessionManager::getManager();
$userSession = $sessionManager->getUserSession();
if (preg_match('/<form.*id="signinForm"[^>]+>/', $output, $matches, PREG_OFFSET_CAPTURE)) { // OMP: id="login" OJS: id="signinForm"
$match = $matches[0][0];
$offset = $matches[0][1];
$context = Request::getContext();
$templateMgr->assign(array(
'targetOp' => 'login',
'orcidProfileOauthPath' => $this->getOauthPath(),
'orcidClientId' => $this->getSetting($context->getId(), 'orcidClientId'),
));
$newOutput = substr($output, 0, $offset);
$newOutput .= $templateMgr->fetch($this->getTemplatePath() . 'orcidLogin.tpl');
$newOutput .= substr($output, $offset);
$output = $newOutput;
}
$templateMgr->unregister_outputfilter('loginFilter');
return $output;
}

/**
* Output filter adds ORCiD interaction to OMP login form.
* @param $output string
* @param $templateMgr TemplateManager
* @return $string
*/
function loginFilterOMP($output, &$templateMgr) {
$sessionManager = SessionManager::getManager();
$userSession = $sessionManager->getUserSession();
if (preg_match('/<form.*id="login"[^>]+>/', $output, $matches, PREG_OFFSET_CAPTURE)) { // OMP: id="login" OJS: id="signinForm"
$match = $matches[0][0];
$offset = $matches[0][1];
$context = Request::getContext();
$templateMgr->assign(array(
'targetOp' => 'login',
'orcidProfileOauthPath' => $this->getOauthPath(),
'orcidClientId' => $this->getSetting($context->getId(), 'orcidClientId'),
));
$newOutput = substr($output, 0, $offset);
$newOutput .= $templateMgr->fetch($this->getTemplatePath() . 'orcidLogin.tpl');
$newOutput .= substr($output, $offset);
$output = $newOutput;
}
$templateMgr->unregister_outputfilter('loginFilterOMP');
return $output;
}

/**
* Output filter adds ORCiD interaction to registration form.
* @param $output string
Expand Down Expand Up @@ -300,22 +499,22 @@ function getInstallEmailTemplateDataFile() {
/**
* Extend the {url ...} smarty to support this plugin.
*/
function smartyPluginUrl($params, &$smarty) {
$path = array($this->getCategory(), $this->getName());
if (is_array($params['path'])) {
$params['path'] = array_merge($path, $params['path']);
} elseif (!empty($params['path'])) {
$params['path'] = array_merge($path, array($params['path']));
} else {
$params['path'] = $path;
}

if (!empty($params['id'])) {
$params['path'] = array_merge($params['path'], array($params['id']));
unset($params['id']);
}
return $smarty->smartyUrl($params, $smarty);
}
//function smartyPluginUrl($params, &$smarty) {
// $path = array($this->getCategory(), $this->getName());
// if (is_array($params['path'])) {
// $params['path'] = array_merge($path, $params['path']);
// } elseif (!empty($params['path'])) {
// $params['path'] = array_merge($path, array($params['path']));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dead code left behind?

// } else {
// $params['path'] = $path;
// }

// if (!empty($params['id'])) {
// $params['path'] = array_merge($params['path'], array($params['id']));
// unset($params['id']);
// }
// return $smarty->smartyUrl($params, $smarty);
//}

/**
* @see Plugin::getActions()
Expand Down
83 changes: 83 additions & 0 deletions classes/OrcidUserSettingsDAO.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

/**
* @file classes/user/UserSettingsDAO.inc.php
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Class name/path incorrect

*
* Copyright (c) 2014-2017 Simon Fraser University
* Copyright (c) 2003-2017 John Willinsky
* Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
*
* @class UserSettingsDAO
* @ingroup user
* @see User
*
* @brief Operations for retrieving and modifying user settings.
*/

import('lib.pkp.classes.user.PKPUserSettingsDAO');

class OrcidUserSettingsDAO extends PKPUserSettingsDAO {
/**
* Constructor
*/
function __construct() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wherever possible, remove unneeded (empty) constructors.

parent::__construct();
}

/**
* Retrieve a user setting value.
* @param $userId int
* @param $name
* @param $pressId int
* @return mixed
*/
function &getSetting($userId, $name, $pressId = null) {
return parent::getSetting($userId, $name, null, $pressId);
}

/**
* Retrieve all users by setting name and value.
* @param $name string
* @param $value mixed
* @param $type string
* @param $pressId int
* @return DAOResultFactory matching Users
*/
function &getUsersBySetting($name, $value, $type = null, $pressId = null) {
return parent::getUsersBySetting($name, $value, $type, null, $pressId);
}

/**
* Retrieve all settings for a user for a press.
* @param $userId int
* @param $pressId int
* @return array
*/
function &getSettingsByPress($userId, $pressId = null) {
return parent::getSettingsByAssoc($userId, null, $pressId);
}

/**
* Add/update a user setting.
* @param $userId int
* @param $name string
* @param $value mixed
* @param $type string data type of the setting. If omitted, type will be guessed
* @param $pressId int
*/
function updateSetting($userId, $name, $value, $type = null, $pressId = null) {
return parent::updateSetting($userId, $name, $value, $type, null, $pressId);
}

/**
* Delete a user setting.
* @param $userId int
* @param $name string
* @param $pressId int
*/
function deleteSetting($userId, $name, $pressId = null) {
return parent::deleteSetting($userId, $name, null, $pressId);
}
}

?>
5 changes: 4 additions & 1 deletion locale/en_US/locale.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
-->

<locale name="en_US" full_name="U.S. English">

<message key="plugins.generic.orcidProfile.displayName">ORCID Profile Plugin</message>
<message key="plugins.generic.orcidProfile.description"><![CDATA[Allows for the import of user profile information from ORCID.]]></message>
<message key="plugins.generic.orcidProfile.instructions"><![CDATA[You can pre-populate this form with information from an ORCID profile. Enter the email address or ORCID iD associated with the ORCID profile, then click "Submit".]]></message>
Expand All @@ -33,4 +32,8 @@
<message key="plugins.generic.orcidProfile.author.submission.success">Your submission has successfully been associated with your ORCID iD.</message>
<message key="plugins.generic.orcidProfile.author.submission.failure">Your submission could not be successfully associated with your ORCID iD. Please contact the journal manager with your name, ORCID, and details of your submission.</message>
<message key="plugins.generic.orcidProfile.authFailure">OJS was not able to communicate with the ORCID service. Please contact the journal manager with your name, ORCID iD, and details of your submission.</message>
<message key="plugins.generic.orcidProfile.linkMessage">Connect with ORCID</message>
<message key="plugins.generic.orcidProfile.oauthLoginError">OJS was not able to communicate with the ORCID service. Please contact the journal manager with your name, ORCID, and details of your submission.</message>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refers to OJS

<message key="plugins.generic.orcidProfile.oauthTooManyMatches">There is more than one user with these auth data.</message>
<message key="plugins.generic.orcidProfile.searchOrcidExists">Check if ORCID exists</message>
</locale>
Loading