silverstripe-framework/src/Security/GroupCsvBulkLoader.php

70 lines
2.2 KiB
PHP
Raw Normal View History

<?php
2016-06-23 01:37:22 +02:00
namespace SilverStripe\Security;
use SilverStripe\ORM\DataObject;
use SilverStripe\Dev\CsvBulkLoader;
/**
* @todo Migrate Permission->Arg and Permission->Type values
*/
2016-11-29 00:31:16 +01:00
class GroupCsvBulkLoader extends CsvBulkLoader
{
2014-08-15 08:53:05 +02:00
2016-11-29 00:31:16 +01:00
public $duplicateChecks = array(
'Code' => 'Code',
);
2014-08-15 08:53:05 +02:00
2016-11-29 00:31:16 +01:00
public function __construct($objectClass = null)
{
if (!$objectClass) {
$objectClass = 'SilverStripe\\Security\\Group';
}
2014-08-15 08:53:05 +02:00
2016-11-29 00:31:16 +01:00
parent::__construct($objectClass);
}
2014-08-15 08:53:05 +02:00
2016-11-29 00:31:16 +01:00
public function processRecord($record, $columnMap, &$results, $preview = false)
{
// We match by 'Code', the ID property is confusing the importer
if (isset($record['ID'])) {
unset($record['ID']);
}
2016-11-29 00:31:16 +01:00
$objID = parent::processRecord($record, $columnMap, $results, $preview);
2014-08-15 08:53:05 +02:00
2016-11-29 00:31:16 +01:00
/** @var Group $group */
$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']) {
$parentGroup = DataObject::get_one('SilverStripe\\Security\\Group', array(
'"Group"."Code"' => $record['ParentCode']
));
if ($parentGroup) {
$group->ParentID = $parentGroup->ID;
$group->write();
}
}
2014-08-15 08:53:05 +02:00
2016-11-29 00:31:16 +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) {
$p = DataObject::get_one('SilverStripe\\Security\\Permission', array(
'"Permission"."Code"' => $code,
'"Permission"."GroupID"' => $group->ID
));
if (!$p) {
$p = new Permission(array('Code' => $code));
$p->write();
}
$group->Permissions()->add($p);
}
}
2016-11-29 00:31:16 +01:00
return $objID;
}
}