mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
ENHANCEMENT MemberCsvBulkLoader for easy member import with group associations
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@94251 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
ef28a27b25
commit
fd86a0ba0e
79
security/MemberCsvBulkLoader.php
Normal file
79
security/MemberCsvBulkLoader.php
Normal file
@ -0,0 +1,79 @@
|
||||
<?php
|
||||
/**
|
||||
* Imports member records, and checks/updates duplicates based on their
|
||||
* 'Email' property.
|
||||
*
|
||||
* @package sapphire
|
||||
* @subpackage security
|
||||
*/
|
||||
class MemberCsvBulkLoader extends CsvBulkLoader {
|
||||
|
||||
/**
|
||||
* @var array Array of {@link Group} records. Import into a specific group.
|
||||
* Is overruled by any "Groups" columns in the import.
|
||||
*/
|
||||
protected $groups = array();
|
||||
|
||||
function __construct($objectClass = null) {
|
||||
if(!$objectClass) $objectClass = 'Member';
|
||||
|
||||
parent::__construct($objectClass);
|
||||
}
|
||||
|
||||
public $duplicateChecks = array(
|
||||
'Email' => 'Email',
|
||||
);
|
||||
|
||||
function processRecord($record, $columnMap, &$results, $preview = false) {
|
||||
$objID = parent::processRecord($record, $columnMap, $results, $preview);
|
||||
|
||||
$_cache_groupByCode = array();
|
||||
|
||||
// Add to predefined groups
|
||||
$member = DataObject::get_by_id($this->objectClass, $objID);
|
||||
foreach($this->groups as $group) {
|
||||
// TODO This isnt the most memory effective way to add members to a group
|
||||
$member->Groups()->add($group);
|
||||
}
|
||||
|
||||
// Add to groups defined in CSV
|
||||
if(isset($record['Groups']) && $record['Groups']) {
|
||||
$groupCodes = explode(',', $record['Groups']);
|
||||
foreach($groupCodes as $groupCode) {
|
||||
if(!isset($_cache_groupByCode[$groupCode])) {
|
||||
$group = DataObject::get_one(
|
||||
'Group',
|
||||
sprintf('"Code" = \'%s\'', Convert::raw2sql($groupCode))
|
||||
);
|
||||
if(!$group) {
|
||||
$group = new Group();
|
||||
$group->Code = $groupCode;
|
||||
$group->Title = $groupCode;
|
||||
$group->write();
|
||||
}
|
||||
$member->Groups()->add($group);
|
||||
}
|
||||
$_cache_groupByCode[$groupCode] = $group;
|
||||
}
|
||||
}
|
||||
|
||||
$member->destroy();
|
||||
unset($member);
|
||||
|
||||
return $objID;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Array $groups
|
||||
*/
|
||||
function setGroups($groups) {
|
||||
$this->groups = $groups;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Array
|
||||
*/
|
||||
function getGroups() {
|
||||
return $this->groups;
|
||||
}
|
||||
}
|
3
tests/security/MemberCsvBulkLoaderTest.csv
Normal file
3
tests/security/MemberCsvBulkLoaderTest.csv
Normal file
@ -0,0 +1,3 @@
|
||||
FirstName,Surname,Email,Password,PasswordEncryption,Salt,PasswordExpiry
|
||||
author1_first,author1_last,author1@test.com,21bb196c4d488023cb4e4b9df5f687c9a4d40172,sha1_v2.4,c23a94a878aa825cee69136bfd6185a874bc68801257978194,
|
||||
author2_first,author2_last,author2@test.com,069b6b22dcaded75c045b7e5ceb5d1dd743f9cac,sha1_v2.4,7e8ea602668b5631bac4baaf7174f9966b6d34571257978210,
|
|
61
tests/security/MemberCsvBulkLoaderTest.php
Normal file
61
tests/security/MemberCsvBulkLoaderTest.php
Normal file
@ -0,0 +1,61 @@
|
||||
<?php
|
||||
/**
|
||||
* @package sapphire
|
||||
* @subpackage tests
|
||||
*/
|
||||
class MemberCsvBulkLoaderTest extends SapphireTest {
|
||||
static $fixture_file = 'sapphire/tests/security/MemberCsvBulkLoaderTest.yml';
|
||||
|
||||
function testNewImport() {
|
||||
$loader = new MemberCsvBulkLoader();
|
||||
$results = $loader->load('sapphire/tests/security/MemberCsvBulkLoaderTest.csv');
|
||||
$created = $results->Created()->toArray();
|
||||
$this->assertEquals(count($created), 2);
|
||||
$this->assertEquals($created[0]->Email, 'author1@test.com');
|
||||
$this->assertEquals($created[1]->Email, 'author2@test.com');
|
||||
}
|
||||
|
||||
function testOverwriteExistingImport() {
|
||||
$author1 = new Member();
|
||||
$author1->FirstName = 'author1_first_old';
|
||||
$author1->Email = 'author1@test.com';
|
||||
$author1->write();
|
||||
|
||||
$loader = new MemberCsvBulkLoader();
|
||||
$results = $loader->load('sapphire/tests/security/MemberCsvBulkLoaderTest.csv');
|
||||
$created = $results->Created()->toArray();
|
||||
$this->assertEquals(count($created), 1);
|
||||
$updated = $results->Updated()->toArray();
|
||||
$this->assertEquals(count($updated), 1);
|
||||
$this->assertEquals($created[0]->Email, 'author2@test.com');
|
||||
$this->assertEquals($updated[0]->Email, 'author1@test.com');
|
||||
$this->assertEquals($updated[0]->FirstName, 'author1_first');
|
||||
}
|
||||
|
||||
function testAddToPredefinedGroups() {
|
||||
$existinggroup = $this->objFromFixture('Group', 'existinggroup');
|
||||
|
||||
$loader = new MemberCsvBulkLoader();
|
||||
$loader->setGroups(array($existinggroup));
|
||||
|
||||
$results = $loader->load('sapphire/tests/security/MemberCsvBulkLoaderTest.csv');
|
||||
|
||||
$created = $results->Created()->toArray();
|
||||
$this->assertEquals($created[0]->Groups()->column('ID'), array($existinggroup->ID));
|
||||
$this->assertEquals($created[1]->Groups()->column('ID'), array($existinggroup->ID));
|
||||
}
|
||||
|
||||
function testAddToCsvColumnGroupsByCode() {
|
||||
$existinggroup = $this->objFromFixture('Group', 'existinggroup');
|
||||
|
||||
$loader = new MemberCsvBulkLoader();
|
||||
$results = $loader->load('sapphire/tests/security/MemberCsvBulkLoaderTest_withGroups.csv');
|
||||
|
||||
$newgroup = DataObject::get_one('Group', sprintf('"Code" = \'%s\'', 'newgroup'));
|
||||
$this->assertEquals($newgroup->Title, 'newgroup');
|
||||
|
||||
$created = $results->Created()->toArray();
|
||||
$this->assertEquals($created[0]->Groups()->column('ID'), array($existinggroup->ID));
|
||||
$this->assertEquals($created[1]->Groups()->column('ID'), array($existinggroup->ID, $newgroup->ID));
|
||||
}
|
||||
}
|
7
tests/security/MemberCsvBulkLoaderTest.yml
Normal file
7
tests/security/MemberCsvBulkLoaderTest.yml
Normal file
@ -0,0 +1,7 @@
|
||||
Group:
|
||||
existinggroup:
|
||||
Code: existinggroup
|
||||
Member:
|
||||
existingauthor:
|
||||
Email: existingauthor@test.com
|
||||
FirstName: Existing Author
|
3
tests/security/MemberCsvBulkLoaderTest_withGroups.csv
Normal file
3
tests/security/MemberCsvBulkLoaderTest_withGroups.csv
Normal file
@ -0,0 +1,3 @@
|
||||
FirstName,Surname,Email,Password,PasswordEncryption,Salt,PasswordExpiry,Groups
|
||||
author1_first,author1_last,author1@test.com,21bb196c4d488023cb4e4b9df5f687c9a4d40172,sha1_v2.4,c23a94a878aa825cee69136bfd6185a874bc68801257978194,,existinggroup
|
||||
author2_first,author2_last,author2@test.com,069b6b22dcaded75c045b7e5ceb5d1dd743f9cac,sha1_v2.4,7e8ea602668b5631bac4baaf7174f9966b6d34571257978210,,"existinggroup,newgroup"
|
|
Loading…
Reference in New Issue
Block a user