Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 52 additions & 44 deletions lib/storage/acl.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

'use strict';

var arrify = require('arrify');
var is = require('is');
var nodeutil = require('util');

Expand Down Expand Up @@ -66,8 +67,8 @@ var nodeutil = require('util');
function Acl(options) {
AclRoleAccessorMethods.call(this);

this.makeReq = options.makeReq;
this.pathPrefix = options.pathPrefix;
this.request_ = options.request;
}

/**
Expand Down Expand Up @@ -232,28 +233,29 @@ nodeutil.inherits(Acl, AclRoleAccessorMethods);
* }, function(err, aclObject, apiResponse) {});
*/
Acl.prototype.add = function(options, callback) {
var that = this;
var self = this;

var body = {
entity: options.entity,
role: options.role.toUpperCase()
};

var query = null;
var query = {};

if (options.generation) {
query = {
generation: options.generation
};
query.generation = options.generation;
}

this.makeReq_('POST', '', query, body, function(err, resp) {
this.request({
method: 'POST',
uri: '',
qs: query,
json: {
entity: options.entity,
role: options.role.toUpperCase()
}
}, function(err, resp) {
if (err) {
callback(err, null, resp);
return;
}

callback(null, that.makeAclObject_(resp), resp);
callback(null, self.makeAclObject_(resp), resp);
});
};

Expand Down Expand Up @@ -287,16 +289,19 @@ Acl.prototype.add = function(options, callback) {
* }, function(err, apiResponse) {});
*/
Acl.prototype.delete = function(options, callback) {
var path = '/' + encodeURIComponent(options.entity);
var query = null;
var query = {};

if (options.generation) {
query = {
generation: options.generation
};
query.generation = options.generation;
}

this.makeReq_('DELETE', path, query, null, callback);
this.request({
method: 'DELETE',
uri: '/' + encodeURIComponent(options.entity),
qs: query
}, function(err, resp) {
callback(err, resp);
});
};

/**
Expand Down Expand Up @@ -346,9 +351,9 @@ Acl.prototype.delete = function(options, callback) {
* }, function(err, aclObject, apiResponse) {});
*/
Acl.prototype.get = function(options, callback) {
var that = this;
var self = this;
var path = '';
var query = null;
var query = {};

if (is.fn(options)) {
callback = options;
Expand All @@ -357,24 +362,25 @@ Acl.prototype.get = function(options, callback) {
path = '/' + encodeURIComponent(options.entity);

if (options.generation) {
query = {
generation: options.generation
};
query.generation = options.generation;
}
}

this.makeReq_('GET', path, query, null, function(err, resp) {
this.request({
uri: path,
qs: query,
}, function(err, resp) {
if (err) {
callback(err, null, resp);
return;
}

var results = resp;
var results;

if (resp.items) {
results = (resp.items || []).map(that.makeAclObject_);
results = arrify(resp.items).map(self.makeAclObject_);
} else {
results = that.makeAclObject_(results);
results = self.makeAclObject_(resp);
}

callback(null, results, resp);
Expand Down Expand Up @@ -420,27 +426,28 @@ Acl.prototype.get = function(options, callback) {
* }, function(err, aclObject, apiResponse) {});
*/
Acl.prototype.update = function(options, callback) {
var that = this;
var path = '/' + encodeURIComponent(options.entity);
var query = null;
var self = this;

var query = {};

if (options.generation) {
query = {
generation: options.generation
};
query.generation = options.generation;
}

var body = {
role: options.role.toUpperCase()
};

this.makeReq_('PUT', path, query, body, function(err, resp) {
this.request({
method: 'PUT',
uri: '/' + encodeURIComponent(options.entity),
qs: query,
json: {
role: options.role.toUpperCase()
}
}, function(err, resp) {
if (err) {
callback(err, null, resp);
return;
}

callback(null, that.makeAclObject_(resp), resp);
callback(null, self.makeAclObject_(resp), resp);
});
};

Expand Down Expand Up @@ -473,8 +480,9 @@ Acl.prototype.makeAclObject_ = function(accessControlObject) {
* @param {*} body - Request body contents.
* @param {function} callback - The callback function.
*/
Acl.prototype.makeReq_ = function(method, path, query, body, callback) {
this.makeReq(method, this.pathPrefix + path, query, body, callback);
Acl.prototype.request = function(reqOpts, callback) {
reqOpts.uri = this.pathPrefix + reqOpts.uri;
this.request_(reqOpts, callback);
};

module.exports = Acl;
Expand Down Expand Up @@ -522,7 +530,7 @@ AclRoleAccessorMethods.roles = [
];

AclRoleAccessorMethods.prototype._assignAccessMethods = function(role) {
var that = this;
var self = this;

var accessMethods = AclRoleAccessorMethods.accessMethods;
var entities = AclRoleAccessorMethods.entities;
Expand Down Expand Up @@ -553,7 +561,7 @@ AclRoleAccessorMethods.prototype._assignAccessMethods = function(role) {
callback = entityId;
}

that[accessMethod]({
self[accessMethod]({
entity: apiEntity,
role: role
}, callback);
Expand Down
Loading