Class: RsUserPolicy::UserAssignments::JsonUserAssignments

Inherits:
Object
  • Object
show all
Includes:
UserAssignments
Defined in:
lib/rs_user_policy/user_assignments/json_user_assignments.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ JsonUserAssignments

Initializes a new UserAssignments

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 JsonUserAssignments, where the keys are;

Options Hash (options):

  • :json (Hash)

    A hash containing the user assignments

  • :json_str (String)

    A JSON string containing the user assignments

  • :filename (String)

    Path and filename to a file containing the user assignments in JSON



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 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

Instance Method Details

#[](email) ⇒ Hash

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.

Parameters:

  • email (String)

    The email address of the user to fetch

Returns:

  • (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



113
114
115
# File 'lib/rs_user_policy/user_assignments/json_user_assignments.rb', line 113

def [](email)
  add_user(email)
end

#add_user(email, options = {}) ⇒ Hash

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”

Parameters:

  • email (String)

    The email address of the user to create or return

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

    Hash of property key/value pairs for the user. The following options are known, but there can be any key in thi hash

Options Hash (options):

  • "roles" (Array<String>)

    An array of role names for the user

Returns:

  • (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.



125
126
127
128
# 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) ⇒ Object

Deletes a user from the user assignments

Parameters:

  • email (String)

    The email address for the user



82
83
84
# File 'lib/rs_user_policy/user_assignments/json_user_assignments.rb', line 82

def delete(email)
  @user_assignments.delete(email)
end

#get_roles(email) ⇒ Array<String>

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

Parameters:

  • email (String)

    The email address for the user

Returns:

  • (Array<String>)

    The roles assigned to the user



73
74
75
76
77
# 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

#lengthInt

Returns The number of users in the user assignments object.

Returns:

  • (Int)

    The number of users in the user assignments object



58
59
60
# File 'lib/rs_user_policy/user_assignments/json_user_assignments.rb', line 58

def length
  @user_assignments.length
end

#listArray<String>

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

Returns:

  • (Array<String>)

    An array of email addresses for users with a user assignment



102
103
104
# File 'lib/rs_user_policy/user_assignments/json_user_assignments.rb', line 102

def list
  @user_assignments.keys
end

#serialize(options = {}) ⇒ Object

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.

Parameters:

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

    A hash containing only one key;

Options Hash (options):

  • :filename (String)

    The filename to write out the JSON state of this JsonUserAssignments object

Raises:

  • (ArgumentError)

    When no output file is specified



94
95
96
97
# 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

#sizeInt

Returns The number of users in the user assignments object.

Returns:

  • (Int)

    The number of users in the user assignments object



63
64
65
# File 'lib/rs_user_policy/user_assignments/json_user_assignments.rb', line 63

def size
  self.length
end