Skip to content

Commit

Permalink
pkp#3462 Error messages added to user import process
Browse files Browse the repository at this point in the history
  • Loading branch information
defstat committed May 10, 2018
1 parent efa4283 commit 9338ea6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
6 changes: 6 additions & 0 deletions locale/en_US/manager.xml
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,12 @@
<!-- native importexport keys -->
<message key="plugins.importexport.native.error.submissionFileImportFailed">The submission file could not be imported</message>

<!-- native import user keys -->
<message key="plugins.importexport.user.importExportErrors">Import/Export errors:</message>
<message key="plugins.importexport.user.error.userHasNoPassword">The imported user "{$username}" has no password. Check your import XML format. The user has not been imported.</message>
<message key="plugins.importexport.user.error.passwordHasBeenChanged">The imported user "{$username}" password could not be imported as is. A new password is been send to the user email. The user has been imported.</message>
<message key="plugins.importexport.user.error.plainPasswordNotValid">The imported user "{$username}" has a plain password that is not valid. The user has not been imported.</message>

<!-- Navigation Menus -->
<message key="manager.navigationMenus.form.title">Title</message>
<message key="manager.navigationMenus.form.path">Path</message>
Expand Down
10 changes: 7 additions & 3 deletions plugins/importexport/users/PKPUserImportExportPlugin.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,16 @@ function display($args, $request) {
}
$temporaryFilePath = $temporaryFile->getFilePath();
libxml_use_internal_errors(true);
$users = $this->importUsers(file_get_contents($temporaryFilePath), $context, $user);
$importFilter = $this->importUsers(file_get_contents($temporaryFilePath), $context, $user);
$validationErrors = array_filter(libxml_get_errors(), function($a) {
return $a->level == LIBXML_ERR_ERROR || $a->level == LIBXML_ERR_FATAL;
});
$templateMgr->assign('validationErrors', $validationErrors);
libxml_clear_errors();
$templateMgr->assign('users', $users);
if ($importFilter->hasErrors()) {
$templateMgr->assign('filterErrors', $importFilter->getErrors());
}
$templateMgr->assign('users', $importFilter->getLastOutput());
$json = new JSONMessage(true, $templateMgr->fetch($this->getTemplatePath() . 'results.tpl'));
return $json->getString();
case 'export':
Expand Down Expand Up @@ -213,7 +216,8 @@ function importUsers($importXml, $context, $user) {
$importFilter = array_shift($userImportFilters);
$importFilter->setDeployment(new PKPUserImportExportDeployment($context, $user));

return $importFilter->execute($importXml);
$importFilter->execute($importXml);
return $importFilter;
}
}

Expand Down
14 changes: 11 additions & 3 deletions plugins/importexport/users/filter/UserXmlPKPUserFilter.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ function parseUser($node) {
$password = $passwordValueNodeList->item(0);
$user->setPassword($password->textContent);
} else {
fatalError("User has no password. Check your import XML format.");
$this->addError(__('plugins.importexport.user.error.userHasNoPassword', array('username' => $user->getUsername())));
}

break;
Expand Down Expand Up @@ -227,18 +227,26 @@ function handleChildElement($n) {
* @param $encryption string null, sha1, md5 (or any other encryption algorithm defined)
* @return string if a new password is generated, the function returns it.
*/
function importUserPasswordValidation(&$userToImport, $encryption) {
function importUserPasswordValidation($userToImport, $encryption) {
$passwordHash = $userToImport->getPassword();
$password = null;
if (!$encryption) {
$userToImport->setPassword(Validation::encryptCredentials($userToImport->getUsername(), $passwordHash));
$siteDao = DAORegistry::getDAO('SiteDAO');
$site = $siteDao->getSite();
if (strlen($passwordHash) >= $site->getMinPasswordLength()) {
$userToImport->setPassword(Validation::encryptCredentials($userToImport->getUsername(), $passwordHash));
} else {
$this->addError(__('plugins.importexport.user.error.plainPasswordNotValid', array('username' => $userToImport->getUsername())));
}
} else {
if (password_needs_rehash($passwordHash, PASSWORD_BCRYPT)) {

$password = Validation::generatePassword();
$userToImport->setPassword(Validation::encryptCredentials($userToImport->getUsername(), $password));

$userToImport->setMustChangePassword(true);

$this->addError(__('plugins.importexport.user.error.passwordHasBeenChanged', array('username' => $userToImport->getUsername())));
} else {
$userToImport->setPassword($passwordHash);
}
Expand Down

0 comments on commit 9338ea6

Please sign in to comment.