Commit 631ba8a7 authored by Patricio Bruna's avatar Patricio Bruna

Commit

parents 091e2577 b4421e32
{
"esnext": true
"esnext": true,
"node": true,
"predef": [ "describe", "it", "beforeEach", "afterEach" ]
}
......@@ -9,7 +9,10 @@ server.js
webpack.config.js
lib/
TODOS.md
<<<<<<< HEAD
=======
>>>>>>> manager
.*
### JetBrains template
......
......@@ -306,21 +306,16 @@ account.setPassword(password, callback);
### Enable and Disable Archive
**Only Zimbra Network Edition**
`Enable` can take the following options:
* **create**: `(0|1)` if the archive mailbox should be created. Default its 1, create.
* **name**: Name of the archive mailbox, if empty the template will be used.
* **cos_id**: Name or ID of the COS to assign to the archive mailbox.
* **password**: password of the archive mailbox.
* **attributes**.
You **must** pass a `COS Name` or `CosID` as firt params
```javascript
account.enableArchive({ cos_id: 'default' }, callback);
// {}
account.enableArchiving('default', callback);
// Account {}
// account.archiveEnabled === true;
account.disableArchive(callback);
// {}
account.disableArchiving(callback);
// Account {}
// account.archiveEnabled === false;
```
### Get Mailbox
......@@ -389,6 +384,20 @@ zimbraApi.getAllCos(callback);
## Domains
This are functions especifics to `Domains`.
### isAliasDomain & masterDomainName
These are `properties`, **not functions**.
* `isAliasDomain`, return if a Domain is an Alias Domain.
* `masterDomainName`, return the name of the master domain.
```
domain.isAliasDomain
// true || false
domain.masterDomainName
// example.com
```
### Count Accounts
Count number of accounts by `CoS` in a domain.
......@@ -437,7 +446,7 @@ const coses = ['default', 'test', 'professional'];
domain.addAdmin(account.id, coses, callback);
// {} if Success
domain.removeAdmin(account.id, callback);
domain.removeAdmin(account.id, coses, callback);
// {} if Success
```
......
This diff is collapsed.
{
"name": "zimbra-admin-api-js",
<<<<<<< HEAD
"version": "0.2.9",
=======
"version": "0.2.10",
>>>>>>> manager
"main": "src/index.js",
"dependencies": {
"crypto-browserify": "^3.11.0",
......
......@@ -216,7 +216,15 @@ class ZimbraAdminApi {
// type: (account|cos|dl|domain),
// identifier: (name or zimbraId)
// }
grantRight(target_data, grantee_data, right_name, callback) {
// Right {
// deny: 0|1,
// canDelegate: 0|1,
// disinheritSubGroups: 0|1,
// subDomain: 0|1,
// _content: <RightName>
// }
grantRight(target_data, grantee_data, right, callback) {
const request_data = this.buildRequestData('GrantRight', callback);
const target_grantee = this.dictionary.buildTargetGrantee(target_data, grantee_data);
const target = target_grantee[0];
......@@ -224,7 +232,7 @@ class ZimbraAdminApi {
request_data.parse_response = ResponseParser.emptyResponse;
request_data.params.params.grantee = grantee;
request_data.params.params.target = target;
request_data.params.params.right = { '_content': right_name };
request_data.params.params.right = right;
return this.performRequest(request_data);
}
......@@ -266,12 +274,12 @@ class ZimbraAdminApi {
request_data.parse_response = ResponseParser.emptyResponse;
const account = { by: this.dictionary.byIdOrName(account_id), _content: account_id };
const archive = {
create: (options.archive || 1),
name: { '_content': options.name },
cos: { by: this.dictionary.byIdOrName(options.cos_id), '_content': options.cos_id },
password: { '_content': options.password },
a: this.dictionary.attributesToArray(options.attributes)
create: (options.create || 1),
cos: { by: this.dictionary.byIdOrName(options.cos), '_content': options.cos }
};
if (options.name) archive.name = { '_content': options.name };
if (options.password) archive.password = { '_content': options.password };
if (options.attributes) archive.a = this.dictionary.attributesToArray(options.attributes || {});
request_data.params.params.account = account;
request_data.params.params.archive = archive;
return this.performRequest(request_data);
......@@ -336,6 +344,28 @@ class ZimbraAdminApi {
return this.create('DistributionList', resource_data, callback);
}
// flushData = {
// type: Comma separated list of cache types. e.g. from skin|locale|account|cos|domain|server|zimlet,
// allServers: 0|1,
// entry: Name or Id of the object, should be relevant to type
// }
flushCache(flushData = {}, callback) {
const request_data = this.buildRequestData('FlushCache', callback);
request_data.parse_response = ResponseParser.emptyResponse;
request_data.params.params = {
cache: {
type: flushData.type,
allServers: (flushData.allServers || 0),
'_content': { entry: { } }
}
};
if (flushData.entry) request_data.params.params.cache._content.entry = {
'by': this.dictionary.byIdOrName(flushData.entry),
'_content': flushData.entry
}
return this.performRequest(request_data);
}
getAllDomains(callback, query_object) {
query_object = query_object || {};
query_object.types = 'domains';
......
......@@ -9,6 +9,7 @@ class Account extends Zimbra {
constructor(account_obj, zimbra_api_client) {
super(account_obj, zimbra_api_client);
this.domain = this.name.split(/@/)[1];
this.archiveEnabled = this.attrs.zimbraArchiveEnabled === 'TRUE';
}
addAccountAlias(alias, callback) {
......@@ -35,12 +36,21 @@ class Account extends Zimbra {
}
}
enableArchive(options, callback) {
return this.api.enableArchive(this.id, options, callback);
enableArchiving(cos, callback) {
const that = this;
const options = { cos: cos };
return this.api.enableArchive(this.id, options, function(err, data){
if (err) return callback(err);
return that.api.getAccount(that.id, callback);
});
}
disableArchive(callback) {
return this.api.disableArchive(this.id, callback);
disableArchiving(callback) {
const that = this;
return this.api.disableArchive(this.id, function(err, data){
if (err) return callback(err);
return that.api.getAccount(that.id, callback);
});
}
setPassword(password, callback) {
......
......@@ -17,12 +17,23 @@ class DistributionList extends Zimbra {
this.api.addDistributionListMember(this.id, members, callback);
}
// addOwner(account_id, callback) {
// const grantee_data = {
// 'type': 'usr',
// 'identifier': account_id
// }
// this.grantRight(grantee_data, this.ownerRights, callback);
// }
addOwner(account_id, callback) {
const grantee_data = {
'type': 'usr',
'identifier': account_id
}
this.grantRight(grantee_data, this.ownerRights, callback);
const zimbraACES = this.attrs.zimbraACE ? [].concat.apply([], [this.attrs.zimbraACE]) : [];
this.api.getAccount(account_id, (err, data) => {
if (err) return callback(err);
const account = data;
const newZimbraACE = `${account.id} usr sendToDistList`;
zimbraACES.push(newZimbraACE);
const attrs = {zimbraACE: zimbraACES};
return this.api.modifyDistributionList(this.id, attrs, callback);
});
}
// return the ID of the owner
......@@ -69,12 +80,26 @@ class DistributionList extends Zimbra {
this.api.removeDistributionListMember(this.id, members, callback);
}
// removeOwner(account_id, callback) {
// const grantee_data = {
// 'type': 'usr',
// 'identifier': account_id
// }
// this.revokeRight(grantee_data, this.ownerRights, callback);
// }
removeOwner(account_id, callback) {
const grantee_data = {
'type': 'usr',
'identifier': account_id
}
this.revokeRight(grantee_data, this.ownerRights, callback);
if (!this.attrs.zimbraACE) return this;
const zimbraACES = [].concat.apply([], [this.attrs.zimbraACE]);
this.api.getAccount(account_id, (err, account) => {
if (err) return callback(err);
const newACES = zimbraACES.filter((ace) =>{
const granteeId = ace.split(/ /)[0];
if (account.id !== granteeId) return true;
return false;
});
const attrs = (newACES.length > 0) ? {zimbraACE: newACES} : {zimbraACE: ''};
return this.api.modifyDistributionList(this.id, attrs, callback);
});
}
rename(new_name, callback) {
......
......@@ -9,60 +9,104 @@ class Domain extends Zimbra {
constructor(domain_obj, zimbra_api_client) {
super(domain_obj, zimbra_api_client);
this.domainAdminRights = 'domainAdminRights';
this.checkAliasDomain();
}
// TODO: Too ugly code
<<<<<<< HEAD
addAdmin(account_id, coses, callback) {
const request_data = {};
=======
addAdmin(account_id, coses = [], callback) {
>>>>>>> manager
const grantee_data = { 'type': 'usr', 'identifier': account_id };
const modifyAccountRequest = this.api.modifyAccount(account_id, { zimbraIsDelegatedAdminAccount: 'TRUE' });
const grantRightRequest = this.grantRight(grantee_data, this.domainAdminRights);
const cosesRights = this.buildCosesGrantsRequest(coses, grantee_data);
request_data.requests = [modifyAccountRequest, grantRightRequest];
request_data.requests = request_data.requests.concat(cosesRights);
request_data.batch = true;
request_data.callback = function(err, data) {
const specialRighsReqs = this.grantSpecialDomainRights(grantee_data);
const cosRights = this.assignCosRights(grantee_data, coses, null);
const rightReqs = specialRighsReqs.concat(cosRights.requests);
this.addDelegatedAttributeToAccount(account_id, (err,data) => {
if (err) return callback(err);
callback(null, data.GrantRightResponse);
};
this.api.performRequest(request_data);
this.api.makeBatchRequest(rightReqs, (err, data) => {
if (err) return callback(err);
return this.api.getDomain(this.id, callback);
}, {onError: 'continue'});
});
}
// This function grants several rights needed to manage the domain, as:
// * access to modify DLs owners,
// * access to modify domain Amavis Lists
// * access to add other domain admins
// * access to modify account cos
grantSpecialDomainRights(grantee_data) {
const requests = [];
requests.push(this.grantRight(grantee_data, {'_content': this.domainAdminRights, canDelegate: 1}));
requests.push(this.grantRight(grantee_data, {'_content': 'set.dl.zimbraACE', canDelegate: 1}));
requests.push(this.grantRight(grantee_data, {'_content': 'set.domain.amavisWhitelistSender', canDelegate: 1}));
requests.push(this.grantRight(grantee_data, {'_content': 'set.domain.amavisBlacklistSender', canDelegate: 1}));
return requests;
}
addDelegatedAttributeToAccount(account_id, callback) {
this.api.modifyAccount(account_id, { zimbraIsDelegatedAdminAccount: 'TRUE' }, (err, data) => {
if (err) return callback(err);
return callback(null, data);
});
}
<<<<<<< HEAD
buildCosesGrantsRequest(coses, grantee_data) {
=======
// This functions add the rights to the domain admin
// to be able to change the accounts cos
assignCosRights(grantee_data, coses, callback, revoke = false) {
const request_data = {};
request_data.requests = this.buildCosesGrantsRequest(coses, grantee_data, revoke);
request_data.batch = true;
request_data.callback = callback;
return this.api.performRequest(request_data);
}
buildCosesGrantsRequest(coses = [], grantee_data, revoke = false) {
>>>>>>> manager
const requests = [];
const right = {'_content': 'assignCos'};
coses.forEach((c) => {
const target_data = { type: 'cos', identifier: c };
const grants = this.buildCosGrantByAcl(target_data, grantee_data);
requests.push(grants);
let grant = null;
if (revoke) {
grant = this.api.revokeRight(target_data, grantee_data, 'assignCos');
} else {
grant = this.api.grantRight(target_data, grantee_data, right);
}
requests.push(grant);
});
return [].concat.apply([], requests);
return requests;
}
// Return an array with all the rights
// needed 'assignCos', 'listCos', 'getCos'
buildCosGrantByAcl(target_data, grantee_data) {
const grants = [];
['assignCos', 'listCos', 'getCos'].forEach((right) => {
const request = this.api.grantRight(target_data, grantee_data, right);
grants.push(request);
});
return grants;
checkAliasDomain() {
this.isAliasDomain = this.attrs.zimbraDomainType === 'alias' ? true : false;
const masterDomain = this.attrs.zimbraMailCatchAllForwardingAddress;
if (this.isAliasDomain && masterDomain) {
this.masterDomainName = masterDomain.split(/@/)[1];
}
return true;
}
// TODO: Fix this fucking ugly code
getAdmins(callback) {
const that = this;
const admins_ids = this.getAdminsIdsFromGrants();
const admins_ids = this.getAdminsIdsFromGrants(this.attrs.zimbraACE);
const query = this.makeAdminIdsQuery(admins_ids);
return this.api.getAllAccounts(callback, {query: query});
}
// Return the ZimbraId if the grantee have the domainAdminRights right
// Grant.right_name() == domainAdminRights
getAdminsIdsFromGrants() {
getAdminsIdsFromGrants(zimbraACES) {
const ids = [];
this.parseACL(this.attrs.zimbraACE).forEach((grantee) => {
if (grantee.right === this.domainAdminRights) ids.push(grantee.id);
const regex = new RegExp(`${this.domainAdminRights}$`);
this.parseACL(zimbraACES).forEach((grantee) => {
if (regex.test(this.domainAdminRights)) ids.push(grantee.id);
});
return ids;
}
......@@ -114,12 +158,15 @@ class Domain extends Zimbra {
return results;
}
removeAdmin(account_id, callback) {
removeAdmin(account_id, coses, callback) {
const grantee_data = {
'type': 'usr',
'identifier': account_id
};
this.revokeRight(grantee_data, this.domainAdminRights, callback);
this.revokeRight(grantee_data, this.domainAdminRights, (err, data) => {
if (err) return callback(err);
this.assignCosRights(grantee_data, coses, callback, true);
});
}
}
......
......@@ -58,14 +58,12 @@ class Zimbra {
});
}
grantRight(grantee_data, right_name, callback, forBatch){
forBatch = forBatch || false;
return this.api.grantRight(this.buildRighTargetData(), grantee_data, right_name, callback, forBatch);
grantRight(grantee_data, right, callback = null){
return this.api.grantRight(this.buildRighTargetData(), grantee_data, right, callback);
}
revokeRight(grantee_data, right_name, callback, forBatch){
forBatch = forBatch || false;
return this.api.revokeRight(this.buildRighTargetData(), grantee_data, right_name, callback, forBatch);
revokeRight(grantee_data, right_name, callback = null){
return this.api.revokeRight(this.buildRighTargetData(), grantee_data, right_name, callback);
}
}
......
......@@ -6,6 +6,8 @@
'url': 'http://zimbra.zboxapp.dev:9000/service/admin/soap',
'user': 'admin@zboxapp.dev',
'password':'12345678'
// 'user': 'superadmin2@zboxtest.com',
// 'password':'zboxapp2016'
};
describe('Basic tests', function() {
......@@ -22,6 +24,16 @@
});
});
it('Should Flush the Cache', function(done){
let api = new ZimbraAdminApi(auth_data);
const flush_data = {type: 'domain', allServers: 1, entry: 'zboxapp.dev'};
api.flushCache(flush_data, function(err, data){
if (err) console.log(err);
expect(err).to.not.exist;
done();
})
});
it('should get all domains', function(done) {
let api = new ZimbraAdminApi(auth_data);
api.getAllDomains(function(err, data){
......@@ -172,6 +184,7 @@
const getAllDomains = api.directorySearch({types: 'domains'});
api.login(function(err, data){
api.makeBatchRequest([deleteAccount, getAllDomains, getAllAccounts], function(err, data){
console.log(data);
expect(data.errors.length).to.be.above(1);
expect(data.errors[0].constructor.name).to.equal('Error');
expect(data.errors[0].extra.code).to.exist;
......@@ -184,7 +197,7 @@
});
describe('Account tests', function() {
this.timeout(5000);
this.timeout(10000);
it('should create and return an account', function(done){
let account_name = Date.now() + '@big.com';
......@@ -284,7 +297,7 @@
it('Should Get The Account Mailbox', function(done){
let api = new ZimbraAdminApi(auth_data);
api.getAccount('cos_basic_14@customer.dev', function(err, data){
api.getAccount('cos_basic_13@customer.dev', function(err, data){
let account = data;
account.getMailbox(function(err, data){
if (err) return console.log(err);
......@@ -296,7 +309,7 @@
it('Should Get The Account Mailbox Size', function(done){
let api = new ZimbraAdminApi(auth_data);
api.getAccount('cos_basic_14@customer.dev', function(err, data){
api.getAccount('cos_basic_13@customer.dev', function(err, data){
let account = data;
account.getMailboxSize(function(err, data){
if (err) return console.log(err);
......@@ -377,12 +390,65 @@
});
});
// it('Should enable the account Archiving', function(done){
// let account_name = Date.now() + '@zboxtest.com';
// let account_password = Date.now();
// let api = new ZimbraAdminApi(auth_data);
// api.createAccount(account_name, account_password, {}, function(err, account){
// if (err) return console.log(err);
// account.enableArchiving('default', function(err, account){
// if (err) return console.log(err);
// expect(account.archiveEnabled).to.be.true;
// expect(account.attrs.zimbraArchiveAccount).to.match(/com\.archive$/);
// done();
// });
// });
// });
//
// it('Should disable the account Archiving', function(done){
// let account_name = Date.now() + '@zboxtest.com';
// let account_password = Date.now();
// let api = new ZimbraAdminApi(auth_data);
// api.createAccount(account_name, account_password, {}, function(err, account){
// if (err) return console.log(err);
// account.enableArchiving('default', function(err, account){
// if (err) return console.log(err);
// account.disableArchiving(function(err, account){
// if (err) return console.log(err);
// expect(account.archiveEnabled).to.be.false;
// expect(account.attrs.zimbraArchiveAccount).to.match(/com\.archive$/);
// done();
// });
// });
// });
// });
});
describe('Domain tests', function() {
this.timeout(5000);
it('Should return if the domain is an alias Domain', function(done){
let api = new ZimbraAdminApi(auth_data);
api.getDomain('reseller.alias', function(err, data){
if (err) return console.log(err);
expect(data.isAliasDomain).to.be.true;
expect(data.masterDomainName).to.be.equal('reseller.dev');
done();
});
});
it('Should return false if the domain is not an alias Domain', function(done){
let api = new ZimbraAdminApi(auth_data);
api.getDomain('reseller.dev', function(err, data){
if (err) return console.log(err);
expect(data.isAliasDomain).to.be.false;
expect(data.masterDomainName).to.be.undefined;
done();
});
});
it('should create and return Domain', function(done){
let resource_name = Date.now() + '.dev';
let resource_attributes = {};
......@@ -522,9 +588,9 @@
it('addAdmin should add Admin', function(done){
let api = new ZimbraAdminApi(auth_data);
let domain_admin = 'domain_admin@customer.dev';
let domain_admin = Date.now() + '@customer.dev';
let resource_name = Date.now() + '.dev';
api.getAccount(domain_admin, function(err, account){
api.createAccount(domain_admin, '12dda.222', {}, function(err, account){
api.createDomain(resource_name, {}, function(err, data){
if (err) console.error(err);
let domain = data;
......@@ -532,10 +598,17 @@
domain.addAdmin(account.id, coses, function(e, d){
if (e) return console.error(e);
expect(err).to.be.null;
domain.getACLs(function(e, d){
d.getACLs(function(e, acls){
if (e) return console.error(e);
expect(d[0].grantee.name).to.be.equal(account.name);
const expectedGrants = ["domainAdminRights", "set.dl.zimbraACE", "set.domain.amavisBlacklistSender", "set.domain.amavisWhitelistSender"];
const actualGrants = acls.map(function(acl){return acl.rightName}).sort()
expect(expectedGrants[0]).to.be.equal(actualGrants[0]);
expect(expectedGrants[2]).to.be.equal(actualGrants[2]);
d.getAdmins(function(e, admins){
if (e) return console.error(e);
expect(admins.account[0].name).to.be.equal(domain_admin);
done();
})
});
});
});
......@@ -548,7 +621,7 @@
let resource_name = Date.now() + '.dev';
api.createDomain(resource_name, {}, function(err, domain){
domain.addAdmin(domain_admin, [], function(e, d){
domain.removeAdmin(domain_admin, function(e, d){
domain.removeAdmin(domain_admin, [], function(e, d){
domain.getACLs(function(e, d){
if (e) return console.error(e);
expect(d.length).to.be.equal(0);
......@@ -661,7 +734,7 @@
let owner_email = 'domain_admin@customer.dev';
let resource_name = Date.now() + '@customer.dev';
api.createDistributionList(resource_name, {}, function(err, dl){
dl.addOwner(owner_email, function(e, d){
dl.addOwner(owner_email, function(e, dl){
if (e) return console.error(e);
expect(err).to.be.null;
dl.getACLs(function(e, d){
......@@ -679,8 +752,8 @@
let resource_name = Date.now() + '@customer.dev';
api.createDistributionList(resource_name, {}, function(err, dl){
dl.addOwner(owner_email, function(e, d){
dl.removeOwner(owner_email, function(e, d){
dl.getACLs(function(e, d){
d.removeOwner(owner_email, function(e, d){
d.getACLs(function(e, d){
if (e) return console.error(e);
expect(d.length).to.be.equal(0);
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