-
Notifications
You must be signed in to change notification settings - Fork 598
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d04c190
commit 9b85196
Showing
6 changed files
with
493 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,248 @@ | ||
/*! | ||
* Copyright 2014 Google Inc. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/*! | ||
* @module storage/acl | ||
*/ | ||
|
||
'use strict'; | ||
|
||
/** | ||
* @type module:common/util | ||
* @private | ||
*/ | ||
var util = require('../common/util.js'); | ||
|
||
/** | ||
* Google Cloud Storage uses access control lists (ACLs) to manage object and | ||
* bucket access. ACLs are the mechanism you use to share objects with other | ||
* users and allow other users to access your buckets and objects. | ||
* | ||
* An ACL consists of one or more entries, where each entry grants permissions | ||
* to a scope. Permissions define the actions that can be performed against an | ||
* object or bucket (for example, `READ` or `WRITE`); the scope defines who the | ||
* permission applies to (for example, a specific user or group of users). | ||
* | ||
* For more detailed information, see | ||
* [About Access Control Lists](http://goo.gl/6qBBPO). | ||
* | ||
* @constructor | ||
* @alias module:storage/acl | ||
*/ | ||
function Acl(module) { | ||
this.assignAccessMethods_(this, '/acl'); | ||
|
||
if (module.constructor.name === 'Bucket') { | ||
this.default = {}; | ||
this.assignAccessMethods_(this.default, '/defaultObjectAcl'); | ||
|
||
this.bucket = module; | ||
} else { | ||
this.bucket = module.bucket; | ||
this.file = module; | ||
} | ||
} | ||
|
||
/** | ||
* Add access controls on a {module:storage/bucket} or {module:storage/file}. | ||
* | ||
* @alias acl.add | ||
* | ||
* @param {string} scope - Whose permissions will be updated. | ||
* @param {string} role - Permissions allowed for the defined scope. | ||
* @param {function} callback - The callback function. | ||
* | ||
* @example | ||
* var scope = '[email protected]'; | ||
* var role = 'owner'; | ||
* | ||
* myBucket.acl.add(scope, role, function(err, aclObject) {}); | ||
*/ | ||
Acl.prototype.add_ = function(path, scope, role, callback) { | ||
var that = this; | ||
|
||
var body = { | ||
entity: scope, | ||
role: role.toUpperCase() | ||
}; | ||
|
||
this.makeReq_('POST', path, null, body, function(err, resp) { | ||
if (err) { | ||
callback(err); | ||
return; | ||
} | ||
|
||
callback(null, that.makeAclObject_(resp)); | ||
}); | ||
}; | ||
|
||
/** | ||
* Delete access controls on a {module:storage/bucket} or {module:storage/file}. | ||
* | ||
* @alias acl.delete | ||
* | ||
* @param {string} scope - Whose permissions will be revoked. | ||
* @param {function} callback - The callback function. | ||
* | ||
* @example | ||
* var scope = '[email protected]'; | ||
* | ||
* myBucket.acl.delete(scope, function(err) {}); | ||
*/ | ||
Acl.prototype.delete_ = function(path, scope, callback) { | ||
path += '/' + encodeURIComponent(scope); | ||
this.makeReq_('DELETE', path, null, null, callback); | ||
}; | ||
|
||
/** | ||
* Get access controls on a {module:storage/bucket} or {module:storage/file}. If | ||
* an scope is omitted, you will receive an array of all applicable access | ||
* controls. | ||
* | ||
* @alias acl.get | ||
* | ||
* @param {string=} scope - Whose permissions will be fetched. | ||
* @param {function} callback - The callback function. | ||
* | ||
* @example | ||
* var scope = '[email protected]'; | ||
* | ||
* myBucket.acl.get(scope, function(err, aclObject) {}); | ||
* | ||
* //- | ||
* // Get all access controls. | ||
* //- | ||
* myBucket.acl.get(function(err, aclObjects) { | ||
* // aclObjects = [ | ||
* // { | ||
* // scope: '[email protected]', | ||
* // role: 'owner' | ||
* // } | ||
* // ] | ||
* }); | ||
*/ | ||
Acl.prototype.get_ = function(path, scope, callback) { | ||
var that = this; | ||
|
||
if (util.is(scope, 'string')) { | ||
path += '/' + encodeURIComponent(scope); | ||
} else if (util.is(scope, 'function')) { | ||
callback = scope; | ||
} | ||
|
||
this.makeReq_('GET', path, null, null, function(err, resp) { | ||
if (err) { | ||
callback(err); | ||
return; | ||
} | ||
|
||
var results = resp; | ||
|
||
if (resp.items) { | ||
results = (resp.items || []).map(that.makeAclObject_); | ||
} else { | ||
results = that.makeAclObject_(results); | ||
} | ||
|
||
callback(null, results); | ||
}); | ||
}; | ||
|
||
/** | ||
* Update access controls on a {module:storage/bucket} or {module:storage/file}. | ||
* | ||
* @alias acl.update | ||
* | ||
* @param {string} scope - Whose permissions will be updated. | ||
* @param {string} role - Permissions allowed for the defined scope. | ||
* @param {function} callback - The callback function. | ||
* | ||
* @example | ||
* var scope = '[email protected]'; | ||
* var role = 'writer'; | ||
* | ||
* myBucket.acl.update(scope, role, function(err) {}); | ||
*/ | ||
Acl.prototype.update_ = function(path, scope, role, callback) { | ||
var that = this; | ||
|
||
path += '/' + encodeURIComponent(scope); | ||
|
||
var body = { | ||
role: role.toUpperCase() | ||
}; | ||
|
||
this.makeReq_('PUT', path, null, body, function(err, resp) { | ||
if (err) { | ||
callback(err); | ||
return; | ||
} | ||
|
||
callback(null, that.makeAclObject_(resp)); | ||
}); | ||
}; | ||
|
||
/** | ||
* Iterate over the access methods, and apply them to the specified object. | ||
* | ||
* @private | ||
*/ | ||
Acl.prototype.assignAccessMethods_ = function(destObj, path) { | ||
var that = this; | ||
|
||
['add', 'delete', 'get', 'update'].forEach(function(method) { | ||
destObj[method] = that[method + '_'].bind(that, path); | ||
}); | ||
}; | ||
|
||
/** | ||
* Transform API responses to a consistent object format. | ||
* | ||
* @private | ||
*/ | ||
Acl.prototype.makeAclObject_ = function(accessControlObject) { | ||
var obj = { | ||
scope: accessControlObject.scope, | ||
role: accessControlObject.role.toLowerCase() | ||
}; | ||
|
||
if (accessControlObject.projectTeam) { | ||
obj.projectTeam = accessControlObject.projectTeam; | ||
} | ||
|
||
return obj; | ||
}; | ||
|
||
/** | ||
* Patch requests up to the bucket's request object. | ||
* | ||
* @private | ||
* | ||
* @param {string} method - Action. | ||
* @param {string} path - Request path. | ||
* @param {*} query - Request query object. | ||
* @param {*} body - Request body contents. | ||
* @param {function} callback - The callback function. | ||
*/ | ||
Acl.prototype.makeReq_ = function(method, path, query, body, callback) { | ||
if (this.file) { | ||
path = '/o/' + encodeURIComponent(this.file.name) + path; | ||
} | ||
|
||
this.bucket.makeReq_(method, path, query, body, callback); | ||
}; | ||
|
||
module.exports = Acl; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.