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

Login process added to the code. #12

Open
wants to merge 1 commit into
base: ojs-dev-2_4
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
33 changes: 33 additions & 0 deletions OrcidProfilePlugin.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ function handleTemplateDisplay($hookName, $args) {
case 'author/submit/step3.tpl':
$templateMgr->register_outputfilter(array(&$this, 'submitFilter'));
break;
case 'user/login.tpl':
$templateMgr->register_outputfilter(array(&$this, 'loginFilter'));
break;
}
return false;
}
Expand All @@ -117,6 +120,36 @@ function getOauthPath() {
}
}

/**
* Output filter adds ORCiD interaction to 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)) {
$match = $matches[0][0];
$offset = $matches[0][1];
$journal = Request::getJournal();

$templateMgr->assign(array(
'targetOp' => 'login',
'orcidProfileOauthPath' => $this->getOauthPath(),
'orcidClientId' => $this->getSetting($journal->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 registration form.
* @param $output string
Expand Down
1 change: 1 addition & 0 deletions locale/en_US/locale.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<message key="plugins.generic.orcidProfile.noData">Couldn\'t find any data from ORCID.</message>
<message key="plugins.generic.orcidProfile.emailOrOrcid">Email address or ORCID iD:</message>
<message key="plugins.generic.orcidProfile.submitAction">Submit</message>
<message key="plugins.generic.orcidProfile.linkMessage">Create or Connect your ORCID iD</message>
<message key="plugins.generic.orcidProfile.manager.orcidProfileSettings">ORCID Profile Settings</message>
<message key="plugins.generic.orcidProfile.manager.settings.description">Configure the ORCID API for use in pulling ORCID profile information into the user profile.</message>
<message key="plugins.generic.orcidProfile.manager.settings.orcidProfileAPIPath">ORCID API</message>
Expand Down
25 changes: 25 additions & 0 deletions orcidLogin.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{**
* plugins/generic/orcidProfile/orcidProfile.tpl
*
* Copyright (c) 2015-2016 University of Pittsburgh
* Copyright (c) 2014-2016 Simon Fraser University Library
* Copyright (c) 2003-2016 John Willinsky
* Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
*
* ORCID Profile authorization form
*
*}

{literal}
<style>
#orcidLoginLink {
margin: 15px;
}
</style>
{/literal}
<div id='orcidLoginLink'>
<a href="{$orcidProfileOauthPath|escape}authorize?client_id={$orcidClientId|urlencode}&response_type=code&scope=/authenticate&redirect_uri={url|urlencode page="orcidapi" op="orcidAuthorize" targetOp=$targetOp params=$params escape=false}">
<img id="orcid-id-logo" src="http://orcid.org/sites/default/files/images/orcid_16x16.png" width='16' height='16' alt="ORCID logo" alt="{translate key='plugins.generic.orcidProfile.submitAction'}"/>
{translate key='plugins.generic.orcidProfile.linkMessage'}
</a>
</div>
2 changes: 1 addition & 1 deletion orcidProfile.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ function openORCID() {ldelim}
</script>

<button id="connect-orcid-button" onclick="return openORCID();"><img id="orcid-id-logo" src="http://orcid.org/sites/default/files/images/orcid_24x24.png" width="24" height="24" alt="{translate key='plugins.generic.orcidProfile.submitAction'}"/>Create or Connect your ORCID iD</button>
<br />
<br />
100 changes: 99 additions & 1 deletion pages/OrcidHandler.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,24 @@ function orcidAuthorize($args, &$request) {

// fetch the access token
$curl = curl_init();
//curl_setopt($curl, CURLOPT_URL, $plugin->getSetting($journal->getId(), 'orcidProfileAPIPath').OAUTH_TOKEN_URL);
curl_setopt_array($curl, array(
CURLOPT_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(
'code' => Request::getUserVar('code'),
'code' => $request->getUserVar('code'),
'grant_type' => 'authorization_code',
'client_id' => $plugin->getSetting($journal->getId(), 'orcidClientId'),
'client_secret' => $plugin->getSetting($journal->getId(), 'orcidClientSecret')
))
));

$result = curl_exec($curl);
$error = curl_error($curl);


$response = json_decode($result, true);

curl_setopt_array($curl, array(
Expand All @@ -56,6 +61,8 @@ function orcidAuthorize($args, &$request) {
$json = json_decode($result, true);
}

// TODO defstat: Check the $json parameter - Not always full because of error code not always 200.

switch (Request::getUserVar('targetOp')) {
case 'register':
echo '<html><body><script type="text/javascript">
Expand All @@ -82,6 +89,95 @@ function orcidAuthorize($args, &$request) {
opener.document.getElementById("connect-orcid-button").style.display = "none";
window.close();
</script></body></html>';
break;
case 'login':
if (!is_null($json)) {
// The user that will be logged in
$loggedInUser = null;

// Check if there is any user that has autoassigned orcidauth parameter on the UserSettings.
$userSettingsDao = DAORegistry::getDAO('UserSettingsDAO');
$userDao = DAORegistry::getDAO('UserDAO');
$users = $userSettingsDao->getUsersBySetting('orcidauth', 'http://orcid.org/' . $response['orcid']);

if (is_null($users) || $users->count == 0) { // If no user exists
// Then we should look for someone that has his orcid field filled.
$users = $userSettingsDao->getUsersBySetting('orcid', 'http://orcid.org/' . $response['orcid']);

if (is_null($users) || $users->count == 0) { // If no user exists
// Then we can look if there is any user with the email assigned to any email from ORCID profile

// get all emails
$emails = $json['orcid-profile']['orcid-bio']['contact-details']['email'];
if (!is_null($emails)) { // No emails retrieved from api. Email field may not be public
foreach($emails as $email) {
$user->$userDao->getUserByEmail($email, false);

if (!is_null($user)) {
$loggedInUser = $user;
break;
}
}
}
} else { // we have at least one user with his orcid field filled
if (count($users) != 1) { // There are more than one users with that orcidauth. Nothing we can do. Loggin fails
$loggedInUser = false;
Validation::redirectLogin('plugins.generic.oauth.message.oauthTooManyMatches');
} else { // only one user has the current orcidauth. We can log him in.
$loggedInUser = $users->next();
}
}

} else { // There is at least one user with its orcidauth assigned to the current value
if ($users->count != 1) { // There are more than one users with that orcidauth. Nothing we can do. Loggin fails
$loggedInUser = false;
Validation::redirectLogin('plugins.generic.oauth.message.oauthTooManyMatches');
} else { // only one user has the current orcidauth. We can log him in.
$loggedInUser = $users->next();
}

}

if ($loggedInUser) {
$userDao =& DAORegistry::getDAO('UserDAO');

$reason = null;
// The user is valid, mark user as logged in in current session
$sessionManager =& SessionManager::getManager();

// Regenerate session ID first
$sessionManager->regenerateSessionId();

$session =& $sessionManager->getUserSession();
$session->setSessionVar('userId', $loggedInUser->getId());
$session->setUserId($loggedInUser->getId());
$session->setSessionVar('username', $loggedInUser->getUsername());
//$session->setRemember($remember);

$loggedInUser->setDateLastLogin(Core::getCurrentDate());
$userDao->updateObject($loggedInUser);

Validation::redirectLogin();
} else { // OAuth successful, but not linked to a user account (yet)
$sessionManager = SessionManager::getManager();
$userSession = $sessionManager->getUserSession();
$user = $userSession->getUser();

if (isset($user)) {
// If the user is authenticated, link this user account
$userSettingsDao->updateSetting($user->getId(), 'orcidauth', 'http://orcid.org/' . $response['orcid'], 'string');
$userSettingsDao->updateSetting($user->getId(), 'orcid', 'http://orcid.org/' . $response['orcid'], 'string');
} else {
// Otherwise, send the user to the login screen (keep track of the oauthUniqueId to link upon login!)
$userSession->setSessionVar('orcidauth', 'http://orcid.org/' . $response['orcid']);
}
}

Validation::redirectLogin('plugins.generic.oauth.message.oauthLoginError');
}

Validation::redirectLogin('plugins.generic.oauth.message.oauthTooManyMatches');

break;
default: assert(false);
}
Expand Down Expand Up @@ -150,6 +246,8 @@ function orcidVerify($args, $request) {
));
$templateMgr->display('common/message.tpl');
}


}

?>