Skip to content

Commit

Permalink
move makePublic() & makePrivate() outside of acl
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus committed Jan 22, 2015
1 parent 3a70ef7 commit fd35f27
Show file tree
Hide file tree
Showing 7 changed files with 262 additions and 105 deletions.
42 changes: 0 additions & 42 deletions lib/storage/acl.js
Original file line number Diff line number Diff line change
Expand Up @@ -417,48 +417,6 @@ Acl.prototype.update = function(options, callback) {
});
};

/**
* Make a {module:storage/bucket} or {module:storage/file} object private.
*
* This is a short-hand method which will remove ACL permissions for the
* "allUsers" scope.
*
* @param {function} callback - The callback function.
*
* @alias acl.makePrivate
*
* @example
* myBucket.acl.makePrivate(function(err) {});
* myFile.acl.makePrivate(function(err) {});
*/
Acl.prototype.makePrivate = function(callback) {
this.delete({
scope: 'allUsers'
}, callback);
};

/**
* Make a {module:storage/bucket} or {module:storage/file} object publicly
* readable.
*
* This is a short-hand method which will grant "reader" permissions to the
* "allUsers" scope.
*
* @param {function} callback - The callback function.
*
* @alias acl.makePublic
*
* @example
* myBucket.acl.makePublic(function(err, aclObject) {});
* myFile.acl.makePublic(function(err, aclObject) {});
*/
Acl.prototype.makePublic = function(callback) {
this.add({
scope: 'allUsers',
role: 'READER'
}, callback);
};

/**
* Transform API responses to a consistent object format.
*
Expand Down
67 changes: 54 additions & 13 deletions lib/storage/bucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,18 +172,6 @@ function Bucket(storage, name) {
*/
var aclDefaultUpdate = true;

/**
* Maps to {module:storage/bucket#acl.makePrivate}.
* @alias acl.default.makePrivate
*/
var aclDefaultMakePrivate = true;

/**
* Maps to {module:storage/bucket#acl.makePublic}.
* @alias acl.default.makePublic
*/
var aclDefaultMakePublic = true;

/**
* Maps to {module:storage/bucket#acl.owners}.
* @alias acl.default.owners
Expand Down Expand Up @@ -317,9 +305,52 @@ Bucket.prototype.getMetadata = function(callback) {
}.bind(this));
};

/**
* Restrict access to your bucket's contents to only project owners.
*
* @param {function=} callback - The callback function.
*
* @example
* bucket.makePrivate(function(err) {});
*/
Bucket.prototype.makePrivate = function(callback) {
callback = callback || util.noop;

this.setMetadata({
predefinedAcl: 'private'
}, function(err) {
callback(err || null);
});
};

/**
* Make the files in your bucket publicly readable.
*
* @param {function=} callback - The callback function.
*
* @example
* bucket.makePublic(function(err) {});
*/
Bucket.prototype.makePublic = function(callback) {
callback = callback || util.noop;

this.setMetadata({
predefinedAcl: 'publicRead'
}, function(err) {
callback(err || null);
});
};

/**
* Set the bucket's metadata.
*
* {module:storage/bucket#makePrivate} and {module:storage/bucket#makePublic)
* use this method to set the `predefinedAcl` metadata property. You may also
* wish to use another value, such as "authenticatedRead", "private",
* "projectPrivate", or "publicReadWrite". See
* https://cloud.google.com/storage/docs/accesscontrol#predefined-acl for a full
* list of supported values.
*
* @param {object} metadata - The metadata you wish to set.
* @param {function=} callback - The callback function.
*
Expand All @@ -334,7 +365,17 @@ Bucket.prototype.getMetadata = function(callback) {
*/
Bucket.prototype.setMetadata = function(metadata, callback) {
callback = callback || util.noop;
this.makeReq_('PATCH', '', null, metadata, function(err, resp) {
var query = null;

if (metadata.predefinedAcl) {
query = {
predefinedAcl: metadata.predefinedAcl
};
delete metadata.predefinedAcl;
metadata.acl = [];
}

this.makeReq_('PATCH', '', query, metadata, function(err, resp) {
if (err) {
callback(err);
return;
Expand Down
55 changes: 54 additions & 1 deletion lib/storage/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -698,9 +698,52 @@ File.prototype.getSignedUrl = function(options, callback) {
});
};

/**
* Restrict access to the file to only project owners.
*
* @param {function=} callback - The callback function.
*
* @example
* file.makePrivate(function(err) {});
*/
File.prototype.makePrivate = function(callback) {
callback = callback || util.noop;

this.setMetadata({
predefinedAcl: 'private'
}, function(err) {
callback(err || null);
});
};

/**
* Make the file publicly readable.
*
* @param {function=} callback - The callback function.
*
* @example
* file.makePublic(function(err) {});
*/
File.prototype.makePublic = function(callback) {
callback = callback || util.noop;

this.setMetadata({
predefinedAcl: 'publicRead'
}, function(err) {
callback(err || null);
});
};

/**
* Set the file's metadata.
*
* {module:storage/file#makePrivate} and {module:storage/file#makePublic) use
* this method to set the `predefinedAcl` metadata property. You may also wish
* to use another value, such as "authenticatedRead", "private",
* "projectPrivate", or "publicReadWrite". See
* https://cloud.google.com/storage/docs/accesscontrol#predefined-acl for a full
* list of supported values.
*
* @param {object} metadata - The metadata you wish to set.
* @param {function=} callback - The callback function.
*
Expand All @@ -716,7 +759,17 @@ File.prototype.getSignedUrl = function(options, callback) {
File.prototype.setMetadata = function(metadata, callback) {
callback = callback || util.noop;
var path = '/o/' + encodeURIComponent(this.name);
this.makeReq_('PATCH', path, null, metadata, function(err, resp) {
var query = null;

if (metadata.predefinedAcl) {
query = {
predefinedAcl: metadata.predefinedAcl
};
delete metadata.predefinedAcl;
metadata.acl = [];
}

this.makeReq_('PATCH', path, query, metadata, function(err, resp) {
if (err) {
callback(err);
return;
Expand Down
20 changes: 1 addition & 19 deletions regression/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ describe('storage', function() {

describe('buckets', function() {
it('should get access controls', function(done) {
bucket.acl.get(done, function(err, accessControls) {
bucket.acl.get(function(err, accessControls) {
assert.ifError(err);
assert(Array.isArray(accessControls));
done();
Expand Down Expand Up @@ -149,15 +149,6 @@ describe('storage', function() {
});
});

it('should make a bucket public and private', function(done) {
bucket.acl.makePublic(function(err, accessControl) {
assert.equal(accessControl.role, storage.acl.READER_ROLE);
assert.equal(accessControl.scope, 'allUsers');

bucket.acl.makePrivate(done);
});
});

it('should update an account', function(done) {
bucket.acl.add({
scope: USER_ACCOUNT,
Expand Down Expand Up @@ -243,15 +234,6 @@ describe('storage', function() {
});
});
});

it('should make a file public and private', function(done) {
file.acl.makePublic(function(err, accessControl) {
assert.equal(accessControl.role, storage.acl.READER_ROLE);
assert.equal(accessControl.scope, 'allUsers');

file.acl.makePrivate(done);
});
});
});
});

Expand Down
29 changes: 0 additions & 29 deletions test/storage/acl.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,35 +276,6 @@ describe('storage/acl', function() {
});
});

describe('makePrivate', function() {
it('should revoke read permission to allUsers entity', function(done) {
acl.delete = function(options, callback) {
assert.deepEqual(options, {
scope: 'allUsers'
});

callback();
};

acl.makePrivate(done);
});
});

describe('makePublic', function() {
it('should grant read permission to allUsers entity', function(done) {
acl.add = function(options, callback) {
assert.deepEqual(options, {
scope: 'allUsers',
role: Storage.acl.READER_ROLE
});

callback();
};

acl.makePublic(done);
});
});

describe('makeAclObject_', function() {
it('should return an ACL object from an API response', function() {
var projectTeam = {
Expand Down
76 changes: 76 additions & 0 deletions test/storage/bucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,70 @@ describe('Bucket', function() {
});
});

describe('makePrivate', function() {
it('should set predefinedAcl', function(done) {
bucket.setMetadata = function(metadata) {
assert.equal(metadata.predefinedAcl, 'private');
done();
};

bucket.makePrivate();
});

it('should execute callback', function(done) {
bucket.setMetadata = function(metadata, callback) {
callback();
};

bucket.makePrivate(done);
});

it('should execute callback with error', function(done) {
var error = new Error('Error.');

bucket.setMetadata = function(metadata, callback) {
callback(error);
};

bucket.makePrivate(function(err) {
assert.equal(err, error);
done();
});
});
});

describe('makePublic', function() {
it('should set predefinedAcl', function(done) {
bucket.setMetadata = function(metadata) {
assert.equal(metadata.predefinedAcl, 'publicRead');
done();
};

bucket.makePublic();
});

it('should execute callback', function(done) {
bucket.setMetadata = function(metadata, callback) {
callback();
};

bucket.makePublic(done);
});

it('should execute callback with error', function(done) {
var error = new Error('Error.');

bucket.setMetadata = function(metadata, callback) {
callback(error);
};

bucket.makePublic(function(err) {
assert.equal(err, error);
done();
});
});
});

describe('setMetadata', function() {
var metadata = { fake: 'metadata' };

Expand All @@ -258,6 +322,18 @@ describe('Bucket', function() {
bucket.setMetadata(metadata);
});

it('should send a query if predefinedAcl is specified', function(done) {
var predefinedAcl = 'publicRead';

bucket.makeReq_ = function(method, path, query, body) {
assert.equal(query.predefinedAcl, predefinedAcl);
assert.deepEqual(body, { acl: [] });
done();
};

bucket.setMetadata({ predefinedAcl: predefinedAcl });
});

it('should execute callback', function(done) {
bucket.makeReq_ = function(method, path, query, body, callback) {
callback();
Expand Down
Loading

0 comments on commit fd35f27

Please sign in to comment.