Skip to content

Commit

Permalink
Merge branch 'fixUsersImport-813' into 'main'
Browse files Browse the repository at this point in the history
Adds numeric suffix to user username if already in use

See merge request softwares-pkp/plugins_ojs/fullJournalTransfer!50
  • Loading branch information
thiagolepidus committed Apr 25, 2024
2 parents 01d636d + 664e507 commit 7eae043
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 3 deletions.
11 changes: 11 additions & 0 deletions filter/filterConfig.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@
description="plugins.importexport.fullJournalTransfer.description"
inputType="xml::schema(plugins/importexport/fullJournalTransfer/fullJournal.xsd)"
outputType="class::classes.journal.Journal" />
<!-- Native XML Users input -->
<filterGroup
symbolic="native-xml=>user"
displayName="plugins.importexport.fullJournalTransfer.displayName"
description="plugins.importexport.fullJournalTransfer.description"
inputType="xml::schema(plugins/importexport/fullJournalTransfer/fullJournal.xsd)"
outputType="class::classes.users.User[]" />
<!-- Plugin native XML output -->
<filterGroup
symbolic="plugin=>native-xml"
Expand Down Expand Up @@ -176,6 +183,10 @@
inGroup="native-xml=>journal"
class="plugins.importexport.fullJournalTransfer.filter.import.NativeXmlJournalFilter"
isTemplate="0" />
<filter
inGroup="native-xml=>user"
class="plugins.importexport.fullJournalTransfer.filter.import.NativeXmlUserFilter"
isTemplate="0" />
<filter
inGroup="plugin=>native-xml"
class="plugins.importexport.fullJournalTransfer.filter.export.PluginNativeXmlFilter"
Expand Down
2 changes: 1 addition & 1 deletion filter/import/NativeXmlJournalFilter.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ public function parseNavigationMenu($node)
public function parseUsers($node, $journal)
{
$filterDao = DAORegistry::getDAO('FilterDAO');
$userFilters = $filterDao->getObjectsByGroup('user-xml=>user');
$userFilters = $filterDao->getObjectsByGroup('native-xml=>user');
assert(count($userFilters) == 1);
$filter = array_shift($userFilters);
$filter->setDeployment(new PKPUserImportExportDeployment($journal, null));
Expand Down
51 changes: 51 additions & 0 deletions filter/import/NativeXmlUserFilter.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

/**
* @file plugins/importexport/fullJournalTransfer/filter/import/NativeXmlUserFilter.inc.php
*
* Copyright (c) 2014-2021 Simon Fraser University
* Copyright (c) 2000-2021 John Willinsky
* Copyright (c) 2014-2024 Lepidus Tecnologia
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class NativeXmlUserFilter
* @ingroup plugins_importexport_fullJournalTransfer
*
* @brief Class that converts a Native XML document to an user.
*/

import('lib.pkp.plugins.importexport.users.filter.UserXmlPKPUserFilter');

class NativeXmlUserFilter extends UserXmlPKPUserFilter
{
public function __construct($filterGroup)
{
$this->setDisplayName('Native XML user import');
parent::__construct($filterGroup);
}

public function getClassName()
{
return 'plugins.importexport.fullJournalTransfer.filter.import.NativeXmlUserFilter';
}

public function importUserPasswordValidation($userToImport, $encryption)
{
$password = parent::importUserPasswordValidation($userToImport, $encryption);

$this->generateUsername($userToImport);

return $password;
}

public function generateUsername($user)
{
$userDAO = DAORegistry::getDAO('UserDAO');
$baseUsername = preg_replace('/[^A-Z0-9]/i', '', $user->getUsername());
for ($username = $baseUsername, $i = 1; $userDAO->userExistsByUsername($username); $i++) {
$username = $baseUsername . $i;
}

$user->setUsername($username);
}
}
43 changes: 43 additions & 0 deletions tests/import/NativeXmlUserFilterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

import('plugins.importexport.fullJournalTransfer.filter.import.NativeXmlUserFilter');
import('plugins.importexport.fullJournalTransfer.tests.NativeImportExportFilterTestCase');

class NativeXmlUserFilterTest extends NativeImportExportFilterTestCase
{
protected function getSymbolicFilterGroup()
{
return 'native-xml=>user';
}

protected function getNativeImportExportFilterClass()
{
return NativeXmlUserFilter::class;
}

protected function getMockedDAOs()
{
return ['UserDAO'];
}

public function testGenerateUsername()
{
$userImportFilter = $this->getNativeImportExportFilter();

$mockUserDAO = $this->getMockBuilder(UserDAO::class)
->setMethods(['userExistsByUsername'])
->getMock();
$mockUserDAO->expects($this->any())
->method('userExistsByUsername')
->will($this->onConsecutiveCalls(true, false));

DAORegistry::registerDAO('UserDAO', $mockUserDAO);

$user = new User();
$user->setUsername('reviewer');

$userImportFilter->generateUsername($user);

$this->assertEquals('reviewer1', $user->getUsername());
}
}
4 changes: 2 additions & 2 deletions version.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<version>
<application>fullJournalTransfer</application>
<type>plugins.importexport</type>
<release>2.0.10.1</release>
<date>2024-04-24</date>
<release>2.0.10.2</release>
<date>2024-04-25</date>
<class>FullJournalImportExportPlugin</class>
</version>

0 comments on commit 7eae043

Please sign in to comment.