Commit bea33a06 authored by Patricio Bruna's avatar Patricio Bruna

Works with NodeJS

parent e6592c8b
This diff is collapsed.
This diff is collapsed.
// Copyright (c) 2016 ZBox, Spa. All Rights Reserved. // Copyright (c) 2016 ZBox, Spa. All Rights Reserved.
// See LICENSE.txt for license information. // See LICENSE.txt for license information.
'use strict';
var jszimbra = require('js-zimbra'); var jszimbra = require('js-zimbra');
var pjson = require('../package.json'); var pjson = require('../package.json');
import Dictionary from './utils/dictionary.js'; var Dictionary = require('./utils/dictionary.js');
import ResponseParser from './utils/response_parser.js'; var ResponseParser = require('./utils/response_parser.js');
import Error from './zimbra/error.js'; var ErrorBuilder = require('./zimbra/error.js');
// TODO: To many parseResponse types // TODO: To many parseResponse types
export default class ZimbraAdminApi { class ZimbraAdminApi {
constructor(auth_object) { constructor(auth_object) {
this.url = auth_object.url; this.url = auth_object.url;
this.user = auth_object.user; this.user = auth_object.user;
...@@ -66,7 +68,7 @@ export default class ZimbraAdminApi { ...@@ -66,7 +68,7 @@ export default class ZimbraAdminApi {
} }
handleError(err) { handleError(err) {
return new Error(err); return new ErrorBuilder(err);
} }
handleResponse(_, response) { handleResponse(_, response) {
...@@ -88,7 +90,8 @@ export default class ZimbraAdminApi { ...@@ -88,7 +90,8 @@ export default class ZimbraAdminApi {
} }
buildRequest(options = {}) { buildRequest(options) {
options = options || {};
let request = null; let request = null;
const that = this; const that = this;
this.client.getRequest(options, (err, req) => { this.client.getRequest(options, (err, req) => {
...@@ -98,7 +101,8 @@ export default class ZimbraAdminApi { ...@@ -98,7 +101,8 @@ export default class ZimbraAdminApi {
return request; return request;
} }
makeBatchRequest(request_data_array, callback, error = {onError: 'stop'}) { makeBatchRequest(request_data_array, callback, error) {
error = error || {onError: 'stop'}
const that = this; const that = this;
if (request_data_array.length === 0) return; if (request_data_array.length === 0) return;
let request_object = this.buildRequest({isBatch: true, batchOnError: error.onError}); let request_object = this.buildRequest({isBatch: true, batchOnError: error.onError});
...@@ -214,7 +218,9 @@ export default class ZimbraAdminApi { ...@@ -214,7 +218,9 @@ export default class ZimbraAdminApi {
// } // }
grantRight(target_data, grantee_data, right_name, callback) { grantRight(target_data, grantee_data, right_name, callback) {
const request_data = this.buildRequestData('GrantRight', callback); const request_data = this.buildRequestData('GrantRight', callback);
const [target, grantee] = this.dictionary.buildTargetGrantee(target_data, grantee_data); const target_grantee = this.dictionary.buildTargetGrantee(target_data, grantee_data);
const target = target_grantee[0];
const grantee = target_grantee[1];
request_data.parse_response = ResponseParser.emptyResponse; request_data.parse_response = ResponseParser.emptyResponse;
request_data.params.params.grantee = grantee; request_data.params.params.grantee = grantee;
request_data.params.params.target = target; request_data.params.params.target = target;
...@@ -308,22 +314,26 @@ export default class ZimbraAdminApi { ...@@ -308,22 +314,26 @@ export default class ZimbraAdminApi {
return this.create('DistributionList', resource_data, callback); return this.create('DistributionList', resource_data, callback);
} }
getAllDomains(callback, query_object = {}) { getAllDomains(callback, query_object) {
query_object = query_object || {};
query_object.types = 'domains'; query_object.types = 'domains';
return this.directorySearch(query_object, callback); return this.directorySearch(query_object, callback);
} }
getAllAccounts(callback, query_object = {}) { getAllAccounts(callback, query_object) {
query_object = query_object || {};
query_object.types = 'accounts'; query_object.types = 'accounts';
return this.directorySearch(query_object, callback); return this.directorySearch(query_object, callback);
} }
getAllDistributionLists(callback, query_object = {}) { getAllDistributionLists(callback, query_object) {
query_object = query_object || {};
query_object.types = 'distributionlists'; query_object.types = 'distributionlists';
return this.directorySearch(query_object, callback); return this.directorySearch(query_object, callback);
} }
getAllAliases(callback, query_object = {}) { getAllAliases(callback, query_object) {
query_object = query_object || {};
query_object.types = 'aliases'; query_object.types = 'aliases';
return this.directorySearch(query_object, callback); return this.directorySearch(query_object, callback);
} }
...@@ -350,7 +360,9 @@ export default class ZimbraAdminApi { ...@@ -350,7 +360,9 @@ export default class ZimbraAdminApi {
// identifier: (name or zimbraId) // identifier: (name or zimbraId)
// } // }
getGrants(target_data, grantee_data, callback) { getGrants(target_data, grantee_data, callback) {
const [target, grantee] = this.dictionary.buildTargetGrantee(target_data, grantee_data); const target_grantee = this.dictionary.buildTargetGrantee(target_data, grantee_data);
const target = target_grantee[0];
const grantee = target_grantee[1];
const request_data = this.buildRequestData('GetGrants', callback); const request_data = this.buildRequestData('GetGrants', callback);
request_data.resource = 'Grant'; request_data.resource = 'Grant';
request_data.parse_response = ResponseParser.allResponse; request_data.parse_response = ResponseParser.allResponse;
...@@ -443,7 +455,9 @@ export default class ZimbraAdminApi { ...@@ -443,7 +455,9 @@ export default class ZimbraAdminApi {
} }
revokeRight(target_data, grantee_data, right_name, callback) { revokeRight(target_data, grantee_data, right_name, callback) {
const [target, grantee] = this.dictionary.buildTargetGrantee(target_data, grantee_data); const target_grantee = this.dictionary.buildTargetGrantee(target_data, grantee_data);
const target = target_grantee[0];
const grantee = target_grantee[1];
const request_data = this.buildRequestData('RevokeRight', callback); const request_data = this.buildRequestData('RevokeRight', callback);
request_data.parse_response = ResponseParser.emptyResponse; request_data.parse_response = ResponseParser.emptyResponse;
request_data.params.params.grantee = grantee; request_data.params.params.grantee = grantee;
...@@ -530,5 +544,5 @@ export default class ZimbraAdminApi { ...@@ -530,5 +544,5 @@ export default class ZimbraAdminApi {
if (typeof module === 'object' && typeof module.exports === 'object') { if (typeof module === 'object' && typeof module.exports === 'object') {
module.exports = ZimbraAdminApi; module.exports = ZimbraAdminApi;
} else { } else {
global.window.ZimbraAdminApi = ZimbraAdminApi; window.ZimbraAdminApi = ZimbraAdminApi;
} }
// Copyright (c) 2016 ZBox, Spa. All Rights Reserved. // Copyright (c) 2016 ZBox, Spa. All Rights Reserved.
// See LICENSE.txt for license information. // See LICENSE.txt for license information.
import Domain from './../zimbra/domain.js'; 'use strict';
import Account from './../zimbra/account.js';
import Alias from './../zimbra/alias.js'; var Domain = require('./../zimbra/domain.js');
import Cos from './../zimbra/cos.js'; var Account = require('./../zimbra/account.js');
import Grant from './../zimbra/grant.js'; var Alias = require('./../zimbra/alias.js');
import DistributionList from './../zimbra/distribution_list.js'; var Cos = require('./../zimbra/cos.js');
var Grant = require('./../zimbra/grant.js');
export default class Dictionary { var DistributionList = require('./../zimbra/distribution_list.js');
class Dictionary {
constructor() { constructor() {
this.zimbra_resources = this.ZimbraResources(); this.zimbra_resources = this.ZimbraResources();
} }
...@@ -176,3 +178,5 @@ export default class Dictionary { ...@@ -176,3 +178,5 @@ export default class Dictionary {
} }
} }
module.exports = Dictionary;
\ No newline at end of file
// Copyright (c) 2016 ZBox, Spa. All Rights Reserved. // Copyright (c) 2016 ZBox, Spa. All Rights Reserved.
// See LICENSE.txt for license information. // See LICENSE.txt for license information.
import Dictionary from './dictionary.js'; 'use strict';
import Error from '../zimbra/error.js';
export default class ResponseParser { var Dictionary = require('./dictionary.js');
var ErrorBuilder = require('../zimbra/error.js');
class ResponseParser {
static dictionary() { static dictionary() {
return new Dictionary(); return new Dictionary();
...@@ -27,7 +29,7 @@ export default class ResponseParser { ...@@ -27,7 +29,7 @@ export default class ResponseParser {
if(response_object.Fault && response_object.Fault.length >= 1 ) { if(response_object.Fault && response_object.Fault.length >= 1 ) {
const errors = []; const errors = [];
response_object.Fault.forEach((e) =>{ response_object.Fault.forEach((e) =>{
errors.push(new Error(e)); errors.push(new ErrorBuilder(e));
}); });
response_object.errors = errors; response_object.errors = errors;
} }
...@@ -114,10 +116,12 @@ export default class ResponseParser { ...@@ -114,10 +116,12 @@ export default class ResponseParser {
statusText: response_object.message[0]._content, statusText: response_object.message[0]._content,
responseJSON: {} responseJSON: {}
}; };
return callback(new Error(err)); return callback(new ErrorBuilder(err));
} else { } else {
return callback(null, {}); return callback(null, {});
} }
} }
} }
module.exports = ResponseParser;
\ No newline at end of file
// Copyright (c) 2016 ZBox, Spa. All Rights Reserved. // Copyright (c) 2016 ZBox, Spa. All Rights Reserved.
// See LICENSE.txt for license information. // See LICENSE.txt for license information.
import Zimbra from './zimbra.js'; 'use strict';
export default class Account extends Zimbra { var Zimbra = require('./zimbra.js');
class Account extends Zimbra {
constructor(account_obj, zimbra_api_client) { constructor(account_obj, zimbra_api_client) {
super(account_obj, zimbra_api_client); super(account_obj, zimbra_api_client);
this.domain = this.name.split(/@/)[1]; this.domain = this.name.split(/@/)[1];
...@@ -66,3 +68,5 @@ export default class Account extends Zimbra { ...@@ -66,3 +68,5 @@ export default class Account extends Zimbra {
} }
} }
module.exports = Account;
\ No newline at end of file
// Copyright (c) 2016 ZBox, Spa. All Rights Reserved. // Copyright (c) 2016 ZBox, Spa. All Rights Reserved.
// See LICENSE.txt for license information. // See LICENSE.txt for license information.
export default class Alias { 'use strict';
class Alias {
constructor(resource_obj) { constructor(resource_obj) {
this.name = resource_obj.name; this.name = resource_obj.name;
this.id = resource_obj.id; this.id = resource_obj.id;
this.targetName = resource_obj.targetName; this.targetName = resource_obj.targetName;
} }
} }
module.exports = Alias;
\ No newline at end of file
// Copyright (c) 2016 ZBox, Spa. All Rights Reserved. // Copyright (c) 2016 ZBox, Spa. All Rights Reserved.
// See LICENSE.txt for license information. // See LICENSE.txt for license information.
import Zimbra from './zimbra.js'; 'use strict';
export default class Cos extends Zimbra { var Zimbra = require('./zimbra.js');
class Cos extends Zimbra {
constructor(cos_obj, zimbra_api_client) { constructor(cos_obj, zimbra_api_client) {
super(cos_obj, zimbra_api_client); super(cos_obj, zimbra_api_client);
} }
} }
module.exports = Cos;
\ No newline at end of file
// Copyright (c) 2016 ZBox, Spa. All Rights Reserved. // Copyright (c) 2016 ZBox, Spa. All Rights Reserved.
// See LICENSE.txt for license information. // See LICENSE.txt for license information.
import Zimbra from './zimbra.js'; 'use strict';
export default class DistributionList extends Zimbra { var Zimbra = require('./zimbra.js');
class DistributionList extends Zimbra {
constructor(dl_obj, zimbra_api_client) { constructor(dl_obj, zimbra_api_client) {
super(dl_obj, zimbra_api_client); super(dl_obj, zimbra_api_client);
this.members = this.parseMembers(dl_obj); this.members = this.parseMembers(dl_obj);
...@@ -81,3 +83,5 @@ export default class DistributionList extends Zimbra { ...@@ -81,3 +83,5 @@ export default class DistributionList extends Zimbra {
} }
module.exports = DistributionList;
\ No newline at end of file
// Copyright (c) 2016 ZBox, Spa. All Rights Reserved. // Copyright (c) 2016 ZBox, Spa. All Rights Reserved.
// See LICENSE.txt for license information. // See LICENSE.txt for license information.
import Zimbra from './zimbra.js'; 'use strict';
export default class Domain extends Zimbra { var Zimbra = require('./zimbra.js');
class Domain extends Zimbra {
constructor(domain_obj, zimbra_api_client) { constructor(domain_obj, zimbra_api_client) {
super(domain_obj, zimbra_api_client); super(domain_obj, zimbra_api_client);
this.domainAdminRights = 'domainAdminRights'; this.domainAdminRights = 'domainAdminRights';
...@@ -98,3 +100,5 @@ export default class Domain extends Zimbra { ...@@ -98,3 +100,5 @@ export default class Domain extends Zimbra {
} }
} }
module.exports = Domain;
\ No newline at end of file
// Copyright (c) 2016 ZBox, Spa. All Rights Reserved. // Copyright (c) 2016 ZBox, Spa. All Rights Reserved.
// See LICENSE.txt for license information. // See LICENSE.txt for license information.
export default class Error { 'use strict';
class Error {
constructor(err) { constructor(err) {
this.code = err.Fault ? err.Fault.Code.Value : err.status; this.code = err.Fault ? err.Fault.Code.Value : err.status;
this.extra = this.getErrorInfo(err); this.extra = this.getErrorInfo(err);
...@@ -25,3 +27,5 @@ export default class Error { ...@@ -25,3 +27,5 @@ export default class Error {
} }
} }
} }
module.exports = Error;
\ No newline at end of file
// Copyright (c) 2016 ZBox, Spa. All Rights Reserved. // Copyright (c) 2016 ZBox, Spa. All Rights Reserved.
// See LICENSE.txt for license information. // See LICENSE.txt for license information.
export default class Grant { 'use strict';
class Grant {
constructor(grant) { constructor(grant) {
this.grantee = grant.grantee[0]; this.grantee = grant.grantee[0];
this.target = grant.target[0]; this.target = grant.target[0];
...@@ -21,3 +23,5 @@ export default class Grant { ...@@ -21,3 +23,5 @@ export default class Grant {
} }
} }
module.exports = Grant;
\ No newline at end of file
// Copyright (c) 2016 ZBox, Spa. All Rights Reserved. // Copyright (c) 2016 ZBox, Spa. All Rights Reserved.
// See LICENSE.txt for license information. // See LICENSE.txt for license information.
export default class Zimbra { 'use strict';
class Zimbra {
constructor(resource_obj, zimbra_api_client) { constructor(resource_obj, zimbra_api_client) {
this.name = resource_obj.name; this.name = resource_obj.name;
this.id = resource_obj.id; this.id = resource_obj.id;
...@@ -56,12 +58,16 @@ export default class Zimbra { ...@@ -56,12 +58,16 @@ export default class Zimbra {
}); });
} }
grantRight(grantee_data, right_name, callback, forBatch = false){ grantRight(grantee_data, right_name, callback, forBatch){
forBatch = forBatch || false;
return this.api.grantRight(this.buildRighTargetData(), grantee_data, right_name, callback, forBatch); return this.api.grantRight(this.buildRighTargetData(), grantee_data, right_name, callback, forBatch);
} }
revokeRight(grantee_data, right_name, callback, forBatch = false){ revokeRight(grantee_data, right_name, callback, forBatch){
forBatch = forBatch || false;
return this.api.revokeRight(this.buildRighTargetData(), grantee_data, right_name, callback, forBatch); return this.api.revokeRight(this.buildRighTargetData(), grantee_data, right_name, callback, forBatch);
} }
} }
module.exports = Zimbra;
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