diff --git a/src/mail.js b/src/mail.js index d2feadb66..a7721a61d 100644 --- a/src/mail.js +++ b/src/mail.js @@ -910,7 +910,7 @@ function addMailbox(name, domain, userId, callback) { if (error && error.reason === UserError.NOT_FOUND) return callback(new MailError(MailError.NOT_FOUND, 'no such user')); if (error) return callback(new MailError(MailError.INTERNAL_ERROR, error)); - mailboxdb.add(name, domain, userId, mailboxdb.TYPE_USER, function (error) { + mailboxdb.addMailbox(name, domain, userId, mailboxdb.TYPE_USER, function (error) { if (error && error.reason === DatabaseError.ALREADY_EXISTS) return callback(new MailError(MailError.ALREADY_EXISTS, 'mailbox already exists')); if (error) return callback(new MailError(MailError.INTERNAL_ERROR, error)); @@ -925,7 +925,7 @@ function updateMailbox(name, domain, userId, callback) { assert.strictEqual(typeof userId, 'string'); assert.strictEqual(typeof callback, 'function'); - mailboxdb.update(name, domain, userId, mailboxdb.TYPE_USER, function (error) { + mailboxdb.updateMailbox(name, domain, userId, mailboxdb.TYPE_USER, function (error) { if (error && error.reason === DatabaseError.NOT_FOUND) return callback(new MailError(MailError.NOT_FOUND, 'no such mailbox')); if (error) return callback(new MailError(MailError.INTERNAL_ERROR, error)); @@ -1032,7 +1032,7 @@ function addList(domain, listName, groupId, callback) { if (error && error.reason === GroupError.NOT_FOUND) return callback(new MailError(MailError.NOT_FOUND, 'no such group')); if (error) return callback(new MailError(MailError.INTERNAL_ERROR, error)); - mailboxdb.add(listName, domain, groupId, mailboxdb.TYPE_GROUP, function (error) { + mailboxdb.addList(listName, domain, groupId, mailboxdb.TYPE_GROUP, function (error) { if (error && error.reason === DatabaseError.ALREADY_EXISTS) return callback(new MailError(MailError.ALREADY_EXISTS, 'list already exits')); if (error) return callback(new MailError(MailError.INTERNAL_ERROR, error)); @@ -1047,7 +1047,7 @@ function updateList(name, domain, groupId, callback) { assert.strictEqual(typeof groupId, 'string'); assert.strictEqual(typeof callback, 'function'); - mailboxdb.update(name, domain, groupId, mailboxdb.TYPE_GROUP, function (error) { + mailboxdb.updateList(name, domain, groupId, mailboxdb.TYPE_GROUP, function (error) { if (error && error.reason === DatabaseError.NOT_FOUND) return callback(new MailError(MailError.NOT_FOUND, 'no such mailbox')); if (error) return callback(new MailError(MailError.INTERNAL_ERROR, error)); diff --git a/src/mailboxdb.js b/src/mailboxdb.js index d990012da..a6dc8ef22 100644 --- a/src/mailboxdb.js +++ b/src/mailboxdb.js @@ -1,8 +1,11 @@ 'use strict'; exports = module.exports = { - add: add, - update: update, + addMailbox: addMailbox, + addList: addList, + + updateMailbox: updateMailbox, + updateList: updateList, del: del, listAliases: listAliases, @@ -36,7 +39,7 @@ var assert = require('assert'), var MAILBOX_FIELDS = [ 'name', 'ownerId', 'ownerType', 'aliasTarget', 'creationTime', 'domain' ].join(','); -function add(name, domain, ownerId, ownerType, callback) { +function addMailbox(name, domain, ownerId, ownerType, callback) { assert.strictEqual(typeof name, 'string'); assert.strictEqual(typeof domain, 'string'); assert.strictEqual(typeof ownerId, 'string'); @@ -51,7 +54,37 @@ function add(name, domain, ownerId, ownerType, callback) { }); } -function update(name, domain, ownerId, ownerType, callback) { +function updateMailbox(name, domain, ownerId, ownerType, callback) { + assert.strictEqual(typeof name, 'string'); + assert.strictEqual(typeof domain, 'string'); + assert.strictEqual(typeof ownerId, 'string'); + assert.strictEqual(typeof ownerType, 'string'); + assert.strictEqual(typeof callback, 'function'); + + database.query('UPDATE mailboxes SET ownerId = ? WHERE name = ? AND domain = ? AND ownerType = ?', [ ownerId, name, domain, ownerType ], function (error, result) { + if (error) return callback(new DatabaseError(DatabaseError.INTERNAL_ERROR, error)); + if (result.affectedRows === 0) return callback(new DatabaseError(DatabaseError.NOT_FOUND)); + + callback(null); + }); +} + +function addList(name, domain, ownerId, ownerType, callback) { + assert.strictEqual(typeof name, 'string'); + assert.strictEqual(typeof domain, 'string'); + assert.strictEqual(typeof ownerId, 'string'); + assert.strictEqual(typeof ownerType, 'string'); + assert.strictEqual(typeof callback, 'function'); + + database.query('INSERT INTO mailboxes (name, domain, ownerId, ownerType) VALUES (?, ?, ?, ?)', [ name, domain, ownerId, ownerType ], function (error) { + if (error && error.code === 'ER_DUP_ENTRY') return callback(new DatabaseError(DatabaseError.ALREADY_EXISTS, 'mailbox already exists')); + if (error) return callback(new DatabaseError(DatabaseError.INTERNAL_ERROR, error)); + + callback(null); + }); +} + +function updateList(name, domain, ownerId, ownerType, callback) { assert.strictEqual(typeof name, 'string'); assert.strictEqual(typeof domain, 'string'); assert.strictEqual(typeof ownerId, 'string'); diff --git a/src/test/database-test.js b/src/test/database-test.js index 32669c383..21f6d3db7 100644 --- a/src/test/database-test.js +++ b/src/test/database-test.js @@ -1596,21 +1596,21 @@ describe('database', function () { }); it('add user mailbox succeeds', function (done) { - mailboxdb.add('girish', DOMAIN_0.domain, 'uid-0', mailboxdb.TYPE_USER, function (error) { + mailboxdb.addMailbox('girish', DOMAIN_0.domain, 'uid-0', mailboxdb.TYPE_USER, function (error) { expect(error).to.be(null); done(); }); }); it('cannot add dup entry', function (done) { - mailboxdb.add('girish', DOMAIN_0.domain, 'uid-1', mailboxdb.TYPE_APP, function (error) { + mailboxdb.addMailbox('girish', DOMAIN_0.domain, 'uid-1', mailboxdb.TYPE_APP, function (error) { expect(error.reason).to.be(DatabaseError.ALREADY_EXISTS); done(); }); }); it('add app mailbox succeeds', function (done) { - mailboxdb.add('support', DOMAIN_0.domain, 'osticket', mailboxdb.TYPE_APP, function (error) { + mailboxdb.addMailbox('support', DOMAIN_0.domain, 'osticket', mailboxdb.TYPE_APP, function (error) { expect(error).to.be(null); done(); }); diff --git a/src/test/ldap-test.js b/src/test/ldap-test.js index bb575c636..41eae6499 100644 --- a/src/test/ldap-test.js +++ b/src/test/ldap-test.js @@ -103,7 +103,7 @@ function setup(done) { appdb.update.bind(null, APP_0.id, { containerId: APP_0.containerId }), appdb.setAddonConfig.bind(null, APP_0.id, 'sendmail', [{ name: 'MAIL_SMTP_PASSWORD', value : 'sendmailpassword' }]), appdb.setAddonConfig.bind(null, APP_0.id, 'recvmail', [{ name: 'MAIL_IMAP_PASSWORD', value : 'recvmailpassword' }]), - mailboxdb.add.bind(null, APP_0.location + '.app', APP_0.domain, APP_0.id, mailboxdb.TYPE_APP), + mailboxdb.addMailbox.bind(null, APP_0.location + '.app', APP_0.domain, APP_0.id, mailboxdb.TYPE_APP), function (callback) { user.createOwner(USER_0.username, USER_0.password, USER_0.email, USER_0.displayName, AUDIT_SOURCE, function (error, result) { @@ -739,7 +739,7 @@ describe('Ldap', function () { describe('search mailbox', function () { before(function (done) { - mailboxdb.add(USER_0.username.toLowerCase(), DOMAIN_0.domain, USER_0.id, mailboxdb.TYPE_USER, done); + mailboxdb.addMailbox(USER_0.username.toLowerCase(), DOMAIN_0.domain, USER_0.id, mailboxdb.TYPE_USER, done); }); it('get specific mailbox by email', function (done) { @@ -824,7 +824,7 @@ describe('Ldap', function () { describe('search mailing list', function () { before(function (done) { - mailboxdb.add(GROUP_NAME, DOMAIN_0.domain, GROUP_ID, mailboxdb.TYPE_GROUP, done); + mailboxdb.addList(GROUP_NAME, DOMAIN_0.domain, GROUP_ID, mailboxdb.TYPE_GROUP, done); }); it('get specific list', function (done) {