Commit 9835cecb authored by Patricio Bruna's avatar Patricio Bruna

Every function now works with BatchRequest

parent d7e9b763
...@@ -163,6 +163,19 @@ zimbraApi.getAllAccounts(callback, query_object); ...@@ -163,6 +163,19 @@ zimbraApi.getAllAccounts(callback, query_object);
``` ```
## Batch Request Functions ## Batch Request Functions
With `BatchRequest` you can ask Zimbra to run multiple requests in just one call, and get
the result in just one answer.
Every function here works for `BatchRequest` if you do not pass a `callback`. For example:
```javascript
var allAccounts = zimbraApi.getAllAccounts();
var allDomains = zimbraApi.getAllDomains();
zimbraApi.makeBatchRequest([allAccounts, allDomains], callback);
// Object {SearchDirectoryResponse: Array[2], _jsns: "urn:zimbra"}
// SearchDirectoryResponse[0].account, SearchDirectoryResponse[1].domain
```
### Count Accounts for several Domains ### Count Accounts for several Domains
Pass an array of domains ids or names and you get back an array of `countAccounts` responses Pass an array of domains ids or names and you get back an array of `countAccounts` responses
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
...@@ -13,47 +13,33 @@ export default class Domain extends Zimbra { ...@@ -13,47 +13,33 @@ export default class Domain extends Zimbra {
addAdmin(account_id, callback) { addAdmin(account_id, callback) {
const request_data = {}; const request_data = {};
const grantee_data = { 'type': 'usr', 'identifier': account_id }; const grantee_data = { 'type': 'usr', 'identifier': account_id };
let modifyAccountRequest = this.api.modifyAccount(account_id, { zimbraIsDelegatedAdminAccount: 'TRUE' }, callback, true); const modifyAccountRequest = this.api.modifyAccount(account_id, { zimbraIsDelegatedAdminAccount: 'TRUE' });
const grantRightRequest = this.grantRight(grantee_data, this.domainAdminRights, callback, true); const grantRightRequest = this.grantRight(grantee_data, this.domainAdminRights);
request_data.requests = [modifyAccountRequest, grantRightRequest]; request_data.requests = [modifyAccountRequest, grantRightRequest];
request_data.batch = true;
request_data.callback = function(err, data) { request_data.callback = function(err, data) {
if (err) return callback(err); if (err) return callback(err);
callback(null, data.GrantRightResponse); callback(null, data.GrantRightResponse);
}; };
this.api.performRequest(request_data, true); this.api.performRequest(request_data);
} }
// TODO: Fix this fucking ugly code // TODO: Fix this fucking ugly code
getAdmins(callback) { getAdmins(callback) {
const that = this; const that = this;
this.getAdminsIdsFromGrants(function(e,d){ const admins_ids = this.getAdminsIdsFromGrants();
if (e) return callback(e); const query = this.makeAdminIdsQuery(admins_ids);
if (d.length < 1) return callback(null, []); return this.api.getAllAccounts(callback, {query: query});
let query = "(|";
d.forEach((id) => {
const zimbra_id = `(zimbraId=${id})`;
query += zimbra_id;
});
query += ")";
that.api.getAllAccounts(function(e,d){
if (e) return callback(e);
if (d.total > 0) return callback(null, d.account);
return callback(null, []);
}, {query: query});
});
} }
// Return the ZimbraId if the grantee have the domainAdminRights right // Return the ZimbraId if the grantee have the domainAdminRights right
// Grant.right_name() == domainAdminRights // Grant.right_name() == domainAdminRights
getAdminsIdsFromGrants(callback) { getAdminsIdsFromGrants() {
const ids = []; const ids = [];
this.getACLs(function(err, data){ this.parseACL(this.attrs.zimbraACE).forEach((grantee) => {
if (err) return callback(err); if (grantee.right === this.domainAdminRights) ids.push(grantee.id);
data.forEach((grant) => {
if (grant.isDomainAdminGrant()) ids.push(grant.granteeId);
});
return callback(null, ids);
}); });
return ids;
} }
getAllDistributionLists(callback) { getAllDistributionLists(callback) {
...@@ -76,6 +62,16 @@ export default class Domain extends Zimbra { ...@@ -76,6 +62,16 @@ export default class Domain extends Zimbra {
}); });
} }
makeAdminIdsQuery(ids) {
let query = "(|";
ids.forEach((id) => {
const zimbra_id = `(zimbraId=${id})`;
query += zimbra_id;
});
query += ")";
return query;
}
maxAccountsByCos() { maxAccountsByCos() {
const results = {}; const results = {};
if (typeof this.attrs.zimbraDomainCOSMaxAccounts === 'undefined') return null; if (typeof this.attrs.zimbraDomainCOSMaxAccounts === 'undefined') return null;
......
...@@ -34,10 +34,12 @@ export default class Zimbra { ...@@ -34,10 +34,12 @@ export default class Zimbra {
parseACL(acls) { parseACL(acls) {
const elements = [].concat.apply([], [acls]); const elements = [].concat.apply([], [acls]);
const grantees = {}; const grantees = [];
elements.forEach((el) => { // Filter to remove undefined
grantee_data = el.split(/ /); // http://stackoverflow.com/questions/28607451/removing-undefined-values-from-array
grantees[grantee_data[0]] = {type: grantee_data[1], right: grantee_data[2]}; elements.filter(Boolean).forEach((el) => {
const grantee_data = el.split(/ /);
grantees.push({id: grantee_data[0], type: grantee_data[1], right: grantee_data[2]});
}); });
return grantees; return grantees;
} }
......
...@@ -8,63 +8,9 @@ ...@@ -8,63 +8,9 @@
'password':'12345678' 'password':'12345678'
}; };
function loadFixture(fixtureName) {
var req = new XMLHttpRequest();
req.open('GET', 'fixtures/' + fixtureName + '.json', false);
req.send(null);
if (req.status === 200) {
return JSON.parse(req.responseText);
} else {
return null;
}
}
describe('Basic tests', function() { describe('Basic tests', function() {
this.timeout(5000); this.timeout(5000);
it('should return error object when timeout', function() {
let api = new ZimbraAdminApi({
'url': 'http://localhost',
'user': 'user',
'password':'pass'});
api.login(null, function(err){
let error = api.handleError(err);
expect(error.constructor.name).to.equal('Error');
expect(error.title).to.equal('timeout');
});
});
it('return error if wrong validation', function(done) {
var auth_data2 = JSON.parse(JSON.stringify(auth_data));
auth_data2.password = 'abc';
let api = new ZimbraAdminApi(auth_data2);
let callback = function(err, response) {
let error = api.handleError(err);
expect(error.constructor.name).to.equal('Error');
expect(error.extra.code).to.equal('account.AUTH_FAILED');
done();
};
api.login(callback);
});
it('return token if ok validation', function(done) {
let api = new ZimbraAdminApi(auth_data);
let callback = function(err, response) {
if (err) return console.error(err);
expect(api.client.token).to.exist;
done();
};
api.login(callback);
});
it('should delete the password after authentication', function(done) {
let api = new ZimbraAdminApi(auth_data);
let callback = function(err, response) {
expect(api.secret).not.to.exist;
expect(api.password).not.to.exist;
done();
}
api.login(callback);
});
it('should get all domains', function(done) { it('should get all domains', function(done) {
let api = new ZimbraAdminApi(auth_data); let api = new ZimbraAdminApi(auth_data);
...@@ -471,8 +417,9 @@ ...@@ -471,8 +417,9 @@
if (err) console.error(err); if (err) console.error(err);
let domain = data; let domain = data;
domain.getAdmins(function(e, d){ domain.getAdmins(function(e, d){
expect(d.length).to.be.above(1); if (e) return console.error(e);
expect(d[0].constructor.name).to.be.equal('Account'); expect(d.account.length).to.be.above(1);
expect(d.account[0].constructor.name).to.be.equal('Account');
done(); done();
}); });
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment