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