Class: Roda::RodaPlugins::PermissionsPolicy::Policy

Inherits:
Object
  • Object
show all
Defined in:
lib/roda/plugins/permissions_policy.rb

Overview

Represents a permissions policy.

Instance Method Summary collapse

Constructor Details

#initializePolicy

Returns a new instance of Policy.



176
177
178
# File 'lib/roda/plugins/permissions_policy.rb', line 176

def initialize
  clear
end

Instance Method Details

#clearObject

Clear all settings, useful to remove any inherited settings.



181
182
183
# File 'lib/roda/plugins/permissions_policy.rb', line 181

def clear
  @opts = {}
end

#freezeObject

Do not allow future modifications to any settings.



186
187
188
189
190
# File 'lib/roda/plugins/permissions_policy.rb', line 186

def freeze
  @opts.freeze
  header_value.freeze
  super
end

#header_keyObject

The header name to use, depends on whether report only mode has been enabled.



193
194
195
# File 'lib/roda/plugins/permissions_policy.rb', line 193

def header_key
  @report_only ? RodaResponseHeaders::PERMISSIONS_POLICY_REPORT_ONLY : RodaResponseHeaders::PERMISSIONS_POLICY
end

#header_valueObject

The header value to use.



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/roda/plugins/permissions_policy.rb', line 198

def header_value
  return @header_value if @header_value

  s = String.new
  @opts.each do |k, vs|
    s << k << "="

    if vs == :all
      s << '*, '
    else
      s << '('
      vs.each{|v| append_formatted_value(s, v)}
      s.chop! unless vs.empty?
      s << '), '
    end
  end
  s.chop!
  s.chop!
  @header_value = s
end

#report_only(report = true) ⇒ Object

Set whether the Permissions-Policy-Report-Only header instead of the default Permissions-Policy header.



221
222
223
# File 'lib/roda/plugins/permissions_policy.rb', line 221

def report_only(report=true)
  @report_only = report
end

#report_only?Boolean

Whether this policy uses report only mode.

Returns:

  • (Boolean)


226
227
228
# File 'lib/roda/plugins/permissions_policy.rb', line 226

def report_only?
  !!@report_only
end

#set_header(headers) ⇒ Object

Set the current policy in the headers hash. If no settings have been made in the policy, does not set a header.



232
233
234
235
# File 'lib/roda/plugins/permissions_policy.rb', line 232

def set_header(headers)
  return if @opts.empty?
  headers[header_key] ||= header_value
end