From: Marcin Haba Date: Thu, 18 Aug 2022 11:04:56 +0000 (+0200) Subject: baculum: Add Microsoft 365 email attachment list endpoint X-Git-Tag: Release-13.0.2~66 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1fca23e2af1e5897eb4d44225241fb14a7e5a437;p=thirdparty%2Fbacula.git baculum: Add Microsoft 365 email attachment list endpoint --- diff --git a/gui/baculum/protected/API/Pages/API/PluginM365EmailAttachmentList.php b/gui/baculum/protected/API/Pages/API/PluginM365EmailAttachmentList.php new file mode 100644 index 000000000..ae50d033a --- /dev/null +++ b/gui/baculum/protected/API/Pages/API/PluginM365EmailAttachmentList.php @@ -0,0 +1,176 @@ + + * @category API + * @package Baculum API + */ +class PluginM365EmailAttachmentList extends ConsoleOutputJSONPage { + + public function get() { + $misc = $this->getModule('misc'); + $client = null; + $clientid = $this->Request->contains('id') ? (int)$this->Request['id'] : 0; + $result = $this->getModule('bconsole')->bconsoleCommand( + $this->director, + ['.client'], + null, + true + ); + if ($result->exitcode === 0) { + $client_val = $this->getModule('client')->getClientById($clientid); + if (is_object($client_val) && in_array($client_val->name, $result->output)) { + $client = $client_val->name; + } else { + $this->output = ClientError::MSG_ERROR_CLIENT_DOES_NOT_EXISTS; + $this->error = ClientError::ERROR_CLIENT_DOES_NOT_EXISTS; + return; + } + } else { + $this->output = PluginError::MSG_ERROR_WRONG_EXITCODE; + $this->error = PluginError::ERROR_WRONG_EXITCODE; + return; + } + + $tenantid = $this->Request->contains('tenantid') ? $this->Request['tenantid'] : null; + $fd_plugin_cfg = $this->getModule('fd_plugin_cfg')->getConfig('m365', $client, $tenantid); + if (!empty($tenantid) && (!$misc->isValidUUID($tenantid) || count($fd_plugin_cfg) == 0)) { + $this->output = PluginM365Error::MSG_ERROR_TENANT_DOES_NOT_EXISTS; + $this->error = PluginM365Error::ERROR_TENANT_DOES_NOT_EXISTS; + return; + } + $tenant = $fd_plugin_cfg['tenant_name']; + + $params = [ + 'client' => $client, + 'tenant' => $tenant + ]; + if ($this->Request->contains('mailbox') && $misc->isValidEmail($this->Request['mailbox'])) { + $params['mailbox'] = $this->Request['mailbox']; + } + if ($this->Request->contains('limit') && $misc->isValidInteger($this->Request['limit'])) { + $params['limit'] = (int)$this->Request['limit']; + } + if ($this->Request->contains('name') && $misc->isValidName($this->Request['name'])) { + $params['name'] = $this->Request['name']; + } + if ($this->Request->contains('contenttype') && $misc->isValidPath($this->Request['contenttype'])) { + $params['contenttype'] = $this->Request['contenttype']; + } + if ($this->Request->contains('minsize') && $misc->isValidInteger($this->Request['minsize'])) { + $params['minsize'] = $this->Request['minsize']; + } + if ($this->Request->contains('maxsize') && $misc->isValidInteger($this->Request['maxsize'])) { + $params['maxsize'] = $this->Request['maxsize']; + } + if ($this->Request->contains('isinline') && $misc->isValidInteger($this->Request['isinline'])) { + $params['isinline'] = $this->Request['isinline']; + } + $out = $this->getJSONOutput($params); + + $output = []; + $error = PluginM365Error::ERROR_NO_ERRORS; + if ($out->exitcode === 0) { + $output = $out->output; + } else { + $error = PluginM365Error::ERROR_WRONG_EXITCODE; + $output = PluginM365Error::MSG_ERROR_WRONG_EXITCODE . implode(PHP_EOL, $out->output); + } + $this->output = $output; + $this->error = $error; + } + + /** + * Get M365 email attachment list in JSON string format. + * + * @param array $params command parameters + * @return StdClass object with output and exitcode + */ + protected function getRawOutput($params = []) { + $cmd = [ + '.jlist', + 'metadata', + 'type="attachment"', + 'tenant="' . $params['tenant'] . '"' + ]; + + if (key_exists('mailbox', $params)) { + $cmd[] ='owner="' . $params['mailbox'] . '"'; + } + if (key_exists('limit', $params)) { + $cmd[] ='limit="' . $params['limit'] . '"'; + } + if (key_exists('name', $params)) { + $cmd[] ='name="' . $params['name'] . '"'; + } + if (key_exists('contenttype', $params)) { + $cmd[] ='contenttype="' . $params['contenttype'] . '"'; + } + if (key_exists('minsize', $params)) { + $cmd[] ='minsize="' . $params['minsize'] . '"'; + } + if (key_exists('maxsize', $params)) { + $cmd[] ='maxsize="' . $params['maxsize'] . '"'; + } + if (key_exists('isinline', $params)) { + $cmd[] ='isinline="' . $params['isinline'] . '"'; + } + $ret = $this->getModule('bconsole')->bconsoleCommand( + $this->director, + $cmd + ); + + if ($ret->exitcode !== 0) { + $this->getModule('logging')->log( + Logging::CATEGORY_EXECUTE, + 'Wrong output from m365 RAW attachment list: ' . implode(PHP_EOL, $ret->output) + ); + } + return $ret; + } + + /** + * Get M365 email attachment list in validated and formatted JSON format. + * + * @param array $params command parameters + * @return StdClass object with output and exitcode + */ + protected function getJSONOutput($params = []) { + $ret = $this->getRawOutput($params); + if ($ret->exitcode === 0) { + $ret->output = $this->parseOutput($ret->output); + if ($ret->output->error === 0) { + $ret->output = $ret->output->data; + } else { + $ret->output = $ret->output->errmsg; + } + } + return $ret; + } +} diff --git a/gui/baculum/protected/API/Pages/API/endpoints.xml b/gui/baculum/protected/API/Pages/API/endpoints.xml index bf6416936..c2ca1f06d 100644 --- a/gui/baculum/protected/API/Pages/API/endpoints.xml +++ b/gui/baculum/protected/API/Pages/API/endpoints.xml @@ -126,6 +126,7 @@ + diff --git a/gui/baculum/protected/API/openapi_baculum.json b/gui/baculum/protected/API/openapi_baculum.json index 4f39bbda2..e5a83996d 100644 --- a/gui/baculum/protected/API/openapi_baculum.json +++ b/gui/baculum/protected/API/openapi_baculum.json @@ -149,6 +149,15 @@ }, "PluginM365Email": { "$ref": "#/definitions/PluginM365Email" + }, + "PluginM365EmailAttachments": { + "type": "array", + "items": { + "$ref": "#/definitions/PluginM365EmailAttachment" + } + }, + "PluginM365EmailAttachment": { + "$ref": "#/definitions/PluginM365EmailAttachment" } }, "parameters": { @@ -6690,7 +6699,7 @@ "name": "mailbox", "in": "query", "required": false, - "description": "Mailbox address", + "description": "Mailbox email address", "schema": { "type": "string" } @@ -6796,6 +6805,100 @@ } ] } + }, + "/api/v2/plugins/m365/{clientid}/{tenantid}/emails/attachments": { + "get": { + "tags": ["plugins"], + "summary": "Microsoft 365 plugin email attachment list", + "description": "Get Microsoft 365 email attachment list.", + "responses": { + "200": { + "description": "List of Microsoft 365 email attachments", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "output": { + "$ref": "#/components/schemas/PluginM365EmailAttachments" + }, + "error": { + "type": "integer", + "description": "Error code", + "enum": [0, 1, 4, 5, 6, 7, 11, 1000] + } + } + } + } + } + } + }, + "parameters": [ + { + "$ref": "#/components/parameters/ClientId" + }, + { + "$ref": "#/components/parameters/TenantId" + }, + { + "$ref": "#/components/parameters/Limit" + }, + { + "name": "mailbox", + "in": "query", + "required": false, + "description": "Mailbox email address", + "schema": { + "type": "string" + } + }, + { + "name": "name", + "in": "query", + "required": false, + "description": "Attachment name", + "schema": { + "type": "string" + } + }, + { + "name": "contenttype", + "in": "query", + "required": false, + "description": "Attachment content type (ex. image/jpeg)", + "schema": { + "type": "string" + } + }, + { + "name": "minsize", + "in": "query", + "required": false, + "description": "Attachment minimum size in bytes", + "schema": { + "type": "integer" + } + }, + { + "name": "maxsize", + "in": "query", + "required": false, + "description": "Attachment maximum size in bytes", + "schema": { + "type": "integer" + } + }, + { + "name": "isinline", + "in": "query", + "required": false, + "description": "Is inline flag that informs if email is attached in-line in email body or as attachment outside content", + "schema": { + "type": "integer" + } + } + ] + } } }, "definitions": { @@ -7781,6 +7884,51 @@ "type": "string" } } + }, + "PluginM365EmailAttachment": { + "type": "object", + "properties": { + "jobid": { + "description": "Job identifier", + "type": "integer" + }, + "fileindex": { + "description": "File index", + "type": "integer" + }, + "attachmenttenant": { + "description": "Tenant name", + "type": "string" + }, + "attachmentowner": { + "description": "Email owner", + "type": "string" + }, + "attachmentcontenttype": { + "description": "Attachment content type (ex. image/jpeg)", + "type": "string" + }, + "attachmentemailid": { + "description": "Attachment email identifier", + "type": "string" + }, + "attachmentisinline": { + "description": "Is inline flag that informs if email is attached in-line in email body or as attachment outside content", + "type": "integer" + }, + "attachmentname": { + "description": "Attachment file name", + "type": "string" + }, + "attachmentsize": { + "description": "Attachment file size", + "type": "string" + }, + "plugin": { + "description": "Plugin name", + "type": "string" + } + } } } }