Class: RsUserPolicy::Policy::JsonPolicy

Inherits:
Object
  • Object
show all
Includes:
Policy
Defined in:
lib/rs_user_policy/policy/json_policy.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ JsonPolicy

Initializes a new Policy

If more than one source is passed into options, the order of preference will be

:json, :json_str, :filename

Parameters:

  • options (Hash) (defaults to: {})

    A hash of inputs for the new JSONPolicy

Options Hash (options):

  • :json (Hash)

    A hash containing the policy

  • :json_str (String)

    A JSON string containing the policy

  • :filename (String)

    Path and filename to a file containing the policy in JSON

Raises:

  • (ArgumentError)

    If neither a filename or json object were supplied

  • (Errno::ENOENT)

    If :filename was specified but the policy file does not exist

  • (JSON::ParseError)

    If the policy is not valid JSON



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rs_user_policy/policy/json_policy.rb', line 43

def initialize(options={})
  if ([:filename, :json, :json_str] & options.keys()).empty?
    raise ArgumentError, "You must supply either a filename, JSON string, or a JSON object"
  end

  if options.has_key?(:json)
    @policy = options[:json]
  elsif options.has_key?(:json_str)
    @policy = JSON.parse(options[:json_str])
  else
    @policy = JSON.parse(File.read(options[:filename]))
  end

  validate()
end

Instance Method Details

#get_permissions(roles, account_href) ⇒ Array<String>

Returns an array of permissions for a particular role in a particular RightScale account

Parameters:

  • roles (Array<String>)

    An array of role names for which permissions should be fetched

  • account_href (String)

    A RightScale API 1.5 href for the RightScale account

Returns:

  • (Array<String>)

    A list of permissions for the role and account pair requested. An empty array is returned if no policy exists for the requested pair



65
66
67
68
69
70
71
72
73
# File 'lib/rs_user_policy/policy/json_policy.rb', line 65

def get_permissions(roles, )
  permissions = []
  roles.each do |role|
    if @policy.has_key?(role)
      permissions = permissions + (@policy[role][] || @policy[role]['default'] || [])
    end
  end
  permissions.uniq
end