diff --git a/lib/decrees.js b/lib/decrees.js index 0be4bd574..a71a1521c 100644 --- a/lib/decrees.js +++ b/lib/decrees.js @@ -7,12 +7,11 @@ IMPLEMENTED: RESTRICT_REGISTRATION() UPDATE_DEFAULT_STORAGE() -NOT IMPLEMENTED: - // QUOTA MANAGEMENT -ADD_QUOTA -RM_QUOTA() -UPDATE_QUOTA +SET_QUOTA(, limit) +RM_QUOTA() + +NOT IMPLEMENTED: // RESTRICTED REGISTRATION ADD_INVITE @@ -73,24 +72,34 @@ commands.UPDATE_DEFAULT_STORAGE = function (Env, args) { var Quota = require("./commands/quota"); var Keys = require("./keys"); +var Util = require("./common-util"); -// CryptPad_AsyncStore.rpc.send('ADMIN', [ 'ADMIN_DECREE', ['ADD_QUOTA', ['[user@box:3000/VzeS4vP1DF+tXGuq1i50DKYuBL+09Yqy8kGxoUKRzhA=]', { limit: 2 * 1024 * 1024 * 1024, plan: 'buddy', note: "you're welcome" } ] ] ], console.log) -commands.ADD_QUOTA = function (Env, args) { +var getCanonicalKey = function (input) { + if (typeof(input) !== 'string') { return; } + // key is already in simple form. ensure that it is an 'unsafeKey' + if (input.length === 44) { + return Util.unescapeKeyCharacters(input); + } + try { + return Keys.parseUser(input).pubkey; + } catch (err) { + return; + } +}; + +// 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 publicSigningKey = args[0]; - // this might throw, but that's fine - var user = Keys.parseUser(publicSigningKey); - if (!user || !user.pubkey || user.pubkey.length !== 44) { + var unsafeKey = getCanonicalKey(args[0]); + if (!unsafeKey) { throw new Error("INVALID_ARGS"); } // make sure you're not overwriting an existing limit - if (Env.customLimits[user.pubkey]) { - throw new Error("EEXISTS"); - } + //if (Env.customLimits[unsafeKey]) { throw new Error("EEXISTS"); } var limit = args[1]; if (!Quota.isValidLimit(limit)) { // do we really want this? @@ -98,20 +107,28 @@ commands.ADD_QUOTA = function (Env, args) { } // map the new limit to the user's unsafeKey - Env.customLimits[user.pubkey] = limit; - Env.limits[user.pubkey] = limit; + Env.customLimits[unsafeKey] = limit; + Env.limits[unsafeKey] = limit; return true; }; commands.RM_QUOTA = function (Env, args) { - args = args; - throw new Error("NOT_IMPLEMENTED"); -}; + if (!Array.isArray(args) || args.length !== 1) { + throw new Error("INVALID_ARGS"); + } -commands.UPDATE_QUOTA = function (Env, args) { - args = args; - throw new Error("NOT_IMPLEMENTED"); + var unsafeKey = getCanonicalKey(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; }; // [, , ,