-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathapicontroller.class.php
More file actions
227 lines (219 loc) · 9.26 KB
/
apicontroller.class.php
File metadata and controls
227 lines (219 loc) · 9.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<?php
/**
* -------------------------------------------------------------------------
* DataInjection plugin for GLPI
* -------------------------------------------------------------------------
*
* LICENSE
*
* This file is part of DataInjection.
*
* DataInjection is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* DataInjection is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with DataInjection. If not, see <http://www.gnu.org/licenses/>.
* -------------------------------------------------------------------------
* @copyright Copyright (C) 2007-2023 by DataInjection plugin team.
* @license GPLv2 https://www.gnu.org/licenses/gpl-2.0.html
* @link https://github.com/pluginsGLPI/datainjection
* -------------------------------------------------------------------------
*/
use Glpi\Api\HL\Controller\AbstractController;
use Glpi\Api\HL\Doc as Doc;
use Glpi\Api\HL\Route;
use Glpi\Api\HL\Search;
use Glpi\Http\JSONResponse;
use Glpi\Http\Request;
use Glpi\Http\Response;
use GuzzleHttp\Psr7\Utils;
#[Route(path: '/Plugin/DataInjection', requirements: [
'id' => '\d+'
], priority: 1, tags: ['Data Injection'])]
class PluginDatainjectionApiController extends AbstractController
{
protected static function getRawKnownSchemas(): array
{
return [
'DataInjectionModel' => [
'type' => Doc\Schema::TYPE_OBJECT,
'x-itemtype' => PluginDatainjectionModel::class,
'properties' => [
'id' => [
'type' => Doc\Schema::TYPE_INTEGER,
'format' => Doc\Schema::FORMAT_INTEGER_INT64,
],
'name' => ['type' => Doc\Schema::TYPE_STRING],
'comment' => ['type' => Doc\Schema::TYPE_STRING],
'itemtype' => ['type' => Doc\Schema::TYPE_STRING],
'entity' => self::getDropdownTypeSchema(class: Entity::class, full_schema: 'Entity'),
'date_creation' => ['type' => Doc\Schema::TYPE_STRING, 'format' => Doc\Schema::FORMAT_STRING_DATE_TIME],
'date_mod' => ['type' => Doc\Schema::TYPE_STRING, 'format' => Doc\Schema::FORMAT_STRING_DATE_TIME],
'allow_add' => [
'type' => Doc\Schema::TYPE_BOOLEAN,
'x-field' => 'behavior_add',
'description' => 'Allow creation of new items'
],
'allow_update' => [
'type' => Doc\Schema::TYPE_BOOLEAN,
'x-field' => 'behavior_update',
'description' => 'Allow update of existing items'
],
'allow_add_linked_items' => [
'type' => Doc\Schema::TYPE_BOOLEAN,
'x-field' => 'can_add_dropdown',
'description' => 'Allow creation of dropdowns/linked items'
],
'allow_overwrite_fields' => [
'type' => Doc\Schema::TYPE_BOOLEAN,
'x-field' => 'can_overwrite_if_not_empty',
'description' => 'Allow update of existing fields'
],
'is_private' => ['type' => Doc\Schema::TYPE_BOOLEAN],
'user' => self::getDropdownTypeSchema(class: User::class, full_schema: 'User'),
'date_format' => ['type' => Doc\Schema::TYPE_STRING],
'float_format' => ['type' => Doc\Schema::TYPE_STRING],
'unique_ports' => [
'type' => Doc\Schema::TYPE_BOOLEAN,
'x-field' => 'port_unicity',
],
'csv_options' => [
'type' => Doc\Schema::TYPE_OBJECT,
'x-join' => [
'table' => 'glpi_plugin_datainjection_modelcsvs',
'field' => 'models_id',
'fkey' => 'id',
'primary-property' => 'id',
],
'properties' => [
'id' => [
'type' => Doc\Schema::TYPE_INTEGER,
'format' => Doc\Schema::FORMAT_INTEGER_INT64,
],
'model' => self::getDropdownTypeSchema(class: PluginDatainjectionModel::class, field: 'models_id'),
'delimiter' => [
'type' => Doc\Schema::TYPE_STRING,
],
'is_header_present' => [
'type' => Doc\Schema::TYPE_BOOLEAN,
],
]
]
]
]
];
}
#[Route(path: '/Model', methods: ['GET'])]
#[Doc\Route(
description: 'List or search data injection models',
responses: [
['schema' => 'DataInjectionModel[]']
]
)]
public function getModels(Request $request): Response
{
$response = Search::searchBySchema($this->getKnownSchema('DataInjectionModel'), $request->getParameters());
$decoded = json_decode((string) $response->getBody(), true);
foreach ($decoded as &$model) {
// Keep only the id and name of the model
$model['csv_options']['model'] = [
'id' => $model['csv_options']['model']['id'],
'name' => $model['csv_options']['model']['name'],
];
}
unset($model);
$response = $response->withBody(Utils::streamFor(json_encode($decoded)));
return $response;
}
#[Route(path: '/Model/{id}', methods: ['GET'])]
#[Doc\Route(
description: 'Get a data injection model',
responses: [
['schema' => 'DataInjectionModel']
]
)]
public function getModel(Request $request): Response
{
return Search::getOneBySchema($this->getKnownSchema('DataInjectionModel'), $request->getAttributes(), $request->getParameters());
}
#[Route(path: '/Model/{id}/Injection', methods: ['POST'])]
#[Doc\Route(
description: 'Inject data using the specified model',
parameters: [
[
'name' => '_',
'location' => Doc\Parameter::LOCATION_BODY,
'schema' => [
'type' => Doc\Schema::TYPE_OBJECT,
'properties' => [
'filename' => [
'type' => Doc\Schema::TYPE_STRING,
'format' => Doc\Schema::FORMAT_STRING_BINARY
]
]
],
'required' => false,
'content_type' => 'multipart/form-data'
],
]
)]
public function inject(Request $request): Response
{
$model = new PluginDatainjectionModel();
if (!$model->getFromDB($request->getAttribute('id'))) {
return self::getNotFoundErrorResponse();
}
$results = [];
foreach ($request->getUploadedFiles() as $file) {
$options = [
'file_encoding' => PluginDatainjectionBackend::ENCODING_AUTO,
'from_api' => true,
'original_filename' => $file['name'],
'unique_filename' => $file['tmp_name'],
'mode' => PluginDatainjectionModel::PROCESS,
'delete_file' => true,
];
$action_results = [];
$success = $model->processUploadedFile($options);
if (!$success) {
return new JSONResponse(
self::getErrorResponseBody(AbstractController::ERROR_GENERIC, 'Error processing file'),
400
);
}
$engine = new PluginDatainjectionEngine($model, [], $_SESSION['glpiactive_entity'] ?? 0);
$first = true;
foreach ($model->injectionData->getData() as $id => $data) {
if ($first && $model->getSpecificModel()->isHeaderPresent()) {
$first = false;
} else {
$action_results[] = $engine->injectLine($data[0], $id);
}
}
$model->cleanData();
$results[] = [
'filename' => $file['name'],
'results' => $action_results
];
}
return new JSONResponse($results);
}
#[Route(path: '/Itemtype', methods: ['GET'])]
#[Doc\Route(
description: 'List item types',
responses: [
['schema' => 'string[]']
]
)]
public function getItemtypes(Request $request): Response
{
return new JSONResponse(PluginDatainjectionInjectionType::getItemtypes());
}
}