Class: Nexpose::UserConfig

Inherits:
Object
  • Object
show all
Includes:
Sanitize
Defined in:
lib/nexpose/user.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Sanitize

#replace_entities

Constructor Details

#initialize(name, full_name, password, role_name = 'user', id = -1,, enabled = 1, email = nil, all_sites = false, all_groups = false) ⇒ UserConfig

Returns a new instance of UserConfig.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/nexpose/user.rb', line 91

def initialize(name, full_name, password, role_name = 'user', id = -1, enabled = 1, email = nil, all_sites = false, all_groups = false)
  @name = name
  @password = password
  @role_name = role_name
  @authsrcid = ('global-admin'.eql? @role_name) ? '1' : '2'
  @id = id
  @enabled = enabled
  @full_name = full_name
  @email = email
  @all_sites = all_sites || role_name == 'global-admin'
  @all_groups = all_groups || role_name == 'global-admin'
  @sites = []
  @groups = []
end

Instance Attribute Details

#all_groupsObject

Boolean values



89
90
91
# File 'lib/nexpose/user.rb', line 89

def all_groups
  @all_groups
end

#all_sitesObject

Boolean values



89
90
91
# File 'lib/nexpose/user.rb', line 89

def all_sites
  @all_sites
end

#authsrcidObject

Will default to XML (1) for global-admin, Data Source (2) otherwise, but caller can override (e.g., using LDAP authenticator).



83
84
85
# File 'lib/nexpose/user.rb', line 83

def authsrcid
  @authsrcid
end

#emailObject

Optional fields



85
86
87
# File 'lib/nexpose/user.rb', line 85

def email
  @email
end

#enabledObject

1 to enable this user, 0 to disable



87
88
89
# File 'lib/nexpose/user.rb', line 87

def enabled
  @enabled
end

#full_nameObject

Returns the value of attribute full_name.



80
81
82
# File 'lib/nexpose/user.rb', line 80

def full_name
  @full_name
end

#groupsObject

Optional fields



85
86
87
# File 'lib/nexpose/user.rb', line 85

def groups
  @groups
end

#idObject (readonly)

user id, set to -1 to create a new user



75
76
77
# File 'lib/nexpose/user.rb', line 75

def id
  @id
end

#nameObject (readonly)

Required fields



79
80
81
# File 'lib/nexpose/user.rb', line 79

def name
  @name
end

#passwordObject

Optional fields



85
86
87
# File 'lib/nexpose/user.rb', line 85

def password
  @password
end

#role_nameObject

valid roles: global-admin|security-manager|site-admin|system-admin|user|custom



77
78
79
# File 'lib/nexpose/user.rb', line 77

def role_name
  @role_name
end

#sitesObject

Optional fields



85
86
87
# File 'lib/nexpose/user.rb', line 85

def sites
  @sites
end

Class Method Details

.delete(connection, user_id) ⇒ Object

Delete a user account.



181
182
183
184
185
186
187
188
189
190
191
# File 'lib/nexpose/user.rb', line 181

def self.delete(connection, user_id)
  xml = '<UserDeleteRequest session-id="' + connection.session_id + '"'
  xml << %Q{ id="#{user_id}"}
  xml << ' />'
  r = connection.execute(xml, '1.1')
  if r.success
    r.res.elements.each('UserConfigResponse/UserConfig') do |config|
      '1'.eql? config.attributes['id']
    end
  end
end

.load(connection, user_id) ⇒ Object

Issue a UserConfigRequest to load an existing UserConfig from Nexpose.



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/nexpose/user.rb', line 155

def self.load(connection, user_id)
  xml = '<UserConfigRequest session-id="' + connection.session_id + '"'
  xml << %Q{ id="#{user_id}"}
  xml << ' />'
  r = connection.execute(xml, '1.1')
  if r.success
    r.res.elements.each('UserConfigResponse/UserConfig') do |config|
      id = config.attributes['id']
      role_name = config.attributes['role-name']
      #authsrcid = config.attributes['authsrcid']
      name = config.attributes['name']
      fullname = config.attributes['fullname']

      email = config.attributes['email']
      password = config.attributes['password']
      enabled = config.attributes['enabled'].to_i
      all_sites = config.attributes['allSites'] == 'true' ? true : false
      all_groups = config.attributes['allGroups'] == 'true' ? true : false
      # Not trying to load sites and groups.
      # Looks like API currently doesn't return that info to load.
      return UserConfig.new(name, fullname, password, role_name, id, enabled, email, all_sites, all_groups)
    end
  end
end

Instance Method Details

#delete(connection) ⇒ Object

Delete the user account associated with this object.



194
195
196
# File 'lib/nexpose/user.rb', line 194

def delete(connection)
  UserConfig.delete(connection, @id)
end

#save(connection) ⇒ Object

Save a user configuration. Returns the (new) user ID if successful.



139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/nexpose/user.rb', line 139

def save(connection)
  xml = '<UserSaveRequest session-id="' + connection.session_id + '">'
  xml << to_xml
  xml << '</UserSaveRequest>'
  r = connection.execute(xml, '1.1')
  if r.success
    r.res.elements.each('UserSaveResponse') do |attr|
      @id = attr.attributes['id'].to_i
    end
    @id
  else
    -1
  end
end

#to_sObject



106
107
108
109
110
111
112
113
# File 'lib/nexpose/user.rb', line 106

def to_s
  out = "#{@name} (#{@full_name}) [ID: #{@id}, Role: #{@role_name}]"
  out << ' Disabled' unless @enabled
  out << ' All-Sites' if @all_sites
  out << ' All-Groups' if @all_groups
  out << " e-mail: #{@email}" unless @email.nil? || @email.empty?
  out
end

#to_xmlObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/nexpose/user.rb', line 115

def to_xml
  xml = '<UserConfig'
  xml << %Q{ id="#{@id}"}
  xml << %Q{ authsrcid="#{@authsrcid}"}
  xml << %Q{ name="#{replace_entities(@name)}"}
  xml << %Q{ fullname="#{replace_entities(@full_name)}"}
  xml << %Q{ role-name="#{@role_name}"}
  xml << %Q{ password="#{replace_entities(@password)}"} if @password
  xml << %Q{ email="#{@email}"} if @email
  xml << %Q{ enabled="#{@enabled}"}
  # These two fields are keying off role_name to work around a defect.
  xml << %Q{ allGroups="#{@all_groups || @role_name == 'global-admin'}"}
  xml << %Q{ allSites="#{@all_sites || @role_name == 'global-admin'}"}
  xml << '>'
  @sites.each do |site|
    xml << %Q{<site id="#{site}" />}
  end
  @groups.each do |group|
    xml << %Q{<group id="#{group}" />}
  end
  xml << '</UserConfig>'
end