-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.rb
185 lines (139 loc) · 4.73 KB
/
routes.rb
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
require 'sinatra'
require './plugins/host_tracker/classes.rb'
require './plugins/host_tracker/host_tracker_helpers.rb'
# Now the magic happens
# Hosts List Menu
# We are handling the host list page in two ways.
# This is because of the way Serpico handles plugins. It sends the report id as a url variable instead of being in the url
get '/host_tracker/hosts' do
# Check for valid session
redirect to("/") unless valid_session?
id = params[:report_id]
@report = get_report(id)
@plugin_side_menu = get_plugin_list('user') # TODO: Look into how this works
#return 'No Such Report' if @report.nil? # # TODO: Handle this a little more gracefully. Maybe an alert?
# Hey go get those hosts please
@hosts = get_hosts(@report)
haml :"../plugins/host_tracker/views/list_hosts"
end
#Hosts List Menu 2
get '/host_tracker/:id/hosts' do
# Check for valid session
redirect to("/") unless valid_session?
id = params[:id]
# Get the report
@report = get_report(id)
# Error Handeling
@error_msgs = []
@error = params[:error]
@error_msgs << params[:error_msg] unless params[:error_msg].empty?
@plugin_side_menu = get_plugin_list('user') # TODO: Look into how this works
return 'No Such Report' if @report.nil? # # TODO: Handle this a little more gracefully. Maybe an alert?
# Hey go get those hosts please
@hosts = get_hosts(@report)
haml :"../plugins/host_tracker/views/list_hosts"
end
get '/host_tracker/:id/host/:host_id' do
# Check for valid session
redirect to("/") unless valid_session?
id = params[:id]
host_id = params[:host_id]
# Get the report
@report = get_report(id)
# If it is not a valid report throw an error
return 'No Such Report' if @report.nil? # # TODO: Handle this a little more gracefully. Maybe an alert?
# Error Handeling
@error_msgs = []
@error = params[:error]
@error_msgs << params[:error_msg] unless params[:error_msg].nil?
@plugin_side_menu = get_plugin_list('user') # TODO: Look into how this works
# Hey go get those hosts please
@host = get_host(host_id, id)
haml :"../plugins/host_tracker/views/individual_host"
end
# Create a new hosts in the report
get '/host_tracker/:id/hosts/new' do
# Query for the first report matching the report_name
id = params[:id]
@report = get_report(id)
return 'No Such Report' if @report.nil?
@hosts = get_hosts(@report)
haml :"../plugins/host_tracker/views/create_host"
end
# Create the host in the DB
post '/host_tracker/:id/hosts/new' do
error = mm_verify(request.POST)
return error if error.size > 1
data = url_escape_hash(request.POST)
id = params[:id]
@report = get_report(id)
return 'No Such Report' if @report.nil?
data['report_id'] = id
@host = ManagedHosts.new(data)
@allhosts = get_hosts(@report)
@error = false
@error_msg = ""
# Check to see if the host already exists
@allhosts.each do |h|
if h.ip == @host.ip
@error = true
@error_msg = "The host #{@host.ip} already exists!"
end
end
# Save if the ip did not match any of the hosts
if @error == false
@host.save
end
redirect to("/host_tracker/#{id}/hosts?error=#{@error}&error_msg=#{@error_msg}")
end
# Delete a template host
get '/host_tracker/:id/hosts/delete/:host_id' do
id = params[:id]
@report = get_report(id)
params[:host_id].split(',').each do |current_id|
host = ManagedHosts.first(id: current_id)
return "No Such host : #{current_id}" if host.nil?
# delete the entries
host.destroy
end
redirect to("/host_tracker/#{id}/hosts")
end
# Edit the host in a report
get '/host_tracker/:id/hosts/edit/:host_id' do
id = params[:id]
# Query for the first report matching the report_name
@report = get_report(id)
return 'No Such Report' if @report.nil?
host_id = params[:host_id]
# Query for all hosts
@host = ManagedHosts.first(report_id: id, id: host_id)
return "No Such Host #{@host.ip}" if @host.nil?
haml :"../plugins/host_tracker/views/edit_host"
end
# Edit a host in the report
post '/host_tracker/:id/hosts/edit/:host_id' do
# Check for kosher name in report name
id = params[:id]
# Query for the report
@report = get_report(id)
return 'No Such Report' if @report.nil?
host_id = params[:host_id]
# Query for all hosts
@host = get_host(host_id, id)
return 'No Such host' if @host.nil?
# not sure what is going on here
error = mm_verify(request.POST)
return error if error.size > 1
data = url_escape_hash(request.POST)
data['ip'] = data['ip'] # what is this all about?
# Update the host
unless @host.update(data)
error = ""
@host.errors.each do |f|
error = error + f.to_s() + "<br>"
end
return "<p>The following error(s) were found while trying to update : </p>#{error}"
end
@host.save
redirect to("/host_tracker/#{id}/hosts")
end