class RsUserPolicy::UserAssignments::JsonUserAssignments

Public Class Methods

new(options={}) click to toggle source

Initializes a new UserAssignments

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

:json, :json_str, :filename

@param [Hash] options A hash of inputs for the new JsonUserAssignments, where the keys are; @option options [Hash] :json A hash containing the user assignments @option options [String] :json_str A JSON string containing the user assignments @option options [String] :filename Path and filename to a file containing the user assignments in JSON

# File lib/rs_user_policy/user_assignments/json_user_assignments.rb, line 39
def initialize(options={})
  begin
    if options.has_key?(:json)
      @user_assignments = options[:json]
    elsif options.has_key?(:json_str)
      @user_assignments = JSON.parse(options[:json_str])
    elsif options.has_key?(:filename)
      @user_assignments = JSON.parse(File.read(options[:filename]))
    else
      @user_assignments = {}
    end
  rescue Errno::ENOENT, JSON::ParserError
    @user_assignments = {}
  end

  validate()
end

Public Instance Methods

[](email) click to toggle source

Returns a hash which represents the user specified by the email address specified If the user does not exist the (see add_user) method will be called and the user will be created.

@param [String] email The email address of the user to fetch

@return [Hash] A hash of key/value pairs to be passed to the RightScale API for Users#create. This will also include a “roles” key, and may also include any other keys returned by the source

# File lib/rs_user_policy/user_assignments/json_user_assignments.rb, line 113
def [](email)
  add_user(email)
end
add_user(email, options={}) click to toggle source

Adds a user to user_assignments. If the user already exists the existing record will be returned. Otherwise the user will be created with a single role of “immutable”

@param [String] email The email address of the user to create or return @param [Hash] options Hash of property key/value pairs for the user. The following options are known, but there can be any key in thi hash @option options [Array<String>] “roles” An array of role names for the user

@return [Hash] The added or existing user where they key is the users email, and the value is a hash of key/value pairs of user properties.

# File lib/rs_user_policy/user_assignments/json_user_assignments.rb, line 125
def add_user(email, options={})
  options = {"roles" => ["immutable"]}.merge(options)
  @user_assignments[email] || @user_assignments[email] = options
end
delete(email) click to toggle source

Deletes a user from the user assignments

@param [String] email The email address for the user

# File lib/rs_user_policy/user_assignments/json_user_assignments.rb, line 82
def delete(email)
  @user_assignments.delete(email)
end
get_roles(email) click to toggle source

Returns the roles assigned to the user. If the user does not exist they should be automatically created with the role “immutable”

@param [String] email The email address for the user

@return [Array<String>] The roles assigned to the user

# File lib/rs_user_policy/user_assignments/json_user_assignments.rb, line 73
def get_roles(email)
  # TODO: This seems expensive to do in an accessor?
  add_user(email)
  @user_assignments[email]['roles']
end
length() click to toggle source

@return [Int] The number of users in the user assignments object

# File lib/rs_user_policy/user_assignments/json_user_assignments.rb, line 58
def length
  @user_assignments.length
end
list() click to toggle source

Returns a list of all user emails which have a user assignment in the source

@return [Array<String>] An array of email addresses for users with a user assignment

# File lib/rs_user_policy/user_assignments/json_user_assignments.rb, line 102
def list
  @user_assignments.keys
end
serialize(options={}) click to toggle source

Commits any changes made to the UserAssignments object back to it’s original data store. This is an opportunity to perform DB flushes or write back to a source file.

@param [Hash] options A hash containing only one key; @option options [String] :filename The filename to write out the JSON state of this JsonUserAssignments object

@raise [ArgumentError] When no output file is specified

# File lib/rs_user_policy/user_assignments/json_user_assignments.rb, line 94
def serialize(options={})
  raise ArgumentError, "You must specify the :filename option" unless options.has_key?(:filename)
  File.open(options[:filename], 'w') {|f| f.write(JSON.pretty_generate(@user_assignments))}
end
size() click to toggle source

@return [Int] The number of users in the user assignments object

# File lib/rs_user_policy/user_assignments/json_user_assignments.rb, line 63
def size
  self.length
end

Private Instance Methods

validate() click to toggle source
# File lib/rs_user_policy/user_assignments/json_user_assignments.rb, line 132
def validate()
  # TODO: Also validate that the user assignments file is in the correct form.
  # I.E. {
  #   "email@address.com": "role"
  #}
end