-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuser_search.thor
238 lines (212 loc) · 5.95 KB
/
user_search.thor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
Bundler.require
require 'logger'
class Scraper
class BraintreeScraper
def initialize(username, password, users)
@host = 'https://www.braintreegateway.com'
@username = username
@password = password
@agent = Mechanize.new
@pages = {}
@users = users
end
def login
page = @agent.get("#{@host}/login")
page.form.login = @username
page.form.password = @password
page = page.form.submit
@merchant_id = page.uri.path.split('/')[2]
page
end
def scrape_all_users
login
page_number = 1
while true do
page = @agent.get("#{@host}/users?merchant_id=#{@merchant_id}&page=#{page_number}")
print "scraping page #{page_number}"
rows = scrape_users_page(page)
puts
break if rows == 0
page_number += 1
end
scrape_roles
end
def scrape_users_page(page)
rows = page.search('.sep tr')
user_count = 0
rows.each do |row|
begin
cells = row.search('td')
next if cells.empty?
user_count += 1
username = cells.first.text.strip
url = "#{@host}/#{cells.first.search('a').first.attributes['href'].value}"
name = cells[1].text
email = cells[2].text
status = cells[3].text
id = url.split('/').last
# api_access =
@users << AuthorizedUser.new(name, username, email, id, url, status, [], [])
print '.'
rescue => e
print 'E'
File.open('scrape.err.log', 'a') { |f| f.puts [e.message, e.backtrace].join("\n") }
end
end
user_count
end
def scrape_roles
roles_page = @agent.get("#{@host}/merchants/#{@merchant_id}/roles")
roles_page.search('#roles.sep a').each do |element|
url = element.attributes['href'].value
scrape_role_page(@agent.get("#{@host}#{url}")) unless url[-4..-1] == 'edit'
end
end
def scrape_role_page(page)
role_name = page.search('h2').first.text
print "scraping roles for #{role_name}"
page.search('div.block a').each do |element|
id = element.attributes['href'].value.split('/').last
user = @users.detect { |u| u.id == id }
if user.nil?
puts "Unknown user id #{id} on role page #{role_name}"
else
user.roles << role_name
end
print '.'
end
puts
end
def scrape_all_active_users_mid_access
@users.select { |u| u.url =~ /braintree/ && u.status =~ /active/ }.
tap { |a| puts "scraping #{a.length} Active Braintree user pages for MID permissions" }.
each_with_index do |u, i|
print '.' if (i + 1).divmod(10)[1] == 0
scrape_user_page(u)
end
end
def scrape_user_page(user)
user_page = @agent.get(user.url)
elements = user_page.search('[id^=merchant_account] [type=checkbox]')
all = elements[0]
all_checked = all.attributes.keys.include?('checked')
if all_checked
user.mids << 'ALL'
else
user.mids << elements[1..-1].select { |e| e.attributes.keys.include?('checked') }.map { |e| e.parent.text.strip }
user.mids.flatten!
end
end
end
attr_accessor :host, :agent, :users
def initialize(username, password)
@username = username
@password = password
end
def scrape_all
@users = [] # init here so reload doesn't dupe everything
BraintreeScraper.new(@username, @password, @users).scrape_all_users
end
def scrape_user_details
BraintreeScraper.new(@username, @password, @users).scrape_all_active_users_mid_access
end
end
class AuthorizedUser < Struct.new(:name, :username, :email, :id, :url, :status, :roles, :mids)
def to_s
"#{name.ljust(35)}#{username.ljust(35)}#{email.ljust(35)} #{status}"
end
def color
case status
when /active/
:bold
when /suspended/
:red
else
nil
end
end
def matches(query)
re = /#{query}/i
name =~ re || username =~ re || email =~ re || status =~ re || id =~ re || !((roles || []).grep(re).empty?)
end
end
class Users < Thor
include Thor::Actions
default_task :search
desc('search', 'search for a Braintree user. QUERY can match anything')
def search
login_and_scrape
end
private
def login_and_scrape
username = ask 'Braintree Username:'
password = ask 'Braintree Password:'
@scraper = Scraper.new(username, password)
@include_roles = false
scrape
end
def scrape
@scraper.scrape_all
@users = @scraper.users
prompt
end
def run(queries)
@found = []
Array(queries).each do |query|
@users.each do |user|
@found << user if user.matches(query)
end
end
@found.each_with_index do |user, index|
say "[#{index}] #{user.to_s}", user.color
say " #{user.roles.join(', ')}", user.color if @include_roles
end
prompt
end
def report
require 'csv'
filename = 'user.report.csv'
CSV.open(filename, 'w') do |f|
f << %w(Name Username Email ID Role)
@users.select { |u| u.status == 'active' }.each do |user|
user.roles.each do |role|
f << [user.name, user.username, user.email, user.id, role]
end
end
end
puts "#{filename} written"
prompt
end
def prompt
prompt_text = @found && !@found.empty? ? 'Type number to open user or new search text:' : 'Enter search text:'
result = ask prompt_text
# this isn't stack friendly, btw :)
case result
when /\A\d+\z/
user = @found[result.to_i]
`open #{user.url}` unless user.nil?
prompt
when /pry/
pry.binding
when 'exit', 'quit'
return
when 'reload'
scrape
when 'roles'
@include_roles = !@include_roles
prompt
when 'report'
report
when /\Aany (.*)\z/
run($1.split(',').map(&:strip))
when /^\s*$/
prompt
when 'details'
else
run(result)
end
rescue => e
say e.message
prompt
end
end