Skip to content

Commit fd35f27

Browse files
move makePublic() & makePrivate() outside of acl
1 parent 3a70ef7 commit fd35f27

File tree

7 files changed

+262
-105
lines changed

7 files changed

+262
-105
lines changed

lib/storage/acl.js

-42
Original file line numberDiff line numberDiff line change
@@ -417,48 +417,6 @@ Acl.prototype.update = function(options, callback) {
417417
});
418418
};
419419

420-
/**
421-
* Make a {module:storage/bucket} or {module:storage/file} object private.
422-
*
423-
* This is a short-hand method which will remove ACL permissions for the
424-
* "allUsers" scope.
425-
*
426-
* @param {function} callback - The callback function.
427-
*
428-
* @alias acl.makePrivate
429-
*
430-
* @example
431-
* myBucket.acl.makePrivate(function(err) {});
432-
* myFile.acl.makePrivate(function(err) {});
433-
*/
434-
Acl.prototype.makePrivate = function(callback) {
435-
this.delete({
436-
scope: 'allUsers'
437-
}, callback);
438-
};
439-
440-
/**
441-
* Make a {module:storage/bucket} or {module:storage/file} object publicly
442-
* readable.
443-
*
444-
* This is a short-hand method which will grant "reader" permissions to the
445-
* "allUsers" scope.
446-
*
447-
* @param {function} callback - The callback function.
448-
*
449-
* @alias acl.makePublic
450-
*
451-
* @example
452-
* myBucket.acl.makePublic(function(err, aclObject) {});
453-
* myFile.acl.makePublic(function(err, aclObject) {});
454-
*/
455-
Acl.prototype.makePublic = function(callback) {
456-
this.add({
457-
scope: 'allUsers',
458-
role: 'READER'
459-
}, callback);
460-
};
461-
462420
/**
463421
* Transform API responses to a consistent object format.
464422
*

lib/storage/bucket.js

+54-13
Original file line numberDiff line numberDiff line change
@@ -172,18 +172,6 @@ function Bucket(storage, name) {
172172
*/
173173
var aclDefaultUpdate = true;
174174

