-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'fixUsersImport-813' into 'main'
Adds numeric suffix to user username if already in use See merge request softwares-pkp/plugins_ojs/fullJournalTransfer!50
- Loading branch information
Showing
5 changed files
with
108 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters