From: Marcin Haba Date: Mon, 17 Apr 2023 14:21:54 +0000 (+0200) Subject: Add new m365 plugin mailbox list endpoint X-Git-Tag: Release-13.0.3~76 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c54b63a2319e9c55f5afe639d85e1859c8b30a3e;p=thirdparty%2Fbacula.git Add new m365 plugin mailbox list endpoint --- diff --git a/gui/baculum/protected/API/Pages/API/PluginM365MailboxList.php b/gui/baculum/protected/API/Pages/API/PluginM365MailboxList.php new file mode 100644 index 000000000..4e7f160a0 --- /dev/null +++ b/gui/baculum/protected/API/Pages/API/PluginM365MailboxList.php @@ -0,0 +1,148 @@ + + * @category API + * @package Baculum API + */ +class PluginM365MailboxList 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('offset') && $misc->isValidInteger($this->Request['offset'])) { + $params['offset'] = (int)$this->Request['offset']; + } + if ($this->Request->contains('limit') && $misc->isValidInteger($this->Request['limit'])) { + $params['limit'] = (int)$this->Request['limit']; + } + $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 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="email"', + 'tenant="' . $params['tenant'] . '"' + ]; + + if (key_exists('offset', $params)) { + $cmd[] ='offset="' . $params['offset'] . '"'; + } + if (key_exists('limit', $params)) { + $cmd[] ='limit="' . $params['limit'] . '"'; + } + $ret = $this->getModule('bconsole')->bconsoleCommand( + $this->director, + $cmd + ); + + if ($ret->exitcode !== 0) { + $this->getModule('logging')->log( + Logging::CATEGORY_EXECUTE, + 'Wrong output from m365 RAW mailbox list: ' . implode(PHP_EOL, $ret->output) + ); + } + return $ret; + } + + /** + * Get M365 email 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 82400e8bf..49710576a 100644 --- a/gui/baculum/protected/API/Pages/API/endpoints.xml +++ b/gui/baculum/protected/API/Pages/API/endpoints.xml @@ -142,6 +142,7 @@ + diff --git a/gui/baculum/protected/API/openapi_baculum.json b/gui/baculum/protected/API/openapi_baculum.json index 7119e415a..a084dafc2 100644 --- a/gui/baculum/protected/API/openapi_baculum.json +++ b/gui/baculum/protected/API/openapi_baculum.json @@ -8652,6 +8652,52 @@ ] } }, + "/api/v2/plugins/m365/{clientid}/{tenantid}/mailboxes": { + "get": { + "tags": ["plugins"], + "summary": "Microsoft 365 plugin mailbox list", + "description": "Get Microsoft 365 mailbox list.", + "responses": { + "200": { + "description": "List of Microsoft 365 mailboxes", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "output": { + "type": "array", + "items": { + "type": "object", + "properties": { + "emailowner": { + "type": "string", + "description": "Mailbox" + } + } + } + }, + "error": { + "type": "integer", + "description": "Error code", + "enum": [0, 1, 4, 5, 6, 7, 11, 1000] + } + } + } + } + } + } + }, + "parameters": [ + { + "$ref": "#/components/parameters/Offset" + }, + { + "$ref": "#/components/parameters/Limit" + } + ] + } + }, "/api/v2/plugins/m365/{clientid}/{tenantid}/jobs": { "get": { "tags": ["plugins"],