Skip to content

Commit

Permalink
pkp#23, pkp#28: test credentials upon settings form entry
Browse files Browse the repository at this point in the history
  • Loading branch information
ctgraham committed Jul 21, 2022
1 parent e47bc74 commit 9da5b37
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 18 deletions.
50 changes: 35 additions & 15 deletions PlagiarismPlugin.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,37 @@ public function sendErrorMessage($submissionid, $message) {
}
error_log('iThenticate submission '.$submissionid.' failed: '.$message);
}


/**
* Connects to iThenticate and validates interop prerequisites
* @param $username string iThenticate username
* @param $password string iThenticate password
* @param $groupname string An iThenticate folder group which will be created if not already existing
* @return Ithenticate iThenticate connection object
* @throws PlagiarismIthenticateException
*/
public function ithenticateConnect($username, $password, $groupname) {
require_once(dirname(__FILE__) . '/vendor/autoload.php');
import('plugins.generic.plagiarism.PlagiarismIthenticateException');

$ithenticate = null;
try {
$ithenticate = new \bsobbe\ithenticate\Ithenticate($username, $password);
} catch (Exception $e) {
throw new PlagiarismIthenticateException($e->getMessage(), 0, $e, null);
}
// Make sure there's a group list for this context, creating if necessary.
$groupList = $ithenticate->fetchGroupList();
if (!($groupId = array_search($groupname, $groupList))) {
// No folder group found for the context; create one.
$groupId = $ithenticate->createGroup($groupname);
if (!$groupId) {
throw new PlagiarismIthenticateException('Could not create folder group for context ' . $contextName . ' on iThenticate.', 0, null, $ithenticate);
}
}
return $ithenticate;
}

/**
* Send submission files to iThenticate.
* @param $hookName string
Expand All @@ -109,8 +139,6 @@ public function callback($hookName, $args) {
$submission = $submissionDao->getById($request->getUserVar('submissionId'));
$publication = $submission->getCurrentPublication();

require_once(dirname(__FILE__) . '/vendor/autoload.php');

// try to get credentials for current context otherwise use default config
$contextId = $context->getId();
list($username, $password) = $this->getForcedCredentials();
Expand All @@ -119,24 +147,15 @@ public function callback($hookName, $args) {
$password = $this->getSetting($contextId, 'ithenticatePass');
}

$contextName = $context->getLocalizedName($context->getPrimaryLocale());

$ithenticate = null;
try {
$ithenticate = new \bsobbe\ithenticate\Ithenticate($username, $password);
$ithenticate = $this->ithenticateConnect($username, $password, $contextName);
} catch (Exception $e) {
$this->sendErrorMessage($submission->getId(), $e->getMessage());
return false;
}
// Make sure there's a group list for this context, creating if necessary.
$groupList = $ithenticate->fetchGroupList();
$contextName = $context->getLocalizedName($context->getPrimaryLocale());
if (!($groupId = array_search($contextName, $groupList))) {
// No folder group found for the context; create one.
$groupId = $ithenticate->createGroup($contextName);
if (!$groupId) {
$this->sendErrorMessage($submission->getId(), 'Could not create folder group for context ' . $contextName . ' on iThenticate.');
return false;
}
}

// Create a folder for this submission.
if (!($folderId = $ithenticate->createFolder(
Expand Down Expand Up @@ -255,3 +274,4 @@ public function submitDocument($essay_title, $author_firstname, $author_lastname
return true;
}
}

42 changes: 39 additions & 3 deletions PlagiarismSettingsForm.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ function __construct($plugin, $contextId) {

parent::__construct($plugin->getTemplateResource('settingsForm.tpl'));

$this->addCheck(new FormValidator($this, 'ithenticateUser', 'required', 'plugins.generic.plagiarism.manager.settings.usernameRequired'));
$this->addCheck(new FormValidator($this, 'ithenticatePass', 'required', 'plugins.generic.plagiarism.manager.settings.passwordRequired'));
$this->addCheck(new FormValidator($this, 'ithenticateUser', FORM_VALIDATOR_REQUIRED_VALUE, 'plugins.generic.plagiarism.manager.settings.usernameRequired'));
$this->addCheck(new FormValidator($this, 'ithenticatePass', FORM_VALIDATOR_REQUIRED_VALUE, 'plugins.generic.plagiarism.manager.settings.passwordRequired'));
$this->addCheck(new FormValidatorCustom($this, 'ithenticatePass', FORM_VALIDATOR_REQUIRED_VALUE, 'plugins.generic.plagiarism.manager.settings.loginTestFailed', array(&$this, '_checkConnection'), array(&$this), true));

$this->addCheck(new FormValidatorPost($this));
$this->addCheck(new FormValidatorCSRF($this));
Expand Down Expand Up @@ -61,7 +62,42 @@ function fetch($request, $template = null, $display = false) {
*/
function execute(...$functionArgs) {
$this->_plugin->updateSetting($this->_contextId, 'ithenticateUser', trim($this->getData('ithenticateUser'), "\"\';"), 'string');
$this->_plugin->updateSetting($this->_contextId, 'ithenticatePass', trim($this->getData('ithenticatepass'), "\"\';"), 'string');
$this->_plugin->updateSetting($this->_contextId, 'ithenticatePass', trim($this->getData('ithenticatePass'), "\"\';"), 'string');
parent::execute(...$functionArgs);
}

/**
* Check the username and password for the service
* @param $formPassword string the value of the field being checked
* @param $form object a reference to this form
* @return boolean Is there a problem with the form?
*/
function _checkConnection($formPassword, $form) {
$username = $form->getData('ithenticateUser');
// bypass testing if login is not present
if (empty($username) || empty($formPassword)) {
return false;
}

$contextDao = Application::getContextDAO();
$context = $contextDao->getById($this->_contextId);
// if credentials are forced, don't bother testing them. The user can't do anything about a failure on this form.
list($username, $password) = $this->_plugin->getForcedCredentials();
if (!empty($username) && !empty($password)) {
return false;
}
$contextName = $context->getLocalizedName($context->getPrimaryLocale());
$username = $username = $form->getData('ithenticateUser');
$password = $formPassword;

$ithenticate = null;
try {
$ithenticate = $this->_plugin->ithenticateConnect($username, $password, $contextName);
} catch (Exception $e) {
error_log($e->getMessage());
return true;
}
return false;
}

}

0 comments on commit 9da5b37

Please sign in to comment.