diff --git a/src/appdb.js b/src/appdb.js index 1eff66ec8..aab7834de 100644 --- a/src/appdb.js +++ b/src/appdb.js @@ -342,6 +342,7 @@ function updateWithConstraints(id, app, constraints, callback) { assert.strictEqual(typeof callback, 'function'); assert(!('portBindings' in app) || typeof app.portBindings === 'object'); assert(!('accessRestriction' in app) || typeof app.accessRestriction === 'object' || app.accessRestriction === ''); + assert(!('alternateDomains' in app) || Array.isArray(alternateDomains)); var queries = [ ]; @@ -363,6 +364,13 @@ function updateWithConstraints(id, app, constraints, callback) { queries.push({ query: 'UPDATE subdomains SET domain = ? WHERE appId = ? AND type = ?', args: [ app.domain, id, exports.SUBDOMAIN_TYPE_PRIMARY ]}); } + if ('alternateDomains' in app) { + queries.push({ query: 'DELETE FROM subdomains WHERE appId = ? AND type = ?', args: [ id, exports.SUBDOMAIN_TYPE_REDIRECT ]}); + app.alternateDomains.forEach(function (d) { + queries.push({ query: 'INSERT INTO subdomains (appId, domain, subdomain, type) VALUES (?, ?, ?, ?)', args: [ id, d.domain, d.subdomain, exports.SUBDOMAIN_TYPE_REDIRECT ]}); + }); + } + var fields = [ ], values = [ ]; for (var p in app) { if (p === 'manifest' || p === 'oldConfig' || p === 'updateConfig' || p === 'restoreConfig' || p === 'accessRestriction' || p === 'debugMode') { diff --git a/src/apps.js b/src/apps.js index ea34687e8..ac4cc9177 100644 --- a/src/apps.js +++ b/src/apps.js @@ -691,6 +691,11 @@ function configure(appId, data, auditSource, callback) { if (error) return callback(error); } + if ('alternateDomains' in data) { + // TODO validate all subdomains [{ domain: '', subdomain: ''}] + values.alternateDomains = data.alternateDomains; + } + domains.get(domain, function (error, domainObject) { if (error && error.reason === DomainsError.NOT_FOUND) return callback(new AppsError(AppsError.NOT_FOUND, 'No such domain')); if (error) return callback(new AppsError(AppsError.INTERNAL_ERROR, 'Could not get domain info:' + error.message)); diff --git a/src/routes/apps.js b/src/routes/apps.js index 6aaa5e8fc..717fad188 100644 --- a/src/routes/apps.js +++ b/src/routes/apps.js @@ -162,6 +162,11 @@ function configureApp(req, res, next) { if ('mailboxName' in data && typeof data.mailboxName !== 'string') return next(new HttpError(400, 'mailboxName must be a string')); + if ('alternateDomains' in data) { + if (!Array.isArray(data.alternateDomains)) return next(new HttpError(400, 'alternateDomains must be an array')); + if (data.alternateDomains.some(function (d) { return (typeof d.domain !== 'string' || typeof d.subdomain !== 'string'); })) return next(new HttpError(400, 'alternateDomains array must contain objects with domain and subdomain strings')); + } + debug('Configuring app id:%s data:%j', req.params.id, data); apps.configure(req.params.id, data, auditSource(req), function (error) {