Skip to content
Snippets Groups Projects
Commit dac11e66 authored by Peter Rotich's avatar Peter Rotich
Browse files

Initial concept for user import/export

Implement CLI user import/export in a well formatted CSV file. Name and
email address are the only supported fields at the moment.
parent 30472267
No related branches found
No related tags found
No related merge requests found
......@@ -394,7 +394,7 @@ class PersonsName {
function __toString() {
global $cfg;
$format = $cfg->getDefaultNameFormat();
$format = $cfg ? $cfg->getDefaultNameFormat() : 'original';
list(,$func) = static::$formats[$format];
if (!$func) $func = 'getFull';
return call_user_func(array($this, $func));
......
<?php
require_once dirname(__file__) . "/class.module.php";
require_once dirname(__file__) . "/../cli.inc.php";
class UserManager extends Module {
var $prologue = 'CLI user manager';
var $arguments = array(
'action' => array(
'help' => 'Action to be performed',
'options' => array(
'import' => 'Import users to the system',
'export' => 'Export users from the system',
),
),
);
var $options = array(
'file' => array('-f', '--file', 'metavar'=>'path',
'help' => 'File or stream to process'),
);
var $stream;
function run($args, $options) {
Bootstrap::connect();
osTicket::start();
switch ($args['action']) {
case 'import':
if (!$options['file'])
$this->fail('CSV file to import users from is required!');
elseif (!($this->stream = fopen($options['file'], 'rb')))
$this->fail("Unable to open input file [{$options['file']}]");
while (($data = fgetcsv($this->stream, 1000, ",")) !== FALSE)
if (!User::fromVars(array('name' => $data[0], 'email' => $data[1])))
$this->stderr->write('Unable to import user: '.print_r($data, true));
break;
case 'export':
$stream = $options['file'] ?: 'php://stdout';
if (!($this->stream = fopen($stream, 'c')))
$this->fail("Unable to open output file [{$options['file']}]");
foreach (User::objects() as $user)
fputcsv($this->stream,
array((string) $user->getName(), $user->getEmail()));
break;
default:
$this->stderr->write('Unknown action!');
}
@fclose($this->stream);
}
}
Module::register('user', 'UserManager');
?>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment