From 80b87eae081165b94b405b2e33958862cefdf37b Mon Sep 17 00:00:00 2001 From: Andris Reinman Date: Thu, 14 Jun 2018 11:36:06 +0300 Subject: [PATCH] If counter key value is modified then reset counter in redis --- lib/user-handler.js | 27 +++++++++++++++++++++++++++ package.json | 2 +- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/user-handler.js b/lib/user-handler.js index 44c09095..2e918e07 100644 --- a/lib/user-handler.js +++ b/lib/user-handler.js @@ -1030,6 +1030,7 @@ class UserHandler { // quota storageUsed: 0, quota: data.quota || 0, + recipients: data.recipients || 0, forwards: data.forwards || 0, @@ -2453,10 +2454,26 @@ class UserHandler { let updates = false; let passwordChanged = false; + // if some of the counter keys are modified, then reset the according value in Redis + let resetKeys = new Map([ + ['recipients', 'wdr'], + ['forwards', 'wdf'], + ['imapMaxUpload', 'iup'], + ['imapMaxDownload', 'idw'], + ['pop3MaxDownload', 'pdw'], + ['receivedMax', 'rl:rcpt'] + ]); + let flushKeys = []; + Object.keys(data).forEach(key => { if (['user', 'existingPassword', 'ip'].includes(key)) { return; } + + if (resetKeys.has(key)) { + flushKeys.push(resetKeys.get(key) + ':' + user); + } + if (key === 'password') { if (data[key] === false) { // removes current password (if set) @@ -2617,6 +2634,16 @@ class UserHandler { return callback(err); } + // check if we need to reset any ttl counters + if (flushKeys.length) { + let flushreq = this.redis.multi(); + flushKeys.forEach(key => { + flushreq = flushreq.del(key); + }); + // just call the operations and hope for the best, no problems if fails + flushreq.exec(() => false); + } + this.userCache.flush(user, () => false); if (passwordChanged) { diff --git a/package.json b/package.json index a329f392..df086ff6 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,7 @@ "dependencies": { "addressparser": "1.0.1", "bcryptjs": "2.4.3", - "bugsnag": "2.4.1", + "bugsnag": "2.4.2", "generate-password": "1.4.0", "he": "1.1.1", "html-to-text": "4.0.0",