Files
cloudron-box/src/routes/settings.js

266 lines
9.0 KiB
JavaScript
Raw Normal View History

'use strict';
exports = module.exports = {
getAutoupdatePattern: getAutoupdatePattern,
setAutoupdatePattern: setAutoupdatePattern,
getCloudronName: getCloudronName,
setCloudronName: setCloudronName,
getCloudronAvatar: getCloudronAvatar,
setCloudronAvatar: setCloudronAvatar,
2017-03-09 15:43:13 -08:00
getEmailStatus: getEmailStatus,
getDnsConfig: getDnsConfig,
2015-10-27 18:38:13 +01:00
setDnsConfig: setDnsConfig,
2015-11-09 20:34:25 -08:00
getBackupConfig: getBackupConfig,
setBackupConfig: setBackupConfig,
2016-05-03 12:10:16 -07:00
getTimeZone: getTimeZone,
2016-06-02 13:36:47 -07:00
setTimeZone: setTimeZone,
2016-05-03 12:10:16 -07:00
getMailConfig: getMailConfig,
setMailConfig: setMailConfig,
2016-07-26 15:32:36 +02:00
getAppstoreConfig: getAppstoreConfig,
setAppstoreConfig: setAppstoreConfig,
2016-07-26 14:42:40 +02:00
2017-01-17 09:51:04 -08:00
setFallbackCertificate: setFallbackCertificate,
setAdminCertificate: setAdminCertificate
};
var assert = require('assert'),
2015-12-11 13:52:21 -08:00
certificates = require('../certificates.js'),
CertificatesError = require('../certificates.js').CertificatesError,
2017-01-10 18:39:40 -08:00
config = require('../config.js'),
HttpError = require('connect-lastmile').HttpError,
HttpSuccess = require('connect-lastmile').HttpSuccess,
safe = require('safetydance'),
settings = require('../settings.js'),
SettingsError = settings.SettingsError;
function getAutoupdatePattern(req, res, next) {
settings.getAutoupdatePattern(function (error, pattern) {
if (error) return next(new HttpError(500, error));
next(new HttpSuccess(200, { pattern: pattern }));
});
}
function setAutoupdatePattern(req, res, next) {
assert.strictEqual(typeof req.body, 'object');
if (typeof req.body.pattern !== 'string') return next(new HttpError(400, 'pattern is required'));
settings.setAutoupdatePattern(req.body.pattern, function (error) {
2016-06-02 13:00:23 -07:00
if (error && error.reason === SettingsError.BAD_FIELD) return next(new HttpError(400, error.message));
if (error) return next(new HttpError(500, error));
next(new HttpSuccess(200));
});
}
function setCloudronName(req, res, next) {
assert.strictEqual(typeof req.body, 'object');
if (typeof req.body.name !== 'string') return next(new HttpError(400, 'name is required'));
settings.setCloudronName(req.body.name, function (error) {
2016-06-02 13:00:23 -07:00
if (error && error.reason === SettingsError.BAD_FIELD) return next(new HttpError(400, error.message));
if (error) return next(new HttpError(500, error));
2016-06-02 13:00:23 -07:00
next(new HttpSuccess(202));
});
}
function getCloudronName(req, res, next) {
settings.getCloudronName(function (error, name) {
if (error) return next(new HttpError(500, error));
2016-06-02 13:00:23 -07:00
next(new HttpSuccess(200, { name: name }));
});
}
2016-05-03 12:10:16 -07:00
function getTimeZone(req, res, next) {
settings.getTimeZone(function (error, tz) {
if (error) return next(new HttpError(500, error));
2016-06-02 13:00:23 -07:00
2016-05-03 12:10:16 -07:00
next(new HttpSuccess(200, { timeZone: tz }));
});
}
2016-06-02 13:36:47 -07:00
function setTimeZone(req, res, next) {
assert.strictEqual(typeof req.body, 'object');
if (typeof req.body.timeZone !== 'string') return next(new HttpError(400, 'timeZone is required'));
settings.setTimeZone(req.body.timeZone, function (error) {
if (error && error.reason === SettingsError.BAD_FIELD) return next(new HttpError(400, error.message));
if (error) return next(new HttpError(500, error));
next(new HttpSuccess(200));
});
}
function getMailConfig(req, res, next) {
settings.getMailConfig(function (error, mail) {
if (error) return next(new HttpError(500, error));
next(new HttpSuccess(200, mail));
});
}
function setMailConfig(req, res, next) {
assert.strictEqual(typeof req.body, 'object');
if (typeof req.body.enabled !== 'boolean') return next(new HttpError(400, 'enabled is required'));
settings.setMailConfig({ enabled: req.body.enabled }, function (error) {
if (error && error.reason === SettingsError.BAD_FIELD) return next(new HttpError(400, error.message));
if (error) return next(new HttpError(500, error));
next(new HttpSuccess(200));
});
}
function setCloudronAvatar(req, res, next) {
assert.strictEqual(typeof req.files, 'object');
if (!req.files.avatar) return next(new HttpError(400, 'avatar must be provided'));
var avatar = safe.fs.readFileSync(req.files.avatar.path);
settings.setCloudronAvatar(avatar, function (error) {
if (error) return next(new HttpError(500, error));
2016-06-02 13:00:23 -07:00
next(new HttpSuccess(202, {}));
});
}
function getCloudronAvatar(req, res, next) {
settings.getCloudronAvatar(function (error, avatar) {
if (error) return next(new HttpError(500, error));
2015-07-23 14:44:05 +02:00
// avoid caching the avatar on the client to see avatar changes immediately
res.set('Cache-Control', 'no-cache');
res.set('Content-Type', 'image/png');
res.status(200).send(avatar);
});
}
2017-03-09 15:43:13 -08:00
function getEmailStatus(req, res, next) {
settings.getEmailStatus(function (error, records) {
if (error) return next(new HttpError(500, error));
next(new HttpSuccess(200, records));
});
}
function getDnsConfig(req, res, next) {
settings.getDnsConfig(function (error, config) {
if (error) return next(new HttpError(500, error));
next(new HttpSuccess(200, config));
});
}
function setDnsConfig(req, res, next) {
assert.strictEqual(typeof req.body, 'object');
2015-10-29 12:25:04 -07:00
if (typeof req.body.provider !== 'string') return next(new HttpError(400, 'provider is required'));
2017-01-10 18:39:40 -08:00
settings.setDnsConfig(req.body, config.fqdn(), function (error) {
if (error && error.reason === SettingsError.BAD_FIELD) return next(new HttpError(400, error.message));
if (error) return next(new HttpError(500, error));
next(new HttpSuccess(200));
});
}
2015-10-27 18:38:13 +01:00
2015-11-09 20:34:25 -08:00
function getBackupConfig(req, res, next) {
settings.getBackupConfig(function (error, config) {
if (error) return next(new HttpError(500, error));
next(new HttpSuccess(200, config));
});
}
function setBackupConfig(req, res, next) {
assert.strictEqual(typeof req.body, 'object');
if (typeof req.body.provider !== 'string') return next(new HttpError(400, 'provider is required'));
if ('key' in req.body && typeof req.body.key !== 'string') return next(new HttpError(400, 'key must be a string'));
2015-11-09 20:34:25 -08:00
settings.setBackupConfig(req.body, function (error) {
if (error && error.reason === SettingsError.BAD_FIELD) return next(new HttpError(400, error.message));
if (error && error.reason === SettingsError.EXTERNAL_ERROR) return next(new HttpError(402, error.message));
2015-11-09 20:34:25 -08:00
if (error) return next(new HttpError(500, error));
next(new HttpSuccess(200));
});
}
2016-07-26 15:32:36 +02:00
function getAppstoreConfig(req, res, next) {
settings.getAppstoreConfig(function (error, result) {
2016-07-26 14:42:40 +02:00
if (error) return next(new HttpError(500, error));
next(new HttpSuccess(200, result));
});
}
2016-07-26 15:32:36 +02:00
function setAppstoreConfig(req, res, next) {
2016-07-26 14:42:40 +02:00
assert.strictEqual(typeof req.body, 'object');
if (typeof req.body.userId !== 'string') return next(new HttpError(400, 'userId is required'));
if (typeof req.body.token !== 'string') return next(new HttpError(400, 'token is required'));
var options = {
userId: req.body.userId,
token: req.body.token
};
2016-07-26 15:32:36 +02:00
settings.setAppstoreConfig(options, function (error) {
2016-07-26 14:42:40 +02:00
if (error && error.reason === SettingsError.BAD_FIELD) return next(new HttpError(400, error.message));
if (error && error.reason === SettingsError.EXTERNAL_ERROR) return next(new HttpError(406, error.message));
2016-07-26 14:42:40 +02:00
if (error) return next(new HttpError(500, error));
settings.getAppstoreConfig(function (error, result) {
if (error) return next(new HttpError(500, error));
next(new HttpSuccess(202, result));
});
2016-07-26 14:42:40 +02:00
});
}
// default fallback cert
2017-01-17 09:51:04 -08:00
function setFallbackCertificate(req, res, next) {
assert.strictEqual(typeof req.body, 'object');
2015-10-27 18:38:13 +01:00
if (!req.body.cert || typeof req.body.cert !== 'string') return next(new HttpError(400, 'cert must be a string'));
if (!req.body.key || typeof req.body.key !== 'string') return next(new HttpError(400, 'key must be a string'));
2015-10-27 18:38:13 +01:00
2015-12-11 14:13:24 -08:00
certificates.setFallbackCertificate(req.body.cert, req.body.key, function (error) {
2015-12-11 13:52:21 -08:00
if (error && error.reason === CertificatesError.INVALID_CERT) return next(new HttpError(400, error.message));
if (error) return next(new HttpError(500, error));
next(new HttpSuccess(202, {}));
});
}
// only webadmin cert, until it can be treated just like a normal app
function setAdminCertificate(req, res, next) {
assert.strictEqual(typeof req.body, 'object');
if (!req.body.cert || typeof req.body.cert !== 'string') return next(new HttpError(400, 'cert must be a string'));
if (!req.body.key || typeof req.body.key !== 'string') return next(new HttpError(400, 'key must be a string'));
2015-12-11 13:52:21 -08:00
certificates.setAdminCertificate(req.body.cert, req.body.key, function (error) {
if (error && error.reason === CertificatesError.INVALID_CERT) return next(new HttpError(400, error.message));
2015-10-27 18:38:13 +01:00
if (error) return next(new HttpError(500, error));
next(new HttpSuccess(202, {}));
});
}