Skip to content

Commit ddc516f

Browse files
committed
Merge pull request #192 from tdt/development
Version 4.1.0
2 parents b012aa1 + 33216ca commit ddc516f

File tree

223 files changed

+18693
-418
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

223 files changed

+18693
-418
lines changed

app/commands/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.json

app/commands/Export.php

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
<?php
2+
3+
use Illuminate\Console\Command;
4+
use Symfony\Component\Console\Input\InputOption;
5+
use Symfony\Component\Console\Input\InputArgument;
6+
7+
use tdt\commands\ie\Definitions;
8+
use tdt\commands\ie\Users;
9+
use tdt\commands\ie\Groups;
10+
11+
class Export extends Command {
12+
13+
/**
14+
* The console command name.
15+
*
16+
* @var string
17+
*/
18+
protected $name = 'datatank:export';
19+
20+
/**
21+
* The console command description.
22+
*
23+
* @var string
24+
*/
25+
protected $description = 'Export functionality for the datatank configuration. By default it exports all of the users and definitions in JSON.';
26+
27+
/**
28+
* Create a new command instance.
29+
*
30+
* @return void
31+
*/
32+
public function __construct()
33+
{
34+
parent::__construct();
35+
}
36+
37+
/**
38+
* Execute the console command.
39+
*
40+
* Ask the user which functionality he wants to use (definitions|users)
41+
* then based on the input, proceed with the logic for chosen option.
42+
*
43+
* This option is given as an input argument upon making the command.
44+
*
45+
* @return void
46+
*/
47+
public function fire()
48+
{
49+
// Check for filename
50+
$filename = $this->option('file');
51+
52+
// Get the optional identifier
53+
$identifier = $this->argument('identifier');
54+
55+
// Create new object for the export data;
56+
$export_data = new \stdClass();
57+
58+
59+
if($this->option('definitions')){
60+
61+
$export_data->definitions = Definitions::export($identifier);
62+
63+
if(!empty($identifier)){
64+
65+
// When the identifier doesn't check out, give the user another chance to fill it out
66+
while(empty($export_data->definitions)){
67+
68+
$this->error("The resource with identifier '$identifier' could not be found.");
69+
70+
// Prompt new indentifier
71+
$identifier = $this->ask('Provide the full identifier of the definition you want (e.g. csv/cities): ');
72+
73+
// Check if the resource id is legit
74+
$export_data->definitions = Definitions::export($identifier);
75+
}
76+
}
77+
78+
}elseif($this->option('users')){
79+
80+
// Export users
81+
$export_data->users = Users::export();
82+
// Export groups
83+
$export_data->groups = Groups::export();
84+
85+
}else{
86+
87+
// Export definitions
88+
$export_data->definitions = Definitions::export();
89+
90+
// Export users
91+
$export_data->users = Users::export();
92+
// Export groups
93+
$export_data->groups = Groups::export();
94+
}
95+
96+
97+
// JSON encode
98+
if (defined('JSON_PRETTY_PRINT')) {
99+
$export_data = json_encode($export_data, JSON_PRETTY_PRINT);
100+
}else{
101+
$export_data = json_encode($export_data);
102+
}
103+
// JSON_UNESCAPED_SLASHES only available in PHP 5.4
104+
$export_data = str_replace('\/', '/', $export_data);
105+
106+
107+
// Output
108+
if(empty($filename)){
109+
// Print to console
110+
echo $export_data;
111+
}else{
112+
try{
113+
// Write to file
114+
file_put_contents($filename, $export_data);
115+
$this->info("The export has been written to the file '$filename'.");
116+
}catch(Exception $e){
117+
$this->error("The contents could not be written to the file '$filename'.");
118+
die();
119+
}
120+
}
121+
122+
}
123+
124+
/**
125+
* Get the console command arguments.
126+
*
127+
* @return array
128+
*/
129+
protected function getArguments()
130+
{
131+
return array(
132+
array('identifier', InputArgument::OPTIONAL, 'If you specify the option -d, you can export a single definition by specifying the indentifier of that definition (e.g. csv/cities)'),
133+
);
134+
}
135+
136+
/**
137+
* Get the console command options.
138+
*
139+
* @return array
140+
*/
141+
protected function getOptions()
142+
{
143+
return array(
144+
array('definitions', 'd', InputOption::VALUE_NONE, 'Only export the definitions, if you specify an identifier only that definition will be exported.', null),
145+
array('users', 'u', InputOption::VALUE_NONE, 'Only export the users.', null),
146+
array('file', 'f', InputOption::VALUE_OPTIONAL, 'Write the export data to a file.', null),
147+
);
148+
}
149+
150+
}

app/commands/Import.php

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
<?php
2+
3+
use Illuminate\Console\Command;
4+
use Symfony\Component\Console\Input\InputOption;
5+
use Symfony\Component\Console\Input\InputArgument;
6+
use Illuminate\Filesystem\Filesystem;
7+
8+
use tdt\commands\ie\Definitions;
9+
use tdt\commands\ie\Users;
10+
use tdt\commands\ie\Groups;
11+
12+
class Import extends Command {
13+
14+
/**
15+
* The console command name.
16+
*
17+
* @var string
18+
*/
19+
protected $name = 'datatank:import';
20+
21+
/**
22+
* The console command description.
23+
*
24+
* @var string
25+
*/
26+
protected $description = 'Import functionality for a datatank configuration, can import users and resources passed in a JSON file.';
27+
28+
/**
29+
* Create a new command instance.
30+
*
31+
* @return void
32+
*/
33+
public function __construct()
34+
{
35+
parent::__construct();
36+
}
37+
38+
/**
39+
* Execute the console command.
40+
*
41+
* @return void
42+
*/
43+
public function fire()
44+
{
45+
46+
// Fetch the json file from the arguments
47+
$file = $this->argument('file');
48+
49+
// Check if the file exists
50+
if(\File::exists($file)){
51+
52+
// JSON decode
53+
$content = json_decode(\File::get($file), true);
54+
55+
if($content){
56+
57+
// Check for user & groups
58+
if(!empty($content['users']) && is_array($content['users'])
59+
&& !empty($content['groups']) && is_array($content['groups'])){
60+
61+
$this->info("\n——————————————————————————————————");
62+
$this->info("Users & groups found, importing...");
63+
$this->info("——————————————————————————————————");
64+
65+
// Import groups
66+
$messages = Groups::import($content['groups']);
67+
68+
foreach($messages as $group => $status){
69+
if($status){
70+
$this->info("• Group '$group' succesfully added.");
71+
}else{
72+
$this->error("• Group '$group' already existed, ignored.");
73+
}
74+
}
75+
76+
// Import users
77+
$messages = Users::import($content['users']);
78+
79+
foreach($messages as $user => $status){
80+
if($status){
81+
$this->info("• User '$user' succesfully added.");
82+
}else{
83+
$this->error("• User '$user' already existed, ignored.");
84+
}
85+
}
86+
}
87+
88+
// Check for definitions
89+
if(!empty($content['definitions']) && is_array($content['definitions'])){
90+
91+
$this->info("———————————————————————————————");
92+
$this->info("Definitions found, importing...");
93+
$this->info("———————————————————————————————");
94+
95+
// Ask for a set of credentials that have definition-create permissions
96+
$username = $this->ask("Enter a username that has permission to create definitions: ");
97+
$password = $this->secret("Enter the corresponding password: ");
98+
99+
$data = array();
100+
$data['username'] = $username;
101+
$data['password'] = $password;
102+
$data['definitions'] = $content['definitions'];
103+
104+
$messages = Definitions::import($data);
105+
106+
foreach($messages as $identifier => $status){
107+
if($status){
108+
$this->info("• Definition with '$identifier' succesfully added.");
109+
}else{
110+
$this->error("Something went wrong when trying to adding the definition '$identifier', check the logs for indications of what may have gone wrong.");
111+
}
112+
}
113+
}
114+
115+
$this->info("\nCompleted task");
116+
}else{
117+
$this->error("The given file doesn't contain valid JSON.");
118+
die();
119+
}
120+
}else{
121+
$this->error("The given file '$file' can't be found on the system.");
122+
die();
123+
}
124+
}
125+
126+
/**
127+
* Get the console command arguments.
128+
*
129+
* @return array
130+
*/
131+
protected function getArguments()
132+
{
133+
return array(
134+
array("file", InputArgument::REQUIRED, "The path to the JSON export file.", null),
135+
);
136+
}
137+
138+
/**
139+
* Get the console command options.
140+
*
141+
* @return array
142+
*/
143+
protected function getOptions()
144+
{
145+
return array(
146+
);
147+
}
148+
149+
}

app/commands/ie/Definitions.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
namespace tdt\commands\ie;
4+
5+
use tdt\core\definitions\DefinitionController;
6+
7+
/**
8+
* Import/export definitions
9+
* @copyright (C) 2011,2013 by OKFN Belgium vzw/asbl
10+
* @license AGPLv3
11+
* @author Michiel Vancoillie <[email protected]>
12+
*/
13+
class Definitions implements IImportExport {
14+
15+
public static function import($data){
16+
17+
$definitions = $data['definitions'];
18+
$username = $data['username'];
19+
$password = $data['password'];
20+
21+
// Basic auth header
22+
$auth_header = "Basic " . base64_encode(trim($username) . ":" . trim($password));
23+
24+
$messages = array();
25+
26+
foreach($definitions as $identifier => $definition_params){
27+
28+
$headers = array(
29+
'Content-Type' => 'application/tdt.definition+json',
30+
'Authorization' => $auth_header,
31+
);
32+
33+
self::updateRequest('PUT', $headers, $definition_params);
34+
35+
// Add the new definition
36+
$response = DefinitionController::handle($identifier);
37+
$status_code = $response->getStatusCode();
38+
39+
$messages[$identifier] = ($status_code == 200);
40+
}
41+
42+
return $messages;
43+
44+
}
45+
46+
public static function export($identifier = null){
47+
if(empty($identifier)){
48+
// Request all of the definitions
49+
return DefinitionController::getAllDefinitions();
50+
}else{
51+
// Request a single definition
52+
$definition = DefinitionController::get($identifier);
53+
return array($identifier => $definition->getAllParameters());
54+
}
55+
}
56+
57+
58+
/**
59+
* Custom API call function
60+
*/
61+
public static function updateRequest($method, $headers = array(), $data = array()){
62+
63+
// Set the custom headers.
64+
\Request::getFacadeRoot()->headers->replace($headers);
65+
66+
// Set the custom method.
67+
\Request::setMethod($method);
68+
69+
// Set the content body.
70+
if(is_array($data)){
71+
\Input::merge($data);
72+
}
73+
}
74+
75+
}
76+

0 commit comments

Comments
 (0)