Class: S33r::S3ACL::Grant

Inherits:
Object
  • Object
show all
Defined in:
lib/s33r/s3_acl.rb,
lib/s33r/s3_logging.rb

Overview

Representation of an S3 Grant (see docs.amazonwebservices.com/AmazonS3/2006-03-01/UsingGrantees.html).

A Grant consists of a Grantee and a permission they are to be assigned.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(grantee, permission) ⇒ Grant

permission: one of the keys in the PERMISSIONS hash or a raw permission string

Raises:

  • (InvalidPermission)


133
134
135
136
137
138
139
140
141
142
# File 'lib/s33r/s3_acl.rb', line 133

def initialize(grantee, permission)
  @grantee = grantee
  if permission.is_a? String
    @permission = permission
  else
    @permission = PERMISSIONS[permission]
  end
  raise InvalidPermission, \
  "Permission #{permission.to_s} is not a valid permission specifier" if @permission.nil?
end

Instance Attribute Details

#granteeObject

Returns the value of attribute grantee.



130
131
132
# File 'lib/s33r/s3_acl.rb', line 130

def grantee
  @grantee
end

#permissionObject

Returns the value of attribute permission.



130
131
132
# File 'lib/s33r/s3_acl.rb', line 130

def permission
  @permission
end

Class Method Details

.for_amazon_customer(email_address, permission) ⇒ Object

Note that setting a grant for an Amazon customer is the same as setting a grant for the CanonicalUser who owns the specified email address. So when you get the ACL back, it will actually contain a CanonicalUser grant.



148
149
150
# File 'lib/s33r/s3_acl.rb', line 148

def Grant.for_amazon_customer(email_address,  permission)
  Grant.new(AmazonCustomer.new(email_address), permission) 
end

.for_canonical_user(id, display_name, permission) ⇒ Object



152
153
154
# File 'lib/s33r/s3_acl.rb', line 152

def Grant.for_canonical_user(id, display_name, permission)
  Grant.new(CanonicalUser.new(id, display_name), permission)
end

.for_group(group_type, permission) ⇒ Object



156
157
158
# File 'lib/s33r/s3_acl.rb', line 156

def Grant.for_group(group_type, permission)
  Grant.new(Group.new(group_type), permission)
end

.log_target_grantsObject

Generator for a grant which gives the LogDelivery group write and read_acl permissions on a bucket.

Returns an array with the two required Grant instances.



106
107
108
109
# File 'lib/s33r/s3_logging.rb', line 106

def Grant.log_target_grants
  log_delivery_group = Group.new(:log_delivery)
  [Grant.new(log_delivery_group, :read_acl), Grant.new(log_delivery_group, :write)]
end

.public_read_grantObject

Generator for a Grant which gives READ permissions to the AllUsers group type.



162
163
164
# File 'lib/s33r/s3_acl.rb', line 162

def Grant.public_read_grant
  Grant.new(Group.new(:all_users), :read)
end

Instance Method Details

#==(obj) ⇒ Object



190
191
192
193
194
195
196
197
198
# File 'lib/s33r/s3_acl.rb', line 190

def ==(obj)
  if !obj.is_a?(Grant)
    return false
  end
  if obj.permission != self.permission or obj.grantee != self.grantee
    return false
  end
  return true
end

#to_sObject



200
201
202
# File 'lib/s33r/s3_acl.rb', line 200

def to_s
  "#{@grantee.to_s} has permission #{@permission}"
end

#to_xmlObject

Convert a Grant object into an XML fragment.



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/s33r/s3_acl.rb', line 167

def to_xml
  xml_str = ""
  xml = S33r::OrderlyXmlMarkup.new(:target => xml_str, :indent => 0)
  
  # <Grant> element.
  xml.Grant {
    xml.Grantee({"xmlns:#{NAMESPACE}" => NAMESPACE_URI, "xsi:type" => @grantee.grantee_type}) {
      case @grantee.grantee_type
        when GRANTEE_TYPES[:amazon_customer]
          xml.EmailAddress @grantee.email_address
        when GRANTEE_TYPES[:canonical_user]
          xml.ID @grantee.user_id
          xml.DisplayName @grantee.display_name
        when GRANTEE_TYPES[:group]
          xml.URI GROUP_ACL_URI_BASE + @grantee.group_type
      end
    }
    xml.Permission @permission
  }
  
  xml_str
end