diff --git a/src/mail.js b/src/mail.js index 9f1279c12..cae866766 100644 --- a/src/mail.js +++ b/src/mail.js @@ -1193,26 +1193,30 @@ async function updateMailbox(name, domain, data, auditSource) { assert.strictEqual(typeof data, 'object'); assert.strictEqual(typeof auditSource, 'object'); - const { ownerId, ownerType, active, enablePop3, storageQuota, messagesQuota } = data; - assert.strictEqual(typeof ownerId, 'string'); - assert.strictEqual(typeof ownerType, 'string'); - assert.strictEqual(typeof active, 'boolean'); - assert.strictEqual(typeof enablePop3, 'boolean'); - assert(Number.isInteger(storageQuota) && storageQuota >= 0); - assert(Number.isInteger(messagesQuota) && messagesQuota >= 0); + const args = []; + const fields = []; + for (const k in data) { + if (k === 'enablePop3' || k === 'active') { + fields.push(k + ' = ?'); + args.push(data[k] ? 1 : 0); + continue; + } - name = name.toLowerCase(); + if (k === 'ownerType' && !OWNERTYPES.includes(data[k])) throw new BoxError(BoxError.BAD_FIELD, 'bad owner type'); - if (!OWNERTYPES.includes(ownerType)) throw new BoxError(BoxError.BAD_FIELD, 'bad owner type'); + fields.push(k + ' = ?'); + args.push(data[k]); + } + args.push(name.toLowerCase()); + args.push(domain); const mailbox = await getMailbox(name, domain); if (!mailbox) throw new BoxError(BoxError.NOT_FOUND, 'No such mailbox'); - const result = await database.query('UPDATE mailboxes SET ownerId = ?, ownerType = ?, active = ?, enablePop3 = ?, storageQuota = ?, messagesQuota = ? WHERE name = ? AND domain = ?', - [ ownerId, ownerType, active, enablePop3, storageQuota, messagesQuota, name, domain ]); + const result = await safe(database.query('UPDATE mailboxes SET ' + fields.join(', ') + ' WHERE name = ? AND domain = ?', args)); if (result.affectedRows === 0) throw new BoxError(BoxError.NOT_FOUND, 'Mailbox not found'); - await eventlog.add(eventlog.ACTION_MAIL_MAILBOX_UPDATE, auditSource, { name, domain, oldUserId: mailbox.userId, ownerId, ownerType, active, storageQuota, messagesQuota }); + await eventlog.add(eventlog.ACTION_MAIL_MAILBOX_UPDATE, auditSource, Object.assign(data, { name, domain, oldUserId: mailbox.userId }) ); } async function removeSolrIndex(mailbox) { diff --git a/src/test/ldap-test.js b/src/test/ldap-test.js index 1204f0259..248792fba 100644 --- a/src/test/ldap-test.js +++ b/src/test/ldap-test.js @@ -71,7 +71,7 @@ describe('Ldap', function () { before(function (done) { async.series([ setup, - async () => await mail.addMailbox(mailboxName, domain.domain, { ownerId: user.id, ownerType: mail.OWNERTYPE_USER, active: true }, auditSource), + async () => await mail.addMailbox(mailboxName, domain.domain, { ownerId: user.id, ownerType: mail.OWNERTYPE_USER, active: true, storageQuota: 0, messagesQuota: 0 }, auditSource), async () => await mail.setAliases(mailboxName, domain.domain, [ { name: mailAliasName, domain: domain.domain} ], auditSource), ldapServer.start.bind(null), async () => {