Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Z
zimbra-admin-api-js
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Public
zimbra-admin-api-js
Commits
9835cecb
Commit
9835cecb
authored
Apr 28, 2016
by
Patricio Bruna
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Every function now works with BatchRequest
parent
d7e9b763
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
180 additions
and
241 deletions
+180
-241
README.md
README.md
+13
-0
zimbra-admin-api.js
lib/zimbra-admin-api.js
+79
-93
zimbra-admin-api.js.map
lib/zimbra-admin-api.js.map
+1
-1
index.js
src/index.js
+57
-62
domain.js
src/zimbra/domain.js
+21
-25
zimbra.js
src/zimbra/zimbra.js
+6
-4
test.js
test/js/spec/test.js
+3
-56
No files found.
README.md
View file @
9835cecb
...
...
@@ -163,6 +163,19 @@ zimbraApi.getAllAccounts(callback, query_object);
```
## 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
Pass an array of domains ids or names and you get back an array of
`countAccounts`
responses
...
...
lib/zimbra-admin-api.js
View file @
9835cecb
...
...
@@ -98,21 +98,21 @@ return /******/ (function(modules) { // webpackBootstrap
function Error(err) {
(0, _classCallCheck3.default)(this, Error);
this.code = err.Fault
.Code.Value
;
this.extra = this.getErrorInfo(err
.Fault
);
this.code = err.Fault
? err.Fault.Code.Value : err.status
;
this.extra = this.getErrorInfo(err);
}
(0, _createClass3.default)(Error, [{
key: 'getErrorInfo',
value: function getErrorInfo(
fault
) {
if (
fault && fault.Detail
) {
value: function getErrorInfo(
err
) {
if (
err && err.Fault
) {
return {
'code':
f
ault.Detail.Error.Code,
'reason':
f
ault.Reason.Text,
'trace':
f
ault.Detail.Error.Trace
'code':
err.F
ault.Detail.Error.Code,
'reason':
err.F
ault.Reason.Text,
'trace':
err.F
ault.Detail.Error.Trace
};
} else {
return {};
return {
'code': err.status, 'reason': err.statusText
};
}
}
}]);
...
...
@@ -241,17 +241,17 @@ return /******/ (function(modules) { // webpackBootstrap
value: function performRequest(request_data) {
var _this = this;
var batch = arguments.length <= 1 || arguments[1] === undefined ? false : arguments[1];
// return request_data for BatchRequest if no callback present
if (typeof request_data.callback !== 'function') return request_data;
if (this.client.token) {
if (batch) return this.makeBatchRequest(request_data.requests, request_data.callback);
if (
request_data.
batch) return this.makeBatchRequest(request_data.requests, request_data.callback);
this.makeRequest(request_data);
} else {
(function () {
var that = _this;
var getCallback = function getCallback(err, response) {
if (err) return this.handleError(err);
if (batch) return that.makeBatchRequest(request_data.requests, request_data.callback);
if (
request_data.
batch) return that.makeBatchRequest(request_data.requests, request_data.callback);
that.makeRequest(request_data);
};
_this.login(getCallback);
...
...
@@ -262,7 +262,7 @@ return /******/ (function(modules) { // webpackBootstrap
key: 'parseBatchResponse',
value: function parseBatchResponse(data, callback) {
var response_object = data.options.response.BatchResponse;
callback(null, response_object);
return
callback(null, response_object);
}
}, {
key: 'parseCountAccountResponse',
...
...
@@ -276,7 +276,7 @@ return /******/ (function(modules) { // webpackBootstrap
value: function parseGrantsResponse(data, request_data, callback) {
var result = {};
var response_object = data.get().GetGrantsResponse;
callback(null, response_object);
return
callback(null, response_object);
}
}, {
key: 'parseResponse',
...
...
@@ -337,7 +337,7 @@ return /******/ (function(modules) { // webpackBootstrap
request_data.resource = resource;
request_data.parse_response = this.parseResponse;
request_data.params.params = resource_data;
this.performRequest(request_data);
return
this.performRequest(request_data);
}
}, {
key: 'remove',
...
...
@@ -346,19 +346,16 @@ return /******/ (function(modules) { // webpackBootstrap
request_data.resource = resource;
request_data.parse_response = this.parseEmptyResponse;
request_data.params.params = resource_data;
this.performRequest(request_data);
return
this.performRequest(request_data);
}
}, {
key: 'modify',
value: function modify(resource, resource_data, callback) {
var forBatch = arguments.length <= 3 || arguments[3] === undefined ? false : arguments[3];
var request_data = this.buildRequestData('Modify' + resource, callback);
request_data.resource = resource;
request_data.parse_response = this.parseResponse;
request_data.params.params = resource_data;
if (forBatch) return request_data;
this.performRequest(request_data);
return this.performRequest(request_data);
}
}, {
key: 'get',
...
...
@@ -371,7 +368,7 @@ return /******/ (function(modules) { // webpackBootstrap
'by': this.dictionary.byIdOrName(resource_identifier),
'_content': resource_identifier
};
this.performRequest(request_data);
return
this.performRequest(request_data);
}
}, {
key: 'getAll',
...
...
@@ -379,7 +376,7 @@ return /******/ (function(modules) { // webpackBootstrap
var request_data = this.buildRequestData('GetAll' + resource + 's', callback);
request_data.resource = resource;
request_data.parse_response = this.parseAllResponse;
this.performRequest(request_data);
return
this.performRequest(request_data);
}
// Grant a right on a target to an individual or group grantee.
...
...
@@ -392,8 +389,6 @@ return /******/ (function(modules) { // webpackBootstrap
}, {
key: 'grantRight',
value: function grantRight(target_data, grantee_data, right_name, callback) {
var forBatch = arguments.length <= 4 || arguments[4] === undefined ? false : arguments[4];
var request_data = this.buildRequestData('GrantRight', callback);
var _dictionary$buildTarg = this.dictionary.buildTargetGrantee(target_data, grantee_data);
...
...
@@ -407,8 +402,7 @@ return /******/ (function(modules) { // webpackBootstrap
request_data.params.params.grantee = grantee;
request_data.params.params.target = target;
request_data.params.params.right = { '_content': right_name };
if (forBatch) return request_data;
this.performRequest(request_data);
return this.performRequest(request_data);
}
// Specific functions
...
...
@@ -419,7 +413,7 @@ return /******/ (function(modules) { // webpackBootstrap
var request_data = this.buildRequestData('AddAccountAlias', callback);
request_data.parse_response = this.parseEmptyResponse;
request_data.params.params = { 'id': account_id, 'alias': alias };
this.performRequest(request_data);
return
this.performRequest(request_data);
}
// Add New members tos distributionlists
...
...
@@ -431,12 +425,12 @@ return /******/ (function(modules) { // webpackBootstrap
var request_data = this.buildRequestData('AddDistributionListMember', callback);
request_data.parse_response = this.parseEmptyResponse;
request_data.params.params = { id: dl_id, dlm: this.dictionary.convertToZimbraArray(members) };
this.performRequest(request_data);
return
this.performRequest(request_data);
}
}, {
key: 'getAccount',
value: function getAccount(identifier, callback) {
this.get('Account', identifier, callback);
return
this.get('Account', identifier, callback);
}
// attributes debe ser un arreglo de objetos:
...
...
@@ -450,34 +444,34 @@ return /******/ (function(modules) { // webpackBootstrap
value: function createAccount(name, password, attributes, callback) {
var resource_data = this.buildResourceData(name, attributes);
resource_data.password = { '_content': password };
this.create('Account', resource_data, callback);
return
this.create('Account', resource_data, callback);
}
}, {
key: 'getCos',
value: function getCos(identifier, callback) {
this.get('Cos', identifier, callback);
return
this.get('Cos', identifier, callback);
}
}, {
key: 'getDomain',
value: function getDomain(identifier, callback) {
this.get('Domain', identifier, callback);
return
this.get('Domain', identifier, callback);
}
}, {
key: 'createDomain',
value: function createDomain(name, attributes, callback) {
var resource_data = this.buildResourceData(name, attributes);
this.create('Domain', resource_data, callback);
return
this.create('Domain', resource_data, callback);
}
}, {
key: 'getDistributionList',
value: function getDistributionList(identifier, callback) {
this.get('DistributionList', identifier, callback);
return
this.get('DistributionList', identifier, callback);
}
}, {
key: 'createDistributionList',
value: function createDistributionList(name, attributes, callback) {
var resource_data = this.buildResourceData(name, attributes);
this.create('DistributionList', resource_data, callback);
return
this.create('DistributionList', resource_data, callback);
}
}, {
key: 'getAllDomains',
...
...
@@ -485,7 +479,7 @@ return /******/ (function(modules) { // webpackBootstrap
var query_object = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
query_object.types = 'domains';
this.directorySearch(query_object, callback);
return
this.directorySearch(query_object, callback);
}
}, {
key: 'getAllAccounts',
...
...
@@ -493,7 +487,7 @@ return /******/ (function(modules) { // webpackBootstrap
var query_object = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
query_object.types = 'accounts';
this.directorySearch(query_object, callback);
return
this.directorySearch(query_object, callback);
}
}, {
key: 'getAllDistributionLists',
...
...
@@ -501,7 +495,7 @@ return /******/ (function(modules) { // webpackBootstrap
var query_object = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
query_object.types = 'distributionlists';
this.directorySearch(query_object, callback);
return
this.directorySearch(query_object, callback);
}
}, {
key: 'getAllAliases',
...
...
@@ -509,7 +503,7 @@ return /******/ (function(modules) { // webpackBootstrap
var query_object = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
query_object.types = 'aliases';
this.directorySearch(query_object, callback);
return
this.directorySearch(query_object, callback);
}
}, {
key: 'getAllCos',
...
...
@@ -517,7 +511,7 @@ return /******/ (function(modules) { // webpackBootstrap
var request_data = this.buildRequestData('GetAllCos', callback);
request_data.resource = 'Cos';
request_data.parse_response = this.parseAllResponse;
this.performRequest(request_data);
return
this.performRequest(request_data);
}
// Returns all grants on the specified target entry, or all grants granted to the specified grantee entry.
...
...
@@ -530,8 +524,6 @@ return /******/ (function(modules) { // webpackBootstrap
}, {
key: 'getGrants',
value: function getGrants(target_data, grantee_data, callback) {
var forBatch = arguments.length <= 3 || arguments[3] === undefined ? false : arguments[3];
var _dictionary$buildTarg3 = this.dictionary.buildTargetGrantee(target_data, grantee_data);
var _dictionary$buildTarg4 = (0, _slicedToArray3.default)(_dictionary$buildTarg3, 2);
...
...
@@ -544,8 +536,7 @@ return /******/ (function(modules) { // webpackBootstrap
request_data.parse_response = this.parseAllResponse;
request_data.params.params.grantee = grantee;
request_data.params.params.target = target;
if (forBatch) return request_data;
this.performRequest(request_data);
return this.performRequest(request_data);
}
// Get current logged account information
...
...
@@ -559,7 +550,7 @@ return /******/ (function(modules) { // webpackBootstrap
request_data.parse_response = function (data, _, callback) {
return callback(null, data.response[0].GetInfoResponse);
};
this.performRequest(request_data);
return
this.performRequest(request_data);
}
// Modify Account
...
...
@@ -567,13 +558,11 @@ return /******/ (function(modules) { // webpackBootstrap
}, {
key: 'modifyAccount',
value: function modifyAccount(zimbra_id, attributes, callback) {
var forBatch = arguments.length <= 3 || arguments[3] === undefined ? false : arguments[3];
var resource_data = {
id: zimbra_id,
a: this.dictionary.attributesToArray(attributes)
};
return this.modify('Account', resource_data, callback
, forBatch
);
return this.modify('Account', resource_data, callback);
}
// Modify Domain
...
...
@@ -606,7 +595,7 @@ return /******/ (function(modules) { // webpackBootstrap
key: 'removeAccount',
value: function removeAccount(zimbra_id, callback) {
var resource_data = { id: zimbra_id };
this.remove('Account', resource_data, callback);
return
this.remove('Account', resource_data, callback);
}
// Remove Account Alias
...
...
@@ -617,7 +606,7 @@ return /******/ (function(modules) { // webpackBootstrap
var request_data = this.buildRequestData('RemoveAccountAlias', callback);
request_data.parse_response = this.parseEmptyResponse;
request_data.params.params = { 'id': account_id, 'alias': alias };
this.performRequest(request_data);
return
this.performRequest(request_data);
}
// Remove Account
...
...
@@ -626,7 +615,7 @@ return /******/ (function(modules) { // webpackBootstrap
key: 'removeDomain',
value: function removeDomain(zimbra_id, callback) {
var resource_data = { id: zimbra_id };
this.remove('Domain', resource_data, callback);
return
this.remove('Domain', resource_data, callback);
}
// Remove DL
...
...
@@ -647,7 +636,7 @@ return /******/ (function(modules) { // webpackBootstrap
var request_data = this.buildRequestData('RemoveDistributionListMember', callback);
request_data.parse_response = this.parseEmptyResponse;
request_data.params.params = { id: dl_id, dlm: this.dictionary.convertToZimbraArray(members) };
this.performRequest(request_data);
return
this.performRequest(request_data);
}
}, {
key: 'revokeRight',
...
...
@@ -664,7 +653,7 @@ return /******/ (function(modules) { // webpackBootstrap
request_data.params.params.grantee = grantee;
request_data.params.params.target = target;
request_data.params.params.right = { '_content': right_name };
this.performRequest(request_data);
return
this.performRequest(request_data);
}
// Search the Directory
...
...
@@ -689,7 +678,7 @@ return /******/ (function(modules) { // webpackBootstrap
var request_data = this.buildRequestData('SearchDirectory', callback);
request_data.params.params = search_object;
request_data.parse_response = this.parseSearchResponse;
this.performRequest(request_data);
return
this.performRequest(request_data);
}
// TODO: Fucking ugly code to make it better
...
...
@@ -705,7 +694,7 @@ return /******/ (function(modules) { // webpackBootstrap
'by': this.dictionary.byIdOrName(domain_idenfitier),
'_content': domain_idenfitier
};
this.performRequest(request_data);
return
this.performRequest(request_data);
}
// TODO: Fix this ugly FCKing Code
...
...
@@ -728,15 +717,11 @@ return /******/ (function(modules) { // webpackBootstrap
};
request_data.requests = [];
domains_ids.forEach(function (domain_id) {
var request = _this2.buildRequestData('CountAccount', callback);
request.parse_response = _this2.parseCountAccountResponse;
request.params.params.domain = {
'by': _this2.dictionary.byIdOrName(domain_id),
'_content': domain_id
};
var request = _this2.countAccounts(domain_id);
request_data.requests.push(request);
});
this.performRequest(request_data, true);
request_data.batch = true;
return this.performRequest(request_data);
}
// TODO: TO ugly
...
...
@@ -760,7 +745,7 @@ return /******/ (function(modules) { // webpackBootstrap
return callback(null, {});
}
};
this.performRequest(request_data);
return
this.performRequest(request_data);
}
// TODO: Ugly
...
...
@@ -779,7 +764,7 @@ return /******/ (function(modules) { // webpackBootstrap
};
return callback(null, result);
};
this.performRequest(request_data);
return
this.performRequest(request_data);
}
}, {
key: 'token',
...
...
@@ -13479,14 +13464,15 @@ return /******/ (function(modules) { // webpackBootstrap
value: function addAdmin(account_id, callback) {
var request_data = {};
var grantee_data = { 'type': 'usr', 'identifier': account_id };
var modifyAccountRequest = this.api.modifyAccount(account_id, { zimbraIsDelegatedAdminAccount: 'TRUE' }
, callback, true
);
var grantRightRequest = this.grantRight(grantee_data, this.domainAdminRights
, callback, true
);
var modifyAccountRequest = this.api.modifyAccount(account_id, { zimbraIsDelegatedAdminAccount: 'TRUE' });
var grantRightRequest = this.grantRight(grantee_data, this.domainAdminRights);
request_data.requests = [modifyAccountRequest, grantRightRequest];
request_data.batch = true;
request_data.callback = function (err, data) {
if (err) return callback(err);
callback(null, data.GrantRightResponse);
};
this.api.performRequest(request_data
, true
);
this.api.performRequest(request_data);
}
// TODO: Fix this fucking ugly code
...
...
@@ -13495,21 +13481,9 @@ return /******/ (function(modules) { // webpackBootstrap
key: 'getAdmins',
value: function getAdmins(callback) {
var that = this;
this.getAdminsIdsFromGrants(function (e, d) {
if (e) return callback(e);
if (d.length < 1) return callback(null, []);
var query = "(|";
d.forEach(function (id) {
var 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 });
});
var admins_ids = this.getAdminsIdsFromGrants();
var query = this.makeAdminIdsQuery(admins_ids);
return this.api.getAllAccounts(callback, { query: query });
}
// Return the ZimbraId if the grantee have the domainAdminRights right
...
...
@@ -13517,15 +13491,14 @@ return /******/ (function(modules) { // webpackBootstrap
}, {
key: 'getAdminsIdsFromGrants',
value: function getAdminsIdsFromGrants(callback) {
value: function getAdminsIdsFromGrants() {
var _this2 = this;
var ids = [];
this.getACLs(function (err, data) {
if (err) return callback(err);
data.forEach(function (grant) {
if (grant.isDomainAdminGrant()) ids.push(grant.granteeId);
});
return callback(null, ids);
this.parseACL(this.attrs.zimbraACE).forEach(function (grantee) {
if (grantee.right === _this2.domainAdminRights) ids.push(grantee.id);
});
return ids;
}
}, {
key: 'getAllDistributionLists',
...
...
@@ -13549,6 +13522,17 @@ return /******/ (function(modules) { // webpackBootstrap
return callback(null, d);
});
}
}, {
key: 'makeAdminIdsQuery',
value: function makeAdminIdsQuery(ids) {
var query = "(|";
ids.forEach(function (id) {
var zimbra_id = '(zimbraId=' + id + ')';
query += zimbra_id;
});
query += ")";
return query;
}
}, {
key: 'maxAccountsByCos',
value: function maxAccountsByCos() {
...
...
@@ -13864,10 +13848,12 @@ return /******/ (function(modules) { // webpackBootstrap
key: "parseACL",
value: function parseACL(acls) {
var elements = [].concat.apply([], [acls]);
var grantees = {};
elements.forEach(function (el) {
grantee_data = el.split(/ /);
grantees[grantee_data[0]] = { type: grantee_data[1], right: grantee_data[2] };
var grantees = [];
// Filter to remove undefined
// http://stackoverflow.com/questions/28607451/removing-undefined-values-from-array
elements.filter(Boolean).forEach(function (el) {
var grantee_data = el.split(/ /);
grantees.push({ id: grantee_data[0], type: grantee_data[1], right: grantee_data[2] });
});
return grantees;
}
lib/zimbra-admin-api.js.map
View file @
9835cecb
This source diff could not be displayed because it is too large. You can
view the blob
instead.
src/index.js
View file @
9835cecb
...
...
@@ -6,19 +6,19 @@ import Dictionary from './utils/dictionary.js';
class
Error
{
constructor
(
err
)
{
this
.
code
=
err
.
Fault
.
Code
.
Value
;
this
.
extra
=
this
.
getErrorInfo
(
err
.
Fault
);
this
.
code
=
err
.
Fault
?
err
.
Fault
.
Code
.
Value
:
err
.
status
;
this
.
extra
=
this
.
getErrorInfo
(
err
);
}
getErrorInfo
(
fault
)
{
if
(
fault
&&
fault
.
Detail
)
{
getErrorInfo
(
err
)
{
if
(
err
&&
err
.
Fault
)
{
return
{
'code'
:
f
ault
.
Detail
.
Error
.
Code
,
'reason'
:
f
ault
.
Reason
.
Text
,
'trace'
:
f
ault
.
Detail
.
Error
.
Trace
}
'code'
:
err
.
F
ault
.
Detail
.
Error
.
Code
,
'reason'
:
err
.
F
ault
.
Reason
.
Text
,
'trace'
:
err
.
F
ault
.
Detail
.
Error
.
Trace
}
;
}
else
{
return
{};
return
{
'code'
:
err
.
status
,
'reason'
:
err
.
statusText
};
}
}
...
...
@@ -105,8 +105,8 @@ export default class ZimbraAdminApi {
let
request
=
null
;
this
.
client
.
getRequest
(
options
,
(
err
,
req
)
=>
{
if
(
err
)
return
error
(
err
);
request
=
req
;
});
request
=
req
;
});
return
request
;
}
...
...
@@ -139,15 +139,17 @@ export default class ZimbraAdminApi {
});
}
performRequest
(
request_data
,
batch
=
false
)
{
performRequest
(
request_data
)
{
// return request_data for BatchRequest if no callback present
if
(
typeof
request_data
.
callback
!==
'function'
)
return
request_data
;
if
(
this
.
client
.
token
)
{
if
(
batch
)
return
this
.
makeBatchRequest
(
request_data
.
requests
,
request_data
.
callback
);
if
(
request_data
.
batch
)
return
this
.
makeBatchRequest
(
request_data
.
requests
,
request_data
.
callback
);
this
.
makeRequest
(
request_data
);
}
else
{
const
that
=
this
;
let
getCallback
=
function
(
err
,
response
){
if
(
err
)
return
this
.
handleError
(
err
);
if
(
batch
)
return
that
.
makeBatchRequest
(
request_data
.
requests
,
request_data
.
callback
);
if
(
request_data
.
batch
)
return
that
.
makeBatchRequest
(
request_data
.
requests
,
request_data
.
callback
);
that
.
makeRequest
(
request_data
);
};
this
.
login
(
getCallback
);
...
...
@@ -156,7 +158,7 @@ export default class ZimbraAdminApi {
parseBatchResponse
(
data
,
callback
)
{
const
response_object
=
data
.
options
.
response
.
BatchResponse
;
callback
(
null
,
response_object
);
return
callback
(
null
,
response_object
);
}
parseCountAccountResponse
(
data
,
request_data
,
callback
)
{
...
...
@@ -168,7 +170,7 @@ export default class ZimbraAdminApi {
parseGrantsResponse
(
data
,
request_data
,
callback
)
{
const
result
=
{};
const
response_object
=
data
.
get
().
GetGrantsResponse
;
callback
(
null
,
response_object
);
return
callback
(
null
,
response_object
);
}
parseResponse
(
data
,
request_data
,
callback
)
{
...
...
@@ -222,7 +224,7 @@ export default class ZimbraAdminApi {
request_data
.
resource
=
resource
;
request_data
.
parse_response
=
this
.
parseResponse
;
request_data
.
params
.
params
=
resource_data
;
this
.
performRequest
(
request_data
);
return
this
.
performRequest
(
request_data
);
}
remove
(
resource
,
resource_data
,
callback
){
...
...
@@ -230,17 +232,16 @@ export default class ZimbraAdminApi {
request_data
.
resource
=
resource
;
request_data
.
parse_response
=
this
.
parseEmptyResponse
;
request_data
.
params
.
params
=
resource_data
;
this
.
performRequest
(
request_data
);
return
this
.
performRequest
(
request_data
);
}
modify
(
resource
,
resource_data
,
callback
,
forBatch
=
false
){
modify
(
resource
,
resource_data
,
callback
){
const
request_data
=
this
.
buildRequestData
(
`Modify
${
resource
}
`
,
callback
);
request_data
.
resource
=
resource
;
request_data
.
parse_response
=
this
.
parseResponse
;
request_data
.
params
.
params
=
resource_data
;
if
(
forBatch
)
return
request_data
;
this
.
performRequest
(
request_data
);
return
this
.
performRequest
(
request_data
);
}
get
(
resource
,
resource_identifier
,
callback
){
...
...
@@ -252,14 +253,14 @@ export default class ZimbraAdminApi {
'by'
:
this
.
dictionary
.
byIdOrName
(
resource_identifier
),
'_content'
:
resource_identifier
};
this
.
performRequest
(
request_data
);
return
this
.
performRequest
(
request_data
);
}
getAll
(
resource
,
callback
)
{
const
request_data
=
this
.
buildRequestData
(
`GetAll
${
resource
}
s`
,
callback
);
request_data
.
resource
=
resource
;
request_data
.
parse_response
=
this
.
parseAllResponse
;
this
.
performRequest
(
request_data
);
return
this
.
performRequest
(
request_data
);
}
// Grant a right on a target to an individual or group grantee.
...
...
@@ -268,15 +269,14 @@ export default class ZimbraAdminApi {
// type: (account|cos|dl|domain),
// identifier: (name or zimbraId)
// }
grantRight
(
target_data
,
grantee_data
,
right_name
,
callback
,
forBatch
=
false
)
{
grantRight
(
target_data
,
grantee_data
,
right_name
,
callback
)
{
const
request_data
=
this
.
buildRequestData
(
'GrantRight'
,
callback
);
const
[
target
,
grantee
]
=
this
.
dictionary
.
buildTargetGrantee
(
target_data
,
grantee_data
);
request_data
.
parse_response
=
this
.
parseEmptyResponse
;
request_data
.
params
.
params
.
grantee
=
grantee
;
request_data
.
params
.
params
.
target
=
target
;
request_data
.
params
.
params
.
right
=
{
'_content'
:
right_name
};
if
(
forBatch
)
return
request_data
;
this
.
performRequest
(
request_data
);
return
this
.
performRequest
(
request_data
);
}
// Specific functions
...
...
@@ -285,7 +285,7 @@ export default class ZimbraAdminApi {
const
request_data
=
this
.
buildRequestData
(
'AddAccountAlias'
,
callback
);
request_data
.
parse_response
=
this
.
parseEmptyResponse
;
request_data
.
params
.
params
=
{
'id'
:
account_id
,
'alias'
:
alias
};
this
.
performRequest
(
request_data
);
return
this
.
performRequest
(
request_data
);
}
// Add New members tos distributionlists
...
...
@@ -294,11 +294,11 @@ export default class ZimbraAdminApi {
const
request_data
=
this
.
buildRequestData
(
'AddDistributionListMember'
,
callback
);
request_data
.
parse_response
=
this
.
parseEmptyResponse
;
request_data
.
params
.
params
=
{
id
:
dl_id
,
dlm
:
this
.
dictionary
.
convertToZimbraArray
(
members
)
};
this
.
performRequest
(
request_data
);
return
this
.
performRequest
(
request_data
);
}
getAccount
(
identifier
,
callback
)
{
this
.
get
(
'Account'
,
identifier
,
callback
);
return
this
.
get
(
'Account'
,
identifier
,
callback
);
}
// attributes debe ser un arreglo de objetos:
...
...
@@ -309,57 +309,57 @@ export default class ZimbraAdminApi {
createAccount
(
name
,
password
,
attributes
,
callback
)
{
const
resource_data
=
this
.
buildResourceData
(
name
,
attributes
);
resource_data
.
password
=
{
'_content'
:
password
};
this
.
create
(
'Account'
,
resource_data
,
callback
);
return
this
.
create
(
'Account'
,
resource_data
,
callback
);
}
getCos
(
identifier
,
callback
)
{
this
.
get
(
'Cos'
,
identifier
,
callback
);
return
this
.
get
(
'Cos'
,
identifier
,
callback
);
}
getDomain
(
identifier
,
callback
)
{
this
.
get
(
'Domain'
,
identifier
,
callback
);
return
this
.
get
(
'Domain'
,
identifier
,
callback
);
}
createDomain
(
name
,
attributes
,
callback
)
{
const
resource_data
=
this
.
buildResourceData
(
name
,
attributes
);
this
.
create
(
'Domain'
,
resource_data
,
callback
);
return
this
.
create
(
'Domain'
,
resource_data
,
callback
);
}
getDistributionList
(
identifier
,
callback
)
{
this
.
get
(
'DistributionList'
,
identifier
,
callback
);
return
this
.
get
(
'DistributionList'
,
identifier
,
callback
);
}
createDistributionList
(
name
,
attributes
,
callback
)
{
const
resource_data
=
this
.
buildResourceData
(
name
,
attributes
);
this
.
create
(
'DistributionList'
,
resource_data
,
callback
);
return
this
.
create
(
'DistributionList'
,
resource_data
,
callback
);
}
getAllDomains
(
callback
,
query_object
=
{})
{
query_object
.
types
=
'domains'
;
this
.
directorySearch
(
query_object
,
callback
);
return
this
.
directorySearch
(
query_object
,
callback
);
}
getAllAccounts
(
callback
,
query_object
=
{})
{
query_object
.
types
=
'accounts'
;
this
.
directorySearch
(
query_object
,
callback
);
return
this
.
directorySearch
(
query_object
,
callback
);
}
getAllDistributionLists
(
callback
,
query_object
=
{})
{
query_object
.
types
=
'distributionlists'
;
this
.
directorySearch
(
query_object
,
callback
);
return
this
.
directorySearch
(
query_object
,
callback
);
}
getAllAliases
(
callback
,
query_object
=
{})
{
query_object
.
types
=
'aliases'
;
this
.
directorySearch
(
query_object
,
callback
);
return
this
.
directorySearch
(
query_object
,
callback
);
}
getAllCos
(
callback
)
{
const
request_data
=
this
.
buildRequestData
(
'GetAllCos'
,
callback
);
request_data
.
resource
=
'Cos'
;
request_data
.
parse_response
=
this
.
parseAllResponse
;
this
.
performRequest
(
request_data
);
return
this
.
performRequest
(
request_data
);
}
// Returns all grants on the specified target entry, or all grants granted to the specified grantee entry.
...
...
@@ -368,15 +368,14 @@ export default class ZimbraAdminApi {
// type: (account|cos|dl|domain),
// identifier: (name or zimbraId)
// }
getGrants
(
target_data
,
grantee_data
,
callback
,
forBatch
=
false
)
{
getGrants
(
target_data
,
grantee_data
,
callback
)
{
const
[
target
,
grantee
]
=
this
.
dictionary
.
buildTargetGrantee
(
target_data
,
grantee_data
);
const
request_data
=
this
.
buildRequestData
(
'GetGrants'
,
callback
);
request_data
.
resource
=
'Grant'
;
request_data
.
parse_response
=
this
.
parseAllResponse
;
request_data
.
params
.
params
.
grantee
=
grantee
;
request_data
.
params
.
params
.
target
=
target
;
if
(
forBatch
)
return
request_data
;
this
.
performRequest
(
request_data
);
return
this
.
performRequest
(
request_data
);
}
// Get current logged account information
...
...
@@ -387,16 +386,16 @@ export default class ZimbraAdminApi {
request_data
.
parse_response
=
function
(
data
,
_
,
callback
){
return
callback
(
null
,
data
.
response
[
0
].
GetInfoResponse
);
};
this
.
performRequest
(
request_data
);
return
this
.
performRequest
(
request_data
);
}
// Modify Account
modifyAccount
(
zimbra_id
,
attributes
,
callback
,
forBatch
=
false
)
{
modifyAccount
(
zimbra_id
,
attributes
,
callback
)
{
let
resource_data
=
{
id
:
zimbra_id
,
a
:
this
.
dictionary
.
attributesToArray
(
attributes
)
};
return
this
.
modify
(
'Account'
,
resource_data
,
callback
,
forBatch
);
return
this
.
modify
(
'Account'
,
resource_data
,
callback
);
}
// Modify Domain
...
...
@@ -420,7 +419,7 @@ export default class ZimbraAdminApi {
// Remove Account
removeAccount
(
zimbra_id
,
callback
)
{
let
resource_data
=
{
id
:
zimbra_id
};
this
.
remove
(
'Account'
,
resource_data
,
callback
);
return
this
.
remove
(
'Account'
,
resource_data
,
callback
);
}
// Remove Account Alias
...
...
@@ -428,13 +427,13 @@ export default class ZimbraAdminApi {
const
request_data
=
this
.
buildRequestData
(
'RemoveAccountAlias'
,
callback
);
request_data
.
parse_response
=
this
.
parseEmptyResponse
;
request_data
.
params
.
params
=
{
'id'
:
account_id
,
'alias'
:
alias
};
this
.
performRequest
(
request_data
);
return
this
.
performRequest
(
request_data
);
}
// Remove Account
removeDomain
(
zimbra_id
,
callback
)
{
let
resource_data
=
{
id
:
zimbra_id
};
this
.
remove
(
'Domain'
,
resource_data
,
callback
);
return
this
.
remove
(
'Domain'
,
resource_data
,
callback
);
}
// Remove DL
...
...
@@ -449,7 +448,7 @@ export default class ZimbraAdminApi {
const
request_data
=
this
.
buildRequestData
(
'RemoveDistributionListMember'
,
callback
);
request_data
.
parse_response
=
this
.
parseEmptyResponse
;
request_data
.
params
.
params
=
{
id
:
dl_id
,
dlm
:
this
.
dictionary
.
convertToZimbraArray
(
members
)
};
this
.
performRequest
(
request_data
);
return
this
.
performRequest
(
request_data
);
}
revokeRight
(
target_data
,
grantee_data
,
right_name
,
callback
)
{
...
...
@@ -459,7 +458,7 @@ export default class ZimbraAdminApi {
request_data
.
params
.
params
.
grantee
=
grantee
;
request_data
.
params
.
params
.
target
=
target
;
request_data
.
params
.
params
.
right
=
{
'_content'
:
right_name
};
this
.
performRequest
(
request_data
);
return
this
.
performRequest
(
request_data
);
}
// Search the Directory
...
...
@@ -481,7 +480,7 @@ export default class ZimbraAdminApi {
const
request_data
=
this
.
buildRequestData
(
`SearchDirectory`
,
callback
);
request_data
.
params
.
params
=
search_object
;
request_data
.
parse_response
=
this
.
parseSearchResponse
;
this
.
performRequest
(
request_data
);
return
this
.
performRequest
(
request_data
);
}
// TODO: Fucking ugly code to make it better
...
...
@@ -494,7 +493,7 @@ export default class ZimbraAdminApi {
'by'
:
this
.
dictionary
.
byIdOrName
(
domain_idenfitier
),
'_content'
:
domain_idenfitier
};
this
.
performRequest
(
request_data
);
return
this
.
performRequest
(
request_data
);
}
// TODO: Fix this ugly FCKing Code
...
...
@@ -512,15 +511,11 @@ export default class ZimbraAdminApi {
};
request_data
.
requests
=
[];
domains_ids
.
forEach
((
domain_id
)
=>
{
const
request
=
this
.
buildRequestData
(
`CountAccount`
,
callback
);
request
.
parse_response
=
this
.
parseCountAccountResponse
;
request
.
params
.
params
.
domain
=
{
'by'
:
this
.
dictionary
.
byIdOrName
(
domain_id
),
'_content'
:
domain_id
};
const
request
=
this
.
countAccounts
(
domain_id
);
request_data
.
requests
.
push
(
request
);
});
this
.
performRequest
(
request_data
,
true
);
request_data
.
batch
=
true
;
return
this
.
performRequest
(
request_data
);
}
// TODO: TO ugly
...
...
@@ -541,7 +536,7 @@ export default class ZimbraAdminApi {
return
callback
(
null
,
{});
}
};
this
.
performRequest
(
request_data
);
return
this
.
performRequest
(
request_data
);
}
// TODO: Ugly
...
...
@@ -557,7 +552,7 @@ export default class ZimbraAdminApi {
};
return
callback
(
null
,
result
);
};
this
.
performRequest
(
request_data
);
return
this
.
performRequest
(
request_data
);
}
}
...
...
src/zimbra/domain.js
View file @
9835cecb
...
...
@@ -13,47 +13,33 @@ export default class Domain extends Zimbra {
addAdmin
(
account_id
,
callback
)
{
const
request_data
=
{};
const
grantee_data
=
{
'type'
:
'usr'
,
'identifier'
:
account_id
};
let
modifyAccountRequest
=
this
.
api
.
modifyAccount
(
account_id
,
{
zimbraIsDelegatedAdminAccount
:
'TRUE'
},
callback
,
true
);
const
grantRightRequest
=
this
.
grantRight
(
grantee_data
,
this
.
domainAdminRights
,
callback
,
true
);
const
modifyAccountRequest
=
this
.
api
.
modifyAccount
(
account_id
,
{
zimbraIsDelegatedAdminAccount
:
'TRUE'
}
);
const
grantRightRequest
=
this
.
grantRight
(
grantee_data
,
this
.
domainAdminRights
);
request_data
.
requests
=
[
modifyAccountRequest
,
grantRightRequest
];
request_data
.
batch
=
true
;
request_data
.
callback
=
function
(
err
,
data
)
{
if
(
err
)
return
callback
(
err
);
callback
(
null
,
data
.
GrantRightResponse
);
};
this
.
api
.
performRequest
(
request_data
,
true
);
this
.
api
.
performRequest
(
request_data
);
}
// TODO: Fix this fucking ugly code
getAdmins
(
callback
)
{
const
that
=
this
;
this
.
getAdminsIdsFromGrants
(
function
(
e
,
d
){
if
(
e
)
return
callback
(
e
);
if
(
d
.
length
<
1
)
return
callback
(
null
,
[]);
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
});
});
const
admins_ids
=
this
.
getAdminsIdsFromGrants
();
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
(
callback
)
{
getAdminsIdsFromGrants
()
{
const
ids
=
[];
this
.
getACLs
(
function
(
err
,
data
){
if
(
err
)
return
callback
(
err
);
data
.
forEach
((
grant
)
=>
{
if
(
grant
.
isDomainAdminGrant
())
ids
.
push
(
grant
.
granteeId
);
});
return
callback
(
null
,
ids
);
this
.
parseACL
(
this
.
attrs
.
zimbraACE
).
forEach
((
grantee
)
=>
{
if
(
grantee
.
right
===
this
.
domainAdminRights
)
ids
.
push
(
grantee
.
id
);
});
return
ids
;
}
getAllDistributionLists
(
callback
)
{
...
...
@@ -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
()
{
const
results
=
{};
if
(
typeof
this
.
attrs
.
zimbraDomainCOSMaxAccounts
===
'undefined'
)
return
null
;
...
...
src/zimbra/zimbra.js
View file @
9835cecb
...
...
@@ -34,10 +34,12 @@ export default class Zimbra {
parseACL
(
acls
)
{
const
elements
=
[].
concat
.
apply
([],
[
acls
]);
const
grantees
=
{};
elements
.
forEach
((
el
)
=>
{
grantee_data
=
el
.
split
(
/ /
);
grantees
[
grantee_data
[
0
]]
=
{
type
:
grantee_data
[
1
],
right
:
grantee_data
[
2
]};
const
grantees
=
[];
// Filter to remove undefined
// http://stackoverflow.com/questions/28607451/removing-undefined-values-from-array
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
;
}
...
...
test/js/spec/test.js
View file @
9835cecb
...
...
@@ -8,63 +8,9 @@
'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
()
{
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
)
{
let
api
=
new
ZimbraAdminApi
(
auth_data
);
...
...
@@ -471,8 +417,9 @@
if
(
err
)
console
.
error
(
err
);
let
domain
=
data
;
domain
.
getAdmins
(
function
(
e
,
d
){
expect
(
d
.
length
).
to
.
be
.
above
(
1
);
expect
(
d
[
0
].
constructor
.
name
).
to
.
be
.
equal
(
'Account'
);
if
(
e
)
return
console
.
error
(
e
);
expect
(
d
.
account
.
length
).
to
.
be
.
above
(
1
);
expect
(
d
.
account
[
0
].
constructor
.
name
).
to
.
be
.
equal
(
'Account'
);
done
();
});
});
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment