Commit d2d21704 authored by Patricio Bruna's avatar Patricio Bruna

Replaced all getAllxx Functions with directorySearch()

parent d85d3949
...@@ -125,6 +125,7 @@ return /******/ (function(modules) { // webpackBootstrap ...@@ -125,6 +125,7 @@ return /******/ (function(modules) { // webpackBootstrap
this._client = new jszimbra.Communication({ url: auth_object.url }); this._client = new jszimbra.Communication({ url: auth_object.url });
this.parseAllResponse = this.parseAllResponse.bind(this); this.parseAllResponse = this.parseAllResponse.bind(this);
this.parseResponse = this.parseResponse.bind(this); this.parseResponse = this.parseResponse.bind(this);
this.parseSearchResponse = this.parseSearchResponse.bind(this);
this.dictionary = new _dictionary2.default(); this.dictionary = new _dictionary2.default();
} }
...@@ -208,24 +209,40 @@ return /******/ (function(modules) { // webpackBootstrap ...@@ -208,24 +209,40 @@ return /******/ (function(modules) { // webpackBootstrap
} }
}, { }, {
key: 'parseResponse', key: 'parseResponse',
value: function parseResponse(data, obj, callback) { value: function parseResponse(data, request_data, callback) {
var resource = obj.resource.toLowerCase(); var resource = request_data.resource.toLowerCase();
var that = this; var that = this;
var response_name = that.dictionary.resourceResponseName(resource); var response_name = that.dictionary.resourceResponseName(resource);
var response_object = data.get()[obj.response_name][response_name][0]; var response_object = data.get()[request_data.response_name][response_name][0];
var result = that.dictionary.classFactory(resource, response_object); var result = that.dictionary.classFactory(resource, response_object);
return callback(null, result); return callback(null, result);
} }
}, { }, {
key: 'parseRawResponse', key: 'parseSearchResponse',
value: function parseRawResponse(data, obj, callback) {} value: function parseSearchResponse(data, request_data, callback) {
var response_types = this.dictionary.searchResponseTypes();
var response_object = data.get()[request_data.response_name];
var result = { total: response_object.searchTotal, more: response_object.more };
var that = this;
response_types.forEach(function (type) {
var resources = [];
if (typeof response_object[type] !== 'undefined') {
response_object[type].forEach(function (resource) {
var object = that.dictionary.classFactory(type, resource);
resources.push(object);
});
result[type] = resources;
}
});
return callback(null, result);
}
}, { }, {
key: 'parseAllResponse', key: 'parseAllResponse',
value: function parseAllResponse(data, obj, callback) { value: function parseAllResponse(data, request_data, callback) {
var resource = obj.resource.toLowerCase(); var resource = request_data.resource.toLowerCase();
var that = this; var that = this;
var response_name = that.dictionary.resourceResponseName(resource); var response_name = that.dictionary.resourceResponseName(resource);
var response_object = data.get()[obj.response_name][response_name]; var response_object = data.get()[request_data.response_name][response_name];
var response_array = []; var response_array = [];
response_object.forEach(function (r) { response_object.forEach(function (r) {
var element = that.dictionary.classFactory(resource, r); var element = that.dictionary.classFactory(resource, r);
...@@ -320,17 +337,34 @@ return /******/ (function(modules) { // webpackBootstrap ...@@ -320,17 +337,34 @@ return /******/ (function(modules) { // webpackBootstrap
}, { }, {
key: 'getAllDomains', key: 'getAllDomains',
value: function getAllDomains(callback) { value: function getAllDomains(callback) {
this.getAll('Domain', callback); var query_object = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
query_object.types = 'domains';
this.directorySearch(query_object, callback);
} }
}, { }, {
key: 'getAllAccounts', key: 'getAllAccounts',
value: function getAllAccounts(callback) { value: function getAllAccounts(callback) {
this.getAll('Account', callback); var query_object = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
query_object.types = 'accounts';
this.directorySearch(query_object, callback);
} }
}, { }, {
key: 'getAllDistributionLists', key: 'getAllDistributionLists',
value: function getAllDistributionLists(callback) { value: function getAllDistributionLists(callback) {
this.getAll('DistributionList', callback); var query_object = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
query_object.types = 'distributionlists';
this.directorySearch(query_object, callback);
}
}, {
key: 'getAllAliases',
value: function getAllAliases(callback) {
var query_object = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
query_object.types = 'aliases';
this.directorySearch(query_object, callback);
} }
// Get current logged account information // Get current logged account information
...@@ -350,22 +384,6 @@ return /******/ (function(modules) { // webpackBootstrap ...@@ -350,22 +384,6 @@ return /******/ (function(modules) { // webpackBootstrap
this.performRequest(request_data); this.performRequest(request_data);
} }
// const req_params =
// const that = this;
// this.client.getRequest({}, function(err, req) {
// if (err) return callback(this.handleError(err));
//
// req.addRequest(req_params, function(err){
// if (err) return callback(that.handleError(err));
// that.client.send(req, function(err, data){
// if (err) return callback(that.handleError(err));
// const result = data.response[0].GetInfoResponse
// return callback(null, result);
// });
// });
// });
// }
// TODO: Fix this fucking code // TODO: Fix this fucking code
// Search the Directory // Search the Directory
// search_object = { // search_object = {
...@@ -385,7 +403,17 @@ return /******/ (function(modules) { // webpackBootstrap ...@@ -385,7 +403,17 @@ return /******/ (function(modules) { // webpackBootstrap
}, { }, {
key: 'directorySearch', key: 'directorySearch',
value: function directorySearch(search_object, callback) {} value: function directorySearch(search_object, callback) {
var request_data = {};
request_data.params = this.requestParams();
request_data.params.params = search_object;
request_data.request_name = "SearchDirectory";
request_data.response_name = "SearchDirectoryResponse";
request_data.params.name = request_data.request_name + 'Request';
request_data.callback = callback;
request_data.parse_response = this.parseSearchResponse;
this.performRequest(request_data);
}
}, { }, {
key: 'token', key: 'token',
set: function set(token) { set: function set(token) {
...@@ -11956,6 +11984,10 @@ return /******/ (function(modules) { // webpackBootstrap ...@@ -11956,6 +11984,10 @@ return /******/ (function(modules) { // webpackBootstrap
value: true value: true
}); });
var _keys = __webpack_require__(278);
var _keys2 = _interopRequireDefault(_keys);
var _entries = __webpack_require__(74); var _entries = __webpack_require__(74);
var _entries2 = _interopRequireDefault(_entries); var _entries2 = _interopRequireDefault(_entries);
...@@ -11980,12 +12012,19 @@ return /******/ (function(modules) { // webpackBootstrap ...@@ -11980,12 +12012,19 @@ return /******/ (function(modules) { // webpackBootstrap
var _account2 = _interopRequireDefault(_account); var _account2 = _interopRequireDefault(_account);
var _alias = __webpack_require__(281);
var _alias2 = _interopRequireDefault(_alias);
var _distribution_list = __webpack_require__(112); var _distribution_list = __webpack_require__(112);
var _distribution_list2 = _interopRequireDefault(_distribution_list); var _distribution_list2 = _interopRequireDefault(_distribution_list);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
// Copyright (c) 2016 ZBox, Spa. All Rights Reserved.
// See LICENSE.txt for license information.
var Dictionary = function () { var Dictionary = function () {
function Dictionary() { function Dictionary() {
(0, _classCallCheck3.default)(this, Dictionary); (0, _classCallCheck3.default)(this, Dictionary);
...@@ -12035,6 +12074,16 @@ return /******/ (function(modules) { // webpackBootstrap ...@@ -12035,6 +12074,16 @@ return /******/ (function(modules) { // webpackBootstrap
value: function resourceResponseName(resource) { value: function resourceResponseName(resource) {
return this.zimbra_resources[resource.toLowerCase()].response_name; return this.zimbra_resources[resource.toLowerCase()].response_name;
} }
}, {
key: 'searchResponseTypes',
value: function searchResponseTypes() {
var result = [];
var that = this;
(0, _keys2.default)(this.zimbra_resources).forEach(function (k) {
result.push(that.zimbra_resources[k].response_name);
});
return result;
}
}, { }, {
key: 'ZimbraResources', key: 'ZimbraResources',
value: function ZimbraResources() { value: function ZimbraResources() {
...@@ -12050,13 +12099,21 @@ return /******/ (function(modules) { // webpackBootstrap ...@@ -12050,13 +12099,21 @@ return /******/ (function(modules) { // webpackBootstrap
distributionlist: { distributionlist: {
class_name: _distribution_list2.default, class_name: _distribution_list2.default,
response_name: 'dl' response_name: 'dl'
},
dl: {
class_name: _distribution_list2.default,
response_name: 'dl'
},
alias: {
response_name: 'alias',
class_name: _alias2.default
} }
}; };
} }
}]); }]);
return Dictionary; return Dictionary;
}(); // Copyright (c) 2016 ZBox, Spa. All Rights Reserved. }();
// See LICENSE.txt for license information.
exports.default = Dictionary; exports.default = Dictionary;
...@@ -13029,7 +13086,7 @@ return /******/ (function(modules) { // webpackBootstrap ...@@ -13029,7 +13086,7 @@ return /******/ (function(modules) { // webpackBootstrap
this.name = resource_obj.name; this.name = resource_obj.name;
this.id = resource_obj.id; this.id = resource_obj.id;
this.attrs = this.buildAttrsMap(resource_obj.a); this.attrs = this.buildAttrsMap(resource_obj.a);
this.obj = resource_obj; // this.obj = resource_obj;
} }
(0, _createClass3.default)(Zimbra, [{ (0, _createClass3.default)(Zimbra, [{
...@@ -39673,6 +39730,77 @@ return /******/ (function(modules) { // webpackBootstrap ...@@ -39673,6 +39730,77 @@ return /******/ (function(modules) { // webpackBootstrap
/* WEBPACK VAR INJECTION */}.call(exports, __webpack_require__(134).Buffer)) /* WEBPACK VAR INJECTION */}.call(exports, __webpack_require__(134).Buffer))
/***/ },
/* 278 */
/***/ function(module, exports, __webpack_require__) {
/*** IMPORTS FROM imports-loader ***/
var $ = __webpack_require__(3);
var jQuery = __webpack_require__(3);
module.exports = { "default": __webpack_require__(279), __esModule: true };
/***/ },
/* 279 */
/***/ function(module, exports, __webpack_require__) {
/*** IMPORTS FROM imports-loader ***/
var $ = __webpack_require__(3);
var jQuery = __webpack_require__(3);
__webpack_require__(280);
module.exports = __webpack_require__(15).Object.keys;
/***/ },
/* 280 */
/***/ function(module, exports, __webpack_require__) {
/*** IMPORTS FROM imports-loader ***/
var $ = __webpack_require__(3);
var jQuery = __webpack_require__(3);
// 19.1.2.14 Object.keys(O)
var toObject = __webpack_require__(50)
, $keys = __webpack_require__(34);
__webpack_require__(100)('keys', function(){
return function keys(it){
return $keys(toObject(it));
};
});
/***/ },
/* 281 */
/***/ function(module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _classCallCheck2 = __webpack_require__(68);
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
// Copyright (c) 2016 ZBox, Spa. All Rights Reserved.
// See LICENSE.txt for license information.
var Alias = function Alias(resource_obj) {
(0, _classCallCheck3.default)(this, Alias);
this.name = resource_obj.name;
this.id = resource_obj.id;
this.targetName = resource_obj.targetName;
};
exports.default = Alias;
/***/ } /***/ }
/******/ ]) /******/ ])
}); });
This source diff could not be displayed because it is too large. You can view the blob instead.
{ {
"name": "zimbra-admin-api-js", "name": "zimbra-admin-api-js",
"version": "0.0.6", "version": "0.0.7",
"private": true, "private": true,
"main": "lib/zimbra-admin-api.js", "main": "lib/zimbra-admin-api.js",
"dependencies": { "dependencies": {
......
...@@ -32,6 +32,7 @@ export default class ZimbraAdminApi { ...@@ -32,6 +32,7 @@ export default class ZimbraAdminApi {
this._client = new jszimbra.Communication({url: auth_object.url}); this._client = new jszimbra.Communication({url: auth_object.url});
this.parseAllResponse = this.parseAllResponse.bind(this); this.parseAllResponse = this.parseAllResponse.bind(this);
this.parseResponse = this.parseResponse.bind(this); this.parseResponse = this.parseResponse.bind(this);
this.parseSearchResponse = this.parseSearchResponse.bind(this);
this.dictionary = new Dictionary(); this.dictionary = new Dictionary();
} }
...@@ -115,25 +116,38 @@ export default class ZimbraAdminApi { ...@@ -115,25 +116,38 @@ export default class ZimbraAdminApi {
} }
} }
parseResponse(data, obj, callback) { parseResponse(data, request_data, callback) {
const resource = obj.resource.toLowerCase(); const resource = request_data.resource.toLowerCase();
const that = this; const that = this;
const response_name = that.dictionary.resourceResponseName(resource); const response_name = that.dictionary.resourceResponseName(resource);
const response_object = data.get()[obj.response_name][response_name][0]; const response_object = data.get()[request_data.response_name][response_name][0];
const result = that.dictionary.classFactory(resource, response_object); const result = that.dictionary.classFactory(resource, response_object);
return callback(null, result); return callback(null, result);
} }
parseRawResponse(data, obj, callback) { parseSearchResponse(data, request_data, callback) {
const response_types = this.dictionary.searchResponseTypes();
const response_object = data.get()[request_data.response_name];
const result = { total: response_object.searchTotal, more: response_object.more };
const that = this;
response_types.forEach((type) => {
const resources = [];
if (typeof response_object[type] !== 'undefined') {
response_object[type].forEach((resource) => {
const object = that.dictionary.classFactory(type, resource);
resources.push(object);
});
result[type] = resources;
}
});
return callback(null, result);
} }
parseAllResponse(data, request_data, callback){
parseAllResponse(data, obj, callback){ const resource = request_data.resource.toLowerCase();
const resource = obj.resource.toLowerCase();
const that = this; const that = this;
const response_name = that.dictionary.resourceResponseName(resource); const response_name = that.dictionary.resourceResponseName(resource);
const response_object = data.get()[obj.response_name][response_name]; const response_object = data.get()[request_data.response_name][response_name];
const response_array = []; const response_array = [];
response_object.forEach((r) => { response_object.forEach((r) => {
let element = that.dictionary.classFactory(resource, r); let element = that.dictionary.classFactory(resource, r);
...@@ -216,16 +230,24 @@ export default class ZimbraAdminApi { ...@@ -216,16 +230,24 @@ export default class ZimbraAdminApi {
this.get('DistributionList', identifier, callback); this.get('DistributionList', identifier, callback);
} }
getAllDomains(callback) { getAllDomains(callback, query_object = {}) {
this.getAll('Domain', callback); query_object.types = 'domains';
this.directorySearch(query_object, callback);
}
getAllAccounts(callback, query_object = {}) {
query_object.types = 'accounts';
this.directorySearch(query_object, callback);
} }
getAllAccounts(callback) { getAllDistributionLists(callback, query_object = {}) {
this.getAll('Account', callback); query_object.types = 'distributionlists';
this.directorySearch(query_object, callback);
} }
getAllDistributionLists(callback) { getAllAliases(callback, query_object = {}) {
this.getAll('DistributionList', callback); query_object.types = 'aliases';
this.directorySearch(query_object, callback);
} }
// Get current logged account information // Get current logged account information
...@@ -242,23 +264,6 @@ export default class ZimbraAdminApi { ...@@ -242,23 +264,6 @@ export default class ZimbraAdminApi {
this.performRequest(request_data); this.performRequest(request_data);
} }
// const req_params =
// const that = this;
// this.client.getRequest({}, function(err, req) {
// if (err) return callback(this.handleError(err));
//
// req.addRequest(req_params, function(err){
// if (err) return callback(that.handleError(err));
// that.client.send(req, function(err, data){
// if (err) return callback(that.handleError(err));
// const result = data.response[0].GetInfoResponse
// return callback(null, result);
// });
// });
// });
// }
// TODO: Fix this fucking code // TODO: Fix this fucking code
// Search the Directory // Search the Directory
// search_object = { // search_object = {
...@@ -276,7 +281,15 @@ export default class ZimbraAdminApi { ...@@ -276,7 +281,15 @@ export default class ZimbraAdminApi {
// attrs: Comma separated list of attributes // attrs: Comma separated list of attributes
// } // }
directorySearch(search_object, callback) { directorySearch(search_object, callback) {
const request_data = { };
request_data.params = this.requestParams();
request_data.params.params = search_object;
request_data.request_name = "SearchDirectory";
request_data.response_name = "SearchDirectoryResponse";
request_data.params.name = `${request_data.request_name}Request`;
request_data.callback = callback;
request_data.parse_response = this.parseSearchResponse;
this.performRequest(request_data);
} }
} }
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
import Domain from './../zimbra/domain.js'; import Domain from './../zimbra/domain.js';
import Account from './../zimbra/account.js'; import Account from './../zimbra/account.js';
import Alias from './../zimbra/alias.js';
import DistributionList from './../zimbra/distribution_list.js'; import DistributionList from './../zimbra/distribution_list.js';
export default class Dictionary { export default class Dictionary {
...@@ -26,7 +27,7 @@ export default class Dictionary { ...@@ -26,7 +27,7 @@ export default class Dictionary {
// {size: 20, age: 30} => [ {n: size, _content: 20}, {n: age, _content: 30}] // {size: 20, age: 30} => [ {n: size, _content: 20}, {n: age, _content: 30}]
attributesToArray (attributes) { attributesToArray (attributes) {
if ($.isEmptyObject(attributes)) return []; if ($.isEmptyObject(attributes)) return [];
const result = [] const result = [];
const map = new Map(Object.entries(attributes)); const map = new Map(Object.entries(attributes));
map.forEach((key, value) => { map.forEach((key, value) => {
result.push({ 'n': value, '_content': key }); result.push({ 'n': value, '_content': key });
...@@ -43,6 +44,15 @@ export default class Dictionary { ...@@ -43,6 +44,15 @@ export default class Dictionary {
return this.zimbra_resources[resource.toLowerCase()].response_name; return this.zimbra_resources[resource.toLowerCase()].response_name;
} }
searchResponseTypes () {
const result = [];
const that = this;
Object.keys(this.zimbra_resources).forEach((k) => {
result.push(that.zimbra_resources[k].response_name);
});
return result;
}
ZimbraResources() { ZimbraResources() {
return { return {
domain: { domain: {
...@@ -56,7 +66,16 @@ export default class Dictionary { ...@@ -56,7 +66,16 @@ export default class Dictionary {
distributionlist: { distributionlist: {
class_name: DistributionList, class_name: DistributionList,
response_name: 'dl' response_name: 'dl'
} },
dl: {
class_name: DistributionList,
response_name: 'dl'
},
alias: {
response_name: 'alias',
class_name: Alias,
},
}; };
} }
......
// Copyright (c) 2016 ZBox, Spa. All Rights Reserved.
// See LICENSE.txt for license information.
export default class Alias {
constructor(resource_obj) {
this.name = resource_obj.name;
this.id = resource_obj.id;
this.targetName = resource_obj.targetName;
}
}
...@@ -6,7 +6,7 @@ export default class Zimbra { ...@@ -6,7 +6,7 @@ export default class Zimbra {
this.name = resource_obj.name; this.name = resource_obj.name;
this.id = resource_obj.id; this.id = resource_obj.id;
this.attrs = this.buildAttrsMap(resource_obj.a); this.attrs = this.buildAttrsMap(resource_obj.a);
this.obj = resource_obj; // this.obj = resource_obj;
} }
buildAttrsMap(obj_ary) { buildAttrsMap(obj_ary) {
......
...@@ -74,7 +74,7 @@ ...@@ -74,7 +74,7 @@
let api = new ZimbraAdminApi(auth_data); let api = new ZimbraAdminApi(auth_data);
api.getAllDomains(function(err, data){ api.getAllDomains(function(err, data){
if (err) console.log(err); if (err) console.log(err);
expect(data[0].constructor.name).to.equal('Domain'); expect(data.domain[0].constructor.name).to.equal('Domain');
done(); done();
}); });
}); });
...@@ -85,7 +85,7 @@ ...@@ -85,7 +85,7 @@
// var proxy = api.getAllAccounts(callback); // var proxy = api.getAllAccounts(callback);
api.getAllAccounts(function(err, data){ api.getAllAccounts(function(err, data){
if (err) console.log(err); if (err) console.log(err);
expect(data[0].constructor.name).to.equal('Account'); expect(data.account[0].constructor.name).to.equal('Account');
done(); done();
}); });
}); });
...@@ -94,7 +94,7 @@ ...@@ -94,7 +94,7 @@
let api = new ZimbraAdminApi(auth_data); let api = new ZimbraAdminApi(auth_data);
api.getAllDistributionLists(function(err, data){ api.getAllDistributionLists(function(err, data){
if (err) console.log(err); if (err) console.log(err);
expect(data[0].constructor.name).to.equal('DistributionList'); expect(data.dl[0].constructor.name).to.equal('DistributionList');
done(); done();
}); });
}); });
...@@ -145,7 +145,7 @@ ...@@ -145,7 +145,7 @@
}); });
it('should create and return an account', function(done){ it('should create and return an account', function(done){
let account_name = Date.now() + '@zboxapp.dev'; let account_name = Date.now() + '@big.com';
let account_password = Date.now(); let account_password = Date.now();
let account_attributes = {}; let account_attributes = {};
let api = new ZimbraAdminApi(auth_data); let api = new ZimbraAdminApi(auth_data);
...@@ -195,7 +195,7 @@ ...@@ -195,7 +195,7 @@
it('should create and return an account with extra attributes', function(done){ it('should create and return an account with extra attributes', function(done){
let account_name = Date.now() + '@zboxapp.dev'; let account_name = Date.now() + '@big.com';
let account_password = Date.now(); let account_password = Date.now();
let account_attributes = { 'sn': 'Bruna', 'givenName': 'Patricio' }; let account_attributes = { 'sn': 'Bruna', 'givenName': 'Patricio' };
let api = new ZimbraAdminApi(auth_data); let api = new ZimbraAdminApi(auth_data);
...@@ -220,6 +220,17 @@ ...@@ -220,6 +220,17 @@
api.login(callback); api.login(callback);
}); });
it('should return directorySearch with total info', function(done){
let api = new ZimbraAdminApi(auth_data);
let query_object = {limit: 10, domain: 'customer.dev', types: "accounts,distributionlists,aliases"};
api.directorySearch(query_object, function(err, data){
expect(data.more).to.equal(true);
expect(data.total).to.be.above(1);
expect(data.account.length).to.be.at.least(2);
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