2009-12-02 10:40:38 +01:00
|
|
|
<?php
|
2016-06-15 06:03:16 +02:00
|
|
|
|
2016-06-23 01:37:22 +02:00
|
|
|
namespace SilverStripe\Security;
|
|
|
|
|
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\DataObject;
|
2016-06-23 01:37:22 +02:00
|
|
|
use CsvBulkLoader;
|
|
|
|
|
2009-12-02 10:40:38 +01:00
|
|
|
/**
|
|
|
|
* @todo Migrate Permission->Arg and Permission->Type values
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2009-12-02 10:40:38 +01:00
|
|
|
* @subpackage security
|
|
|
|
*/
|
|
|
|
class GroupCsvBulkLoader extends CsvBulkLoader {
|
|
|
|
|
|
|
|
public $duplicateChecks = array(
|
|
|
|
'Code' => 'Code',
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function __construct($objectClass = null) {
|
2016-06-23 01:37:22 +02:00
|
|
|
if(!$objectClass) $objectClass = 'SilverStripe\\Security\\Group';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-12-02 10:40:38 +01:00
|
|
|
parent::__construct($objectClass);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function processRecord($record, $columnMap, &$results, $preview = false) {
|
2009-12-02 10:40:38 +01:00
|
|
|
// We match by 'Code', the ID property is confusing the importer
|
|
|
|
if(isset($record['ID'])) unset($record['ID']);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-12-02 10:40:38 +01:00
|
|
|
$objID = parent::processRecord($record, $columnMap, $results, $preview);
|
|
|
|
|
|
|
|
$group = DataObject::get_by_id($this->objectClass, $objID);
|
|
|
|
// set group hierarchies - we need to do this after all records
|
|
|
|
// are imported to avoid missing "early" references to parents
|
|
|
|
// which are imported later on in the CSV file.
|
|
|
|
if(isset($record['ParentCode']) && $record['ParentCode']) {
|
2016-06-23 01:37:22 +02:00
|
|
|
$parentGroup = DataObject::get_one('SilverStripe\\Security\\Group', array(
|
2013-06-21 00:32:08 +02:00
|
|
|
'"Group"."Code"' => $record['ParentCode']
|
|
|
|
));
|
2009-12-02 10:40:38 +01:00
|
|
|
if($parentGroup) {
|
|
|
|
$group->ParentID = $parentGroup->ID;
|
|
|
|
$group->write();
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-12-02 10:40:38 +01:00
|
|
|
// set permission codes - these are all additive, meaning
|
|
|
|
// existing permissions arent cleared.
|
|
|
|
if(isset($record['PermissionCodes']) && $record['PermissionCodes']) {
|
|
|
|
foreach(explode(',', $record['PermissionCodes']) as $code) {
|
2016-06-23 01:37:22 +02:00
|
|
|
$p = DataObject::get_one('SilverStripe\\Security\\Permission', array(
|
2013-06-21 00:32:08 +02:00
|
|
|
'"Permission"."Code"' => $code,
|
|
|
|
'"Permission"."GroupID"' => $group->ID
|
|
|
|
));
|
2009-12-02 10:40:38 +01:00
|
|
|
if(!$p) {
|
|
|
|
$p = new Permission(array('Code' => $code));
|
|
|
|
$p->write();
|
|
|
|
}
|
|
|
|
$group->Permissions()->add($p);
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-12-02 10:40:38 +01:00
|
|
|
return $objID;
|
|
|
|
}
|
|
|
|
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|