Class: RsUserPolicy::User

Inherits:
Object
  • Object
show all
Defined in:
lib/rs_user_policy/user.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user) ⇒ User

Initializes read only attributes for an RsUserPolicy::User

Parameters:

  • user (RightApi::ResourceDetail)

    The user detail returned by RightApi::Client


29
30
31
32
33
34
# File 'lib/rs_user_policy/user.rb', line 29

def initialize(user)
  @email = user.email
  @href = user.href
  @user = user
  @permissions = {}
end

Instance Attribute Details

#emailObject (readonly)

Returns the value of attribute email.


24
25
26
# File 'lib/rs_user_policy/user.rb', line 24

def email
  @email
end

#hrefObject (readonly)

Returns the value of attribute href.


24
25
26
# File 'lib/rs_user_policy/user.rb', line 24

def href
  @href
end

#permissionsObject (readonly)

Returns the value of attribute permissions.


24
25
26
# File 'lib/rs_user_policy/user.rb', line 24

def permissions
  @permissions
end

Instance Method Details

#add_permission(account_href, permission) ⇒ Object

Adds a single permission for a single RightScale account

Parameters:

  • account_href (String)

    The RightScale API href of the account

  • permission (RightApi::ResourceDetail)

    A single RightApi::ResourceDetail for a permission.


51
52
53
54
# File 'lib/rs_user_policy/user.rb', line 51

def add_permission(, permission)
  @permissions[] ||= []
  @permissions[] << permission
end

#clear_permissions(account_href, client, options = {}) ⇒ Hash

Removes all permissions for the user in the specified rightscale account using the supplied client

Parameters:

  • account_href (String)

    The RightScale API href of the account

  • client (RightApi::Client)

    An active RightApi::Client instance for the account referenced in account_href

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

    Optional parameters

Options Hash (options):

  • :dry_run (Bool)

    If true, no API calls will be made, but the return value will contain the actions which would have been taken

Returns:

  • (Hash)

    A hash where the keys are the permission hrefs destroyed, and the keys are the role_title of those permissions

Raises:

  • (RightApi::ApiError)

    If an unrecoverable API error has occurred.


75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/rs_user_policy/user.rb', line 75

def clear_permissions(, client, options={})
  options = {:dry_run => false}.merge(options)
  current_permissions = get_api_permissions()
  if options[:dry_run]
    Hash[current_permissions.map{|p| [p.href, p.role_title]}]
  else
    retval = RsUserPolicy::RightApi::PermissionUtilities.destroy_permissions(
      current_permissions,
      client
    )
    @permissions.delete()
    retval
  end
end

#get_api_permissions(account_href) ⇒ Array<RightApi::ResourceDetail>

Returns the RightScale permissions the user has for the specified account href

Parameters:

  • account_href (String)

    The RightScale API href of the account

Returns:

  • (Array<RightApi::ResourceDetail>)

    An array of permission RightApi::ResourceDetail objects


61
62
63
# File 'lib/rs_user_policy/user.rb', line 61

def get_api_permissions()
  @permissions[] || []
end

#set_api_permissions(permissions, account_href, client, options = {}) ⇒ Hash

Removes and adds permissions as appropriate so that the users current permissions reflect the desired set passed in as “permissions”

Parameters:

  • permissions (Array<String>)

    The list of desired permissions for the user in the specified account

  • account_href (String)

    The RightScale API href of the account

  • client (RightApi::Client)

    An active RightApi::Client instance for the account referenced in account_href

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

    Optional parameters

Options Hash (options):

  • :dry_run (Bool)

    If true, no API calls will be made, but the return value will contain the actions which would have been taken

Returns:

  • (Hash, Hash)

    A tuple where two hashes are returned. The keys of the hashes are the href of the permission, and the values are the role_title of the permission. The first hash is the permissions removed, and the second hash is the permissions added

Raises:

  • (RightApi::ApiError)

    If an unrecoverable API error has occurred.


102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/rs_user_policy/user.rb', line 102

def set_api_permissions(permissions, , client, options={})
  options = {:dry_run => false}.merge(options)
  existing_api_permissions_response = get_api_permissions()
  existing_api_permissions = Hash[existing_api_permissions_response.map{|p| [p.role_title, p] }]
  if permissions.length == 0
    removed = clear_permissions(, client, options)
    @permissions.delete()
    return removed, {}
  else
    permissions_to_remove = (existing_api_permissions.keys - permissions).map{|p| existing_api_permissions[p]}
    remove_response = Hash[permissions_to_remove.map{|p| [p.href, p.role_title]}]
    unless options[:dry_run]
      remove_response = RsUserPolicy::RightApi::PermissionUtilities.destroy_permissions(permissions_to_remove, client)
    end

    permissions_to_add = {
      @href => Hash[(permissions - existing_api_permissions.keys).map{|p| [p,nil]}]
    }
    add_response = {}
    if options[:dry_run]
      href_idx = 0
      add_response = {
        @href => Hash[(permissions - existing_api_permissions.keys).map{|p| [p,(href_idx += 1)]}]
      }
    else
      add_response = RsUserPolicy::RightApi::PermissionUtilities.create_permissions(permissions_to_add, client)
    end

    @permissions[] = client.permissions.index(:filter => ["user_href==#{@href}"]) unless options[:dry_run]

    return remove_response, Hash[add_response[@href].keys.map{|p| [add_response[@href][p],p]}]
  end
end

#to_hashObject

Converts this object to a hash which can be serialized


37
38
39
40
41
42
43
44
45
# File 'lib/rs_user_policy/user.rb', line 37

def to_hash()
  rethash = {
    "permissions" => @permissions
  }
  (@user.attributes - [:links]).each do |attr_sym|
    rethash[attr_sym.to_s] = @user.send(attr_sym.to_s)
  end
  rethash
end