var Decrees = module.exports; /* Admin decrees which modify global server state IMPLEMENTED: RESTRICT_REGISTRATION() UPDATE_DEFAULT_STORAGE() // QUOTA MANAGEMENT SET_QUOTA(, limit) RM_QUOTA() // INACTIVITY SET_INACTIVE_TIME SET_ACCOUNT_RETENTION_TIME SET_ARCHIVE_RETENTION_TIME // UPLOADS SET_MAX_UPLOAD_SIZE SET_PREMIUM_UPLOAD_SIZE // BACKGROUND PROCESSES DISABLE_INTEGRATED_TASKS DISABLE_INTEGRATED_EVICTION // BROADCAST SET_LAST_BROADCAST_HASH SET_SURVEY_URL SET_MAINTENANCE NOT IMPLEMENTED: // RESTRICTED REGISTRATION ADD_INVITE REVOKE_INVITE REDEEM_INVITE // 2.0 Env.adminEmail Env.supportMailbox Env.DEV_MODE || Env.FRESH_MODE, */ var commands = {}; /* commands have a simple API: * they receive the global Env and the arguments to be applied * if the arguments are invalid the operation will not be applied * the command throws * if the arguments are valid but do not result in a change, the operation is redundant. * return false * if the arguments are valid and will result in a change, the operation should be applied * apply it * return true to indicate that it was applied */ var args_isBoolean = function (args) { return !(!Array.isArray(args) || typeof(args[0]) !== 'boolean'); }; // Toggles a simple boolean var makeBooleanSetter = function (attr) { return function (Env, args) { if (!args_isBoolean(args)) { throw new Error('INVALID_ARGS'); } var bool = args[0]; if (bool === Env[attr]) { return false; } Env[attr] = bool; return true; }; }; // CryptPad_AsyncStore.rpc.send('ADMIN', [ 'ADMIN_DECREE', ['RESTRICT_REGISTRATION', [true]]], console.log) commands.RESTRICT_REGISTRATION = makeBooleanSetter('restrictRegistration'); // CryptPad_AsyncStore.rpc.send('ADMIN', [ 'ADMIN_DECREE', ['DISABLE_INTEGRATED_EVICTION', [true]]], console.log) commands.DISABLE_INTEGRATED_EVICTION = makeBooleanSetter('disableIntegratedEviction'); // CryptPad_AsyncStore.rpc.send('ADMIN', [ 'ADMIN_DECREE', ['DISABLE_INTEGRATED_TASKS', [true]]], console.log) commands.DISABLE_INTEGRATED_TASKS = makeBooleanSetter('disableIntegratedTasks'); /* var isNonNegativeNumber = function (n) { return !(typeof(n) !== 'number' || isNaN(n) || n < 0); }; */ var isInteger = function (n) { return !(typeof(n) !== 'number' || isNaN(n) || (n % 1) !== 0); }; var args_isInteger = function (args) { return !(!Array.isArray(args) || !isInteger(args[0])); }; var makeIntegerSetter = function (attr) { return function (Env, args) { if (!args_isInteger(args)) { throw new Error('INVALID_ARGS'); } var integer = args[0]; if (integer === Env[attr]) { return false; } Env[attr] = integer; return true; }; }; // CryptPad_AsyncStore.rpc.send('ADMIN', [ 'ADMIN_DECREE', ['SET_MAX_UPLOAD_SIZE', [50 * 1024 * 1024]]], console.log) commands.SET_MAX_UPLOAD_SIZE = makeIntegerSetter('maxUploadSize'); // CryptPad_AsyncStore.rpc.send('ADMIN', [ 'ADMIN_DECREE', ['SET_PREMIUM_UPLOAD_SIZE', [150 * 1024 * 1024]]], console.log) commands.SET_PREMIUM_UPLOAD_SIZE = makeIntegerSetter('premiumUploadSize'); // CryptPad_AsyncStore.rpc.send('ADMIN', [ 'ADMIN_DECREE', ['UPDATE_DEFAULT_STORAGE', [100 * 1024 * 1024]]], console.log) commands.UPDATE_DEFAULT_STORAGE = makeIntegerSetter('defaultStorageLimit'); // CryptPad_AsyncStore.rpc.send('ADMIN', [ 'ADMIN_DECREE', ['SET_INACTIVE_TIME', [90]]], console.log) commands.SET_INACTIVE_TIME = makeIntegerSetter('inactiveTime'); // CryptPad_AsyncStore.rpc.send('ADMIN', [ 'ADMIN_DECREE', ['SET_ARCHIVE_RETENTION_TIME', [30]]], console.log) commands.SET_ARCHIVE_RETENTION_TIME = makeIntegerSetter('archiveRetentionTime'); // CryptPad_AsyncStore.rpc.send('ADMIN', [ 'ADMIN_DECREE', ['SET_ACCOUNT_RETENTION_TIME', [365]]], console.log) commands.SET_ACCOUNT_RETENTION_TIME = makeIntegerSetter('accountRetentionTime'); var args_isString = function (args) { return Array.isArray(args) && typeof(args[0]) === "string"; }; // Maintenance: Empty string or an object with a start and end time var isNumber = function (value) { return typeof(value) === "number" && !isNaN(value); }; var args_isMaintenance = function (args) { return Array.isArray(args) && args[0] && (args[0] === "" || (isNumber(args[0].end) && isNumber(args[0].start))); }; // we anticipate that we'll add language-specific surveys in the future // whenever that happens we can relax validation a bit to support more formats var makeBroadcastSetter = function (attr, validation) { return function (Env, args) { if ((validation && !validation(args)) && !args_isString(args)) { throw new Error('INVALID_ARGS'); } var str = args[0]; if (str === Env[attr]) { return false; } Env[attr] = str; Env.broadcastCache = {}; return true; }; }; // CryptPad_AsyncStore.rpc.send('ADMIN', [ 'ADMIN_DECREE', ['SET_LAST_BROADCAST_HASH', [hash]]], console.log) commands.SET_LAST_BROADCAST_HASH = makeBroadcastSetter('lastBroadcastHash'); // CryptPad_AsyncStore.rpc.send('ADMIN', [ 'ADMIN_DECREE', ['SET_SURVEY_URL', [url]]], console.log) commands.SET_SURVEY_URL = makeBroadcastSetter('surveyURL'); // CryptPad_AsyncStore.rpc.send('ADMIN', [ 'ADMIN_DECREE', ['SET_MAINTENANCE', [{start: +Date, end: +Date}]]], console.log) // CryptPad_AsyncStore.rpc.send('ADMIN', [ 'ADMIN_DECREE', ['SET_MAINTENANCE', [""]]], console.log) commands.SET_MAINTENANCE = makeBroadcastSetter('maintenance', args_isMaintenance); var Quota = require("./commands/quota"); var Keys = require("./keys"); var Util = require("./common-util"); // CryptPad_AsyncStore.rpc.send('ADMIN', [ 'ADMIN_DECREE', ['SET_QUOTA', ['[user@box:3000/VzeS4vP1DF+tXGuq1i50DKYuBL+09Yqy8kGxoUKRzhA=]', { limit: 2 * 1024 * 1024 * 1024, plan: 'buddy', note: "you're welcome" } ] ] ], console.log) commands.SET_QUOTA = function (Env, args) { if (!Array.isArray(args) || args.length !== 2) { throw new Error("INVALID_ARGS"); } var unsafeKey = Keys.canonicalize(args[0]); if (!unsafeKey) { throw new Error("INVALID_ARGS"); } // make sure you're not overwriting an existing limit //if (Env.customLimits[unsafeKey]) { throw new Error("EEXISTS"); } var limit = args[1]; if (!Quota.isValidLimit(limit)) { // do we really want this? throw new Error("INVALID_ARGS"); } limit.origin = 'decree'; // map the new limit to the user's unsafeKey Env.customLimits[unsafeKey] = limit; Env.limits[unsafeKey] = limit; return true; }; commands.RM_QUOTA = function (Env, args) { if (!Array.isArray(args) || args.length !== 1) { throw new Error("INVALID_ARGS"); } var unsafeKey = Keys.canonicalize(args[0]); if (!unsafeKey) { throw new Error("INVALID_ARGS"); } if (!Env.customLimits[unsafeKey]) { throw new Error("ENOENT"); } delete Env.customLimits[unsafeKey]; delete Env.limits[unsafeKey]; return true; }; // [, , ,