class Google::Cloud::Storage::Bucket::Acl

# Bucket Access Control List

Represents a Bucket's Access Control List.

@example

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my-bucket"

bucket.acl.readers.each { |reader| puts reader }

Constants

RULES

@private

Attributes

user_project[RW]

A boolean value or a project ID string to indicate the project to be billed for operations on the bucket and its files. If this attribute is set to `true`, transit costs for operations on the bucket will be billed to the current project for this client. (See {Project#project} for the ID of the current project.) If this attribute is set to a project ID, and that project is authorized for the currently authenticated service account, transit costs will be billed to that project. This attribute is required with requester pays-enabled buckets. The default is `nil`.

In general, this attribute should be set when first retrieving the owning bucket by providing the `user_project` option to {Project#bucket}.

See also {Bucket#requester_pays=} and {Bucket#requester_pays}.

Public Class Methods

new(bucket) click to toggle source

@private Initialized a new Acl object. Must provide a valid Bucket object.

# File lib/google/cloud/storage/bucket/acl.rb, line 73
def initialize bucket
  @bucket = bucket.name
  @service = bucket.service
  @user_project = bucket.user_project
  @owners  = nil
  @writers = nil
  @readers = nil
end
predefined_rule_for(rule_name) click to toggle source

@private

# File lib/google/cloud/storage/bucket/acl.rb, line 333
def self.predefined_rule_for rule_name
  RULES[rule_name.to_s]
end

Public Instance Methods

add_owner(entity) click to toggle source

Grants owner permission to the bucket.

@param [String] entity The entity holding the permission, in one of

the following forms:

* user-userId
* user-email
* group-groupId
* group-email
* domain-domain
* project-team-projectId
* allUsers
* allAuthenticatedUsers

@return [String] The entity.

@example Grant access to a user by prepending `“user-”` to an email:

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my-bucket"

email = "heidi@example.net"
bucket.acl.add_owner "user-#{email}"

@example Grant access to a group by prepending `“group-”` to email:

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my-bucket"

email = "authors@example.net"
bucket.acl.add_owner "group-#{email}"
# File lib/google/cloud/storage/bucket/acl.rb, line 197
def add_owner entity
  gapi = @service.insert_bucket_acl @bucket, entity, "OWNER",
                                    user_project: user_project
  entity = gapi.entity
  @owners&.push entity
  entity
end
add_reader(entity) click to toggle source

Grants reader permission to the bucket.

@param [String] entity The entity holding the permission, in one of

the following forms:

* user-userId
* user-email
* group-groupId
* group-email
* domain-domain
* project-team-projectId
* allUsers
* allAuthenticatedUsers

@return [String] The entity.

@example Grant access to a user by prepending `“user-”` to an email:

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my-bucket"

email = "heidi@example.net"
bucket.acl.add_reader "user-#{email}"

@example Grant access to a group by prepending `“group-”` to email:

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my-bucket"

email = "authors@example.net"
bucket.acl.add_reader "group-#{email}"
# File lib/google/cloud/storage/bucket/acl.rb, line 287
def add_reader entity
  gapi = @service.insert_bucket_acl @bucket, entity, "READER",
                                    user_project: user_project
  entity = gapi.entity
  @readers&.push entity
  entity
end
add_writer(entity) click to toggle source

Grants writer permission to the bucket.

@param [String] entity The entity holding the permission, in one of

the following forms:

* user-userId
* user-email
* group-groupId
* group-email
* domain-domain
* project-team-projectId
* allUsers
* allAuthenticatedUsers

@return [String] The entity.

@example Grant access to a user by prepending `“user-”` to an email:

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my-bucket"

email = "heidi@example.net"
bucket.acl.add_writer "user-#{email}"

@example Grant access to a group by prepending `“group-”` to email:

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my-bucket"

email = "authors@example.net"
bucket.acl.add_writer "group-#{email}"
# File lib/google/cloud/storage/bucket/acl.rb, line 242
def add_writer entity
  gapi = @service.insert_bucket_acl @bucket, entity, "WRITER",
                                    user_project: user_project
  entity = gapi.entity
  @writers&.push entity
  entity
end
auth!() click to toggle source

Convenience method to apply the `authenticatedRead` predefined ACL rule to the bucket.

@example

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my-bucket"

bucket.acl.auth!
# File lib/google/cloud/storage/bucket/acl.rb, line 352
def auth!
  update_predefined_acl! "authenticatedRead"
end
auth_read!()
Alias for: auth!
authenticated!()
Alias for: auth!
authenticatedRead!()
Alias for: auth!
authenticated_read!()
Alias for: auth!
delete(entity) click to toggle source

Permanently deletes the entity from the bucket's access control list.

@param [String] entity The entity holding the permission, in one of

the following forms:

* user-userId
* user-email
* group-groupId
* group-email
* domain-domain
* project-team-projectId
* allUsers
* allAuthenticatedUsers

@return [Boolean]

@example

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my-bucket"

email = "heidi@example.net"
bucket.acl.delete "user-#{email}"
# File lib/google/cloud/storage/bucket/acl.rb, line 323
def delete entity
  @service.delete_bucket_acl @bucket, entity,
                             user_project: user_project
  @owners&.delete entity
  @writers&.delete entity
  @readers&.delete entity
  true
end
owners() click to toggle source

Lists the owners of the bucket.

@return [Array<String>]

@example

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my-bucket"

bucket.acl.owners.each { |owner| puts owner }
# File lib/google/cloud/storage/bucket/acl.rb, line 117
def owners
  reload! if @owners.nil?
  @owners
end
private!() click to toggle source

Convenience method to apply the `private` predefined ACL rule to the bucket.

@example

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my-bucket"

bucket.acl.private!
# File lib/google/cloud/storage/bucket/acl.rb, line 373
def private!
  update_predefined_acl! "private"
end
projectPrivate!()
Alias for: project_private!
project_private!() click to toggle source

Convenience method to apply the `projectPrivate` predefined ACL rule to the bucket.

@example

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my-bucket"

bucket.acl.project_private!
# File lib/google/cloud/storage/bucket/acl.rb, line 390
def project_private!
  update_predefined_acl! "projectPrivate"
end
Also aliased as: projectPrivate!
public!() click to toggle source

Convenience method to apply the `publicRead` predefined ACL rule to the bucket.

@example

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my-bucket"

bucket.acl.public!
# File lib/google/cloud/storage/bucket/acl.rb, line 408
def public!
  update_predefined_acl! "publicRead"
end
Also aliased as: publicRead!, public_read!
publicRead!()
Alias for: public!
publicReadWrite!()
Alias for: public_write!
public_read!()
Alias for: public!
public_write!() click to toggle source

Convenience method to apply the `publicReadWrite` predefined ACL rule to the bucket.

@example

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my-bucket"

bucket.acl.public_write!
# File lib/google/cloud/storage/bucket/acl.rb, line 426
def public_write!
  update_predefined_acl! "publicReadWrite"
end
Also aliased as: publicReadWrite!
readers() click to toggle source

Lists the readers of the bucket.

@return [Array<String>]

@example

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my-bucket"

bucket.acl.readers.each { |reader| puts reader }
# File lib/google/cloud/storage/bucket/acl.rb, line 155
def readers
  reload! if @readers.nil?
  @readers
end
refresh!()
Alias for: reload!
reload!() click to toggle source

Reloads all Access Control List data for the bucket.

@example

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my-bucket"

bucket.acl.reload!
# File lib/google/cloud/storage/bucket/acl.rb, line 94
def reload!
  gapi = @service.list_bucket_acls @bucket, user_project: user_project
  acls = Array(gapi.items)
  @owners  = entities_from_acls acls, "OWNER"
  @writers = entities_from_acls acls, "WRITER"
  @readers = entities_from_acls acls, "READER"
end
Also aliased as: refresh!
writers() click to toggle source

Lists the owners of the bucket.

@return [Array<String>]

@example

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my-bucket"

bucket.acl.writers.each { |writer| puts writer }
# File lib/google/cloud/storage/bucket/acl.rb, line 136
def writers
  reload! if @writers.nil?
  @writers
end

Protected Instance Methods

clear!() click to toggle source
# File lib/google/cloud/storage/bucket/acl.rb, line 433
def clear!
  @owners  = nil
  @writers = nil
  @readers = nil
  self
end
entities_from_acls(acls, role) click to toggle source
# File lib/google/cloud/storage/bucket/acl.rb, line 446
def entities_from_acls acls, role
  selected = acls.select { |acl| acl.role == role }
  selected.map(&:entity)
end
update_predefined_acl!(acl_role) click to toggle source
# File lib/google/cloud/storage/bucket/acl.rb, line 440
def update_predefined_acl! acl_role
  @service.patch_bucket @bucket, predefined_acl: acl_role,
                                 user_project: user_project
  clear!
end