diff --git a/backend/internal/proxy-host.js b/backend/internal/proxy-host.js index 32f2bc0dc..65947aa87 100644 --- a/backend/internal/proxy-host.js +++ b/backend/internal/proxy-host.js @@ -258,6 +258,56 @@ const internalProxyHost = { }); }, + /** + * @param {Access} access + * @param {Object} data + * @param {String} data.domain + * @param {Array} [data.expand] + * @param {Array} [data.omit] + * @return {Promise} + */ + getByDomain: (access, data) => { + if (typeof data === 'undefined') { + data = {}; + } + + return access.can('proxy_hosts:get', data.domain) + .then((access_data) => { + let query = proxyHostModel + .query() + .where('is_deleted', 0) + .andWhere(castJsonIfNeed('domain_names'), 'like', '%' + data.domain + '%') + .allowGraph('[owner,access_list.[clients,items],certificate]') + .modify(function(queryBuilder) { + if (data.expand) { + queryBuilder.withGraphFetched(`[${data.expand.join(', ')}]`); + } + }) + .first(); + + if (access_data.permission_visibility !== 'all') { + query.andWhere('owner_user_id', access.token.getUserId(1)); + } + + if (typeof data.expand !== 'undefined' && data.expand !== null) { + query.withGraphFetched('[' + data.expand.join(', ') + ']'); + } + + return query.then(utils.omitRow(omissions())); + }) + .then((row) => { + if (!row || !row.id) { + throw new error.ItemNotFoundError(data.id); + } + row = internalHost.cleanRowCertificateMeta(row); + // Custom omissions + if (typeof data.omit !== 'undefined' && data.omit !== null) { + row = _.omit(row, data.omit); + } + return row; + }); + }, + /** * @param {Access} access * @param {Object} data diff --git a/backend/routes/nginx/proxy_hosts.js b/backend/routes/nginx/proxy_hosts.js index 3be4582a8..c123963cb 100644 --- a/backend/routes/nginx/proxy_hosts.js +++ b/backend/routes/nginx/proxy_hosts.js @@ -146,6 +146,52 @@ router .catch(next); }); +/** + * Specific proxy-host by domain + * + * /api/nginx/proxy-hosts/domain/:domain + */ +router + .route('/domain/:domain') + .options((req, res) => { + res.sendStatus(204); + }) + .all(jwtdecode()) + + /** + * GET /api/nginx/proxy-hosts/domain/:domain + * + * Retrieve a specific proxy-host by domain + */ + .get((req, res, next) => { + validator({ + required: ['domain'], + additionalProperties: false, + properties: { + domain: { + type: 'string' + }, + expand: { + $ref: 'common#/properties/expand' + } + } + }, { + domain: req.params.domain, + expand: (typeof req.query.expand === 'string' ? req.query.expand.split(',') : null) + }) + .then((data) => { + return internalProxyHost.getByDomain(res.locals.access, { + domain: data.domain, + expand: data.expand + }); + }) + .then((row) => { + res.status(200) + .send(row); + }) + .catch(next); + }); + /** * Enable proxy-host *