175-
/**
176-
* Maps to {module:storage/bucket#acl.makePrivate}.
177-
* @alias acl.default.makePrivate
178-
*/
179-
var aclDefaultMakePrivate = true;
180-
181-
/**
182-
* Maps to {module:storage/bucket#acl.makePublic}.
183-
* @alias acl.default.makePublic
184-
*/
185-
var aclDefaultMakePublic = true;
186-
187175
/**
188176
* Maps to {module:storage/bucket#acl.owners}.
189177
* @alias acl.default.owners
@@ -317,9 +305,52 @@ Bucket.prototype.getMetadata = function(callback) {
317305
}.bind(this));
318306
};
319307

308+
/**
309+
* Restrict access to your bucket's contents to only project owners.
310+
*
311+
* @param {function=} callback - The callback function.
312+
*
313+
* @example
314+
* bucket.makePrivate(function(err) {});
315+
*/
316+
Bucket.prototype.makePrivate = function(callback) {
317+
callback = callback || util.noop;
318+
319+
this.setMetadata({
320+
predefinedAcl: 'private'
321+
}, function(err) {
322+
callback(err || null);
323+
});
324+
};
325+
326+
/**
327+
* Make the files in your bucket publicly readable.
328+
*
329+
* @param {function=} callback - The callback function.
330+
*
331+
* @example
332+
* bucket.makePublic(function(err) {});
333+
*/
334+
Bucket.prototype.makePublic = function(callback) {
335+
callback = callback || util.noop;
336+
337+
this.setMetadata({
338+
predefinedAcl: 'publicRead'
339+
}, function(err) {
340+
callback(err || null);
341+
});
342+
};
343+
320344
/**
321345
* Set the bucket's metadata.
322346
*
347+
* {module:storage/bucket#makePrivate} and {module:storage/bucket#makePublic)
348+
* use this method to set the `predefinedAcl` metadata property. You may also
349+
* wish to use another value, such as "authenticatedRead", "private",
350+
* "projectPrivate", or "publicReadWrite". See
351+
* https://cloud.google.com/storage/docs/accesscontrol#predefined-acl for a full
352+
* list of supported values.
353+
*
323354
* @param {object} metadata - The metadata you wish to set.
324355
* @param {function=} callback - The callback function.
325356
*
@@ -334,7 +365,17 @@ Bucket.prototype.getMetadata = function(callback) {
334365
*/
335366
Bucket.prototype.setMetadata = function(metadata, callback) {
336367
callback = callback || util.noop;
337-
this.makeReq_('PATCH', '', null, metadata, function(err, resp) {
368+
var query = null;
369+
370+
if (metadata.predefinedAcl) {
371+
query = {
372+
predefinedAcl: metadata.predefinedAcl
373+
};
374+
delete metadata.predefinedAcl;
375+
metadata.acl = [];
376+
}
377+
378+
this.makeReq_('PATCH', '', query, metadata, function(err, resp) {
338379
if (err) {
339380
callback(err);
340381
return;

lib/storage/file.js

+54-1
Original file line numberDiff line numberDiff line change
@@ -698,9 +698,52 @@ File.prototype.getSignedUrl = function(options, callback) {
698698
});
699699
};
700700

701+
/**
702+
* Restrict access to the file to only project owners.
703+
*
704+
* @param {function=} callback - The callback function.
705+
*
706+
* @example
707+
* file.makePrivate(function(err) {});
708+
*/
709+
File.prototype.makePrivate = function(callback) {
710+
callback = callback || util.noop;
711+
712+
this.setMetadata({
713+
predefinedAcl: 'private'
714+
}, function(err) {
715+
callback(err || null);
716+
});
717+
};
718+
719+
/**
720+
* Make the file publicly readable.
721+
*
722+
* @param {function=} callback - The callback function.
723+
*
724+
* @example
725+
* file.makePublic(function(err) {});
726+
*/
727+
File.prototype.makePublic = function(callback) {
728+
callback = callback || util.noop;
729+
730+
this.setMetadata({
731+
predefinedAcl: 'publicRead'
732+
}, function(err) {
733+
callback(err || null);
734+
});
735+
};
736+
701737
/**
702738
* Set the file's metadata.
703739
*
740+
* {module:storage/file#makePrivate} and {module:storage/file#makePublic) use
741+
* this method to set the `predefinedAcl` metadata property. You may also wish
742+
* to use another value, such as "authenticatedRead", "private",
743+
* "projectPrivate", or "publicReadWrite". See
744+
* https://cloud.google.com/storage/docs/accesscontrol#predefined-acl for a full
745+
* list of supported values.
746+
*
704747
* @param {object} metadata - The metadata you wish to set.
705748
* @param {function=} callback - The callback function.
706749
*
@@ -716,7 +759,17 @@ File.prototype.getSignedUrl = function(options, callback) {
716759
File.prototype.setMetadata = function(metadata, callback) {
717760
callback = callback || util.noop;
718761
var path = '/o/' + encodeURIComponent(this.name);
719-
this.makeReq_('PATCH', path, null, metadata, function(err, resp) {
762+
var query = null;
763+
764+
if (metadata.predefinedAcl) {
765+
query = {
766+
predefinedAcl: metadata.predefinedAcl
767+
};
768+
delete metadata.predefinedAcl;
769+
metadata.acl = [];
770+
}
771+
772+
this.makeReq_('PATCH', path, query, metadata, function(err, resp) {
720773
if (err) {
721774
callback(err);
722775
return;

regression/storage.js

+1-19
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ describe('storage', function() {
9090

9191
describe('buckets', function() {
9292
it('should get access controls', function(done) {
93-
bucket.acl.get(done, function(err, accessControls) {
93+
bucket.acl.get(function(err, accessControls) {
9494
assert.ifError(err);
9595
assert(Array.isArray(accessControls));
9696
done();
@@ -149,15 +149,6 @@ describe('storage', function() {
149149
});
150150
});
151151

152-
it('should make a bucket public and private', function(done) {
153-
bucket.acl.makePublic(function(err, accessControl) {
154-
assert.equal(accessControl.role, storage.acl.READER_ROLE);
155-
assert.equal(accessControl.scope, 'allUsers');
156-
157-
bucket.acl.makePrivate(done);
158-
});
159-
});
160-
161152
it('should update an account', function(done) {
162153
bucket.acl.add({
163154
scope: USER_ACCOUNT,
@@ -243,15 +234,6 @@ describe('storage', function() {
243234
});
244235
});
245236
});
246-
247-
it('should make a file public and private', function(done) {
248-
file.acl.makePublic(function(err, accessControl) {
249-
assert.equal(accessControl.role, storage.acl.READER_ROLE);
250-
assert.equal(accessControl.scope, 'allUsers');
251-
252-
file.acl.makePrivate(done);
253-
});
254-
});
255237
});
256238
});
257239

test/storage/acl.js

-29
Original file line numberDiff line numberDiff line change
@@ -276,35 +276,6 @@ describe('storage/acl', function() {
276276
});
277277
});
278278

279-
describe('makePrivate', function() {
280-
it('should revoke read permission to allUsers entity', function(done) {
281-
acl.delete = function(options, callback) {
282-
assert.deepEqual(options, {
283-
scope: 'allUsers'
284-
});
285-
286-
callback();
287-
};
288-
289-
acl.makePrivate(done);
290-
});
291-
});
292-
293-
describe('makePublic', function() {
294-
it('should grant read permission to allUsers entity', function(done) {
295-
acl.add = function(options, callback) {
296-
assert.deepEqual(options, {
297-
scope: 'allUsers',
298-
role: Storage.acl.READER_ROLE
299-
});
300-
301-
callback();
302-
};
303-
304-
acl.makePublic(done);
305-
});
306-
});
307-
308279
describe('makeAclObject_', function() {
309280
it('should return an ACL object from an API response', function() {
310281
var projectTeam = {

test/storage/bucket.js

+76
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,70 @@ describe('Bucket', function() {
245245
});
246246
});
247247

248+
describe('makePrivate', function() {
249+
it('should set predefinedAcl', function(done) {
250+
bucket.setMetadata = function(metadata) {
251+
assert.equal(metadata.predefinedAcl, 'private');
252+
done();
253+
};
254+
255+
bucket.makePrivate();
256+
});
257+
258+
it('should execute callback', function(done) {
259+
bucket.setMetadata = function(metadata, callback) {
260+
callback();
261+
};
262+
263+
bucket.makePrivate(done);
264+
});
265+
266+
it('should execute callback with error', function(done) {
267+
var error = new Error('Error.');
268+
269+
bucket.setMetadata = function(metadata, callback) {
270+
callback(error);
271+
};
272+
273+
bucket.makePrivate(function(err) {
274+
assert.equal(err, error);
275+
done();
276+
});
277+
});
278+
});
279+
280+
describe('makePublic', function() {
281+
it('should set predefinedAcl', function(done) {
282+
bucket.setMetadata = function(metadata) {
283+
assert.equal(metadata.predefinedAcl, 'publicRead');
284+
done();
285+
};
286+
287+
bucket.makePublic();
288+
});
289+
290+
it('should execute callback', function(done) {
291+
bucket.setMetadata = function(metadata, callback) {
292+
callback();
293+
};
294+
295+
bucket.makePublic(done);
296+
});
297+
298+
it('should execute callback with error', function(done) {
299+
var error = new Error('Error.');
300+
301+
bucket.setMetadata = function(metadata, callback) {
302+
callback(error);
303+
};
304+
305+
bucket.makePublic(function(err) {
306+
assert.equal(err, error);
307+
done();
308+
});
309+
});
310+
});
311+
248312
describe('setMetadata', function() {
249313
var metadata = { fake: 'metadata' };
250314

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

325+
it('should send a query if predefinedAcl is specified', function(done) {
326+
var predefinedAcl = 'publicRead';
327+
328+
bucket.makeReq_ = function(method, path, query, body) {
329+
assert.equal(query.predefinedAcl, predefinedAcl);
330+
assert.deepEqual(body, { acl: [] });
331+
done();
332+
};
333+
334+
bucket.setMetadata({ predefinedAcl: predefinedAcl });
335+
});
336+
261337
it('should execute callback', function(done) {
262338
bucket.makeReq_ = function(method, path, query, body, callback) {
263339
callback();

0 commit comments

Comments
 (0)