Skip to content

Commit 9a445e2

Browse files
committed
Land rapid7#4707, updates to finder syntax
Updates some Rails 3 style ActiveRecord calls to use the Rails 4 Arel syntax, in preparation for our move to Rails 4. Fixes rapid7#4707, also see MSP-12018
2 parents 4822aa5 + 3deac54 commit 9a445e2

File tree

6 files changed

+14
-14
lines changed

6 files changed

+14
-14
lines changed

lib/msf/core/db_manager/client.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def report_client(opts)
3636
ret = {}
3737

3838
host = get_host(:workspace => wspace, :host => addr)
39-
client = host.clients.find_or_initialize_by_ua_string(opts[:ua_string])
39+
client = host.clients.where(ua_string: opts[:ua_string]).first_or_initialize
4040

4141
opts[:ua_string] = opts[:ua_string].to_s
4242

lib/msf/core/db_manager/cred.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,28 +102,28 @@ def report_auth_info(opts={})
102102
# If duplicate usernames are okay, find by both user and password (allows
103103
# for actual duplicates to get modified updated_at, sources, etc)
104104
if token[0].nil? or token[0].empty?
105-
cred = service.creds.find_or_initialize_by_user_and_ptype_and_pass(token[0] || "", ptype, token[1] || "")
105+
cred = service.creds.where(user: token[0] || "", ptype: ptype, pass: token[1] || "").first_or_initialize
106106
else
107107
cred = service.creds.find_by_user_and_ptype_and_pass(token[0] || "", ptype, token[1] || "")
108108
unless cred
109109
dcu = token[0].downcase
110110
cred = service.creds.find_by_user_and_ptype_and_pass( dcu || "", ptype, token[1] || "")
111111
unless cred
112-
cred = service.creds.find_or_initialize_by_user_and_ptype_and_pass(token[0] || "", ptype, token[1] || "")
112+
cred = service.creds.where(user: token[0] || "", ptype: ptype, pass: token[1] || "").first_or_initialize
113113
end
114114
end
115115
end
116116
else
117117
# Create the cred by username only (so we can change passwords)
118118
if token[0].nil? or token[0].empty?
119-
cred = service.creds.find_or_initialize_by_user_and_ptype(token[0] || "", ptype)
119+
cred = service.creds.where(user: token[0] || "", ptype: ptype).first_or_initialize
120120
else
121121
cred = service.creds.find_by_user_and_ptype(token[0] || "", ptype)
122122
unless cred
123123
dcu = token[0].downcase
124124
cred = service.creds.find_by_user_and_ptype_and_pass( dcu || "", ptype, token[1] || "")
125125
unless cred
126-
cred = service.creds.find_or_initialize_by_user_and_ptype(token[0] || "", ptype)
126+
cred = service.creds.where(user: token[0] || "", ptype: ptype).first_or_initialize
127127
end
128128
end
129129
end

lib/msf/core/db_manager/host.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,9 @@ def report_host(opts)
166166
end
167167

168168
if opts[:comm] and opts[:comm].length > 0
169-
host = wspace.hosts.find_or_initialize_by_address_and_comm(addr, opts[:comm])
169+
host = wspace.hosts.where(address: addr, comm: opts[:comm]).first_or_initialize
170170
else
171-
host = wspace.hosts.find_or_initialize_by_address(addr)
171+
host = wspace.hosts.where(address: addr).first_or_initialize
172172
end
173173
else
174174
host = addr
@@ -257,9 +257,9 @@ def update_host_via_sysinfo(opts)
257257
end
258258

259259
if opts[:comm] and opts[:comm].length > 0
260-
host = wspace.hosts.find_or_initialize_by_address_and_comm(addr, opts[:comm])
260+
host = wspace.hosts.where(address: addr, comm: opts[:comm]).first_or_initialize
261261
else
262-
host = wspace.hosts.find_or_initialize_by_address(addr)
262+
host = wspace.hosts.where(address: addr).first_or_initialize
263263
end
264264
else
265265
host = addr

lib/msf/core/db_manager/ref.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def find_or_create_ref(opts)
88
return ret[:ref] if ret[:ref]
99

1010
::ActiveRecord::Base.connection_pool.with_connection {
11-
ref = ::Mdm::Ref.find_or_initialize_by_name(opts[:name])
11+
ref = ::Mdm::Ref.where(name: opts[:name]).first_or_initialize
1212
if ref and ref.changed?
1313
ref.save!
1414
end

lib/msf/core/db_manager/service.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def report_service(opts)
8787

8888
proto = opts[:proto] || 'tcp'
8989

90-
service = host.services.find_or_initialize_by_port_and_proto(opts[:port].to_i, proto)
90+
service = host.services.where(port: opts[:port].to_i, proto: proto).first_or_initialize
9191
opts.each { |k,v|
9292
if (service.attribute_names.include?(k.to_s))
9393
service[k] = ((v and k == :name) ? v.to_s.downcase : v)

lib/msf/core/db_manager/web.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ def report_web_page(opts)
135135

136136
ret = {}
137137

138-
page = ::Mdm::WebPage.find_or_initialize_by_web_site_id_and_path_and_query(site[:id], path, query)
138+
page = ::Mdm::WebPage.where(web_site_id: site[:id], path: path, query: query).first_or_initialize
139139
page.code = code
140140
page.body = body
141141
page.headers = headers
@@ -243,7 +243,7 @@ def report_web_site(opts)
243243
=end
244244

245245
vhost ||= host.address
246-
site = ::Mdm::WebSite.find_or_initialize_by_vhost_and_service_id(vhost, serv[:id])
246+
site = ::Mdm::WebSite.where(vhost: vhost, service_id: serv[:id]).first_or_initialize
247247
site.options = opts[:options] if opts[:options]
248248

249249
# XXX:
@@ -342,7 +342,7 @@ def report_web_vuln(opts)
342342

343343
meth = meth.to_s.upcase
344344

345-
vuln = ::Mdm::WebVuln.find_or_initialize_by_web_site_id_and_path_and_method_and_pname_and_name_and_category_and_query(site[:id], path, meth, pname, name, cat, quer)
345+
vuln = ::Mdm::WebVuln.where(web_site_id: site[:id], path: path, method: meth, pname: pname, name: name, category: cat, query: quer).first_or_initialize
346346
vuln.name = name
347347
vuln.risk = risk
348348
vuln.params = para

0 commit comments

Comments
 (0)