From 874b5d061d0648ba4b82bb4818aeb7ae423b2069 Mon Sep 17 00:00:00 2001 From: AI-Mozi Date: Fri, 3 Nov 2023 15:45:47 +0100 Subject: [PATCH 1/6] add and list notes on some db views --- app.rb | 42 ++++++++++++++++++++++++++++++ views/_add_note.erb | 14 ++++++++++ views/_notes.erb | 43 +++++++++++++++++++++++++++++++ views/db/advisories/show.erb | 4 +++ views/db/credentials/show.erb | 4 +++ views/db/email_addresses/show.erb | 5 ++++ views/db/host_names/show.erb | 4 +++ views/db/ip_addresses/show.erb | 4 +++ views/db/mac_addresses/show.erb | 4 +++ views/db/open_ports/show.erb | 5 ++++ views/db/passwords/show.erb | 5 ++++ views/db/ports/show.erb | 4 +++ views/db/services/show.erb | 4 +++ views/db/urls/show.erb | 4 +++ views/db/user_names/show.erb | 4 +++ 15 files changed, 150 insertions(+) create mode 100644 views/_add_note.erb create mode 100644 views/_notes.erb diff --git a/app.rb b/app.rb index ae416b6..d823577 100644 --- a/app.rb +++ b/app.rb @@ -341,6 +341,48 @@ class App < Sinatra::Base end end + { + host_names: Ronin::DB::HostName, + mac_addresses: Ronin::DB::MACAddress, + ip_addresses: Ronin::DB::IPAddress, + host_names: Ronin::DB::HostName, + ports: Ronin::DB::Port, + services: Ronin::DB::Service, + open_ports: Ronin::DB::OpenPort, + credentials: Ronin::DB::Credential, + urls: Ronin::DB::URL, + user_names: Ronin::DB::UserName, + email_addresses: Ronin::DB::EmailAddress, + passwords: Ronin::DB::Password, + advisories: Ronin::DB::Advisory + }.each do |name, model| + post "/db/#{name}/:id/notes" do + @record = model.find(params[:id]) + + if @record + if @record.notes.create!(body: params[:body]) + flash[:success] = "Note added successfully." + else + flash[:danger] = "Something went wrong!" + end + + redirect "/db/#{name}/#{params[:id]}" + else + halt 404 + end + end + + delete "/db/#{name}/:id/notes/:note_id" do + @record = model.find(params[:id]) + + if @record + @record.notes.destroy(params[:note_id]) + else + halt 404 + end + end + end + get '/db/asns' do @asns = Ronin::DB::ASN.all diff --git a/views/_add_note.erb b/views/_add_note.erb new file mode 100644 index 0000000..d328b77 --- /dev/null +++ b/views/_add_note.erb @@ -0,0 +1,14 @@ +
+
method="POST"> +
+
+ +
+
+

+ +

+
+
+
+
diff --git a/views/_notes.erb b/views/_notes.erb new file mode 100644 index 0000000..2fec729 --- /dev/null +++ b/views/_notes.erb @@ -0,0 +1,43 @@ +<% if notes.present? %> +

Notes

+ <% notes.each do |note| %> +
+
+
+

<%= note.body %>

+ Created at: <%= note.created_at %> +
+
+ +
+
+
+ <% end %> +<% end %> + + \ No newline at end of file diff --git a/views/db/advisories/show.erb b/views/db/advisories/show.erb index d1eebf3..ed11e42 100644 --- a/views/db/advisories/show.erb +++ b/views/db/advisories/show.erb @@ -98,3 +98,7 @@ + +<%= partial(:notes, notes: @advisory.notes) %> + +<%= partial(:add_note, parent: @advisory) %> diff --git a/views/db/credentials/show.erb b/views/db/credentials/show.erb index 1ed28c2..ba18d30 100644 --- a/views/db/credentials/show.erb +++ b/views/db/credentials/show.erb @@ -44,3 +44,7 @@ + +<%= partial(:notes, notes: @credential.notes) %> + +<%= partial(:add_note, parent: @credential) %> \ No newline at end of file diff --git a/views/db/email_addresses/show.erb b/views/db/email_addresses/show.erb index 62f2d2c..7cf7a17 100644 --- a/views/db/email_addresses/show.erb +++ b/views/db/email_addresses/show.erb @@ -36,3 +36,8 @@ + + +<%= partial(:notes, notes: @email_address.notes) %> + +<%= partial(:add_note, parent: @email_address) %> \ No newline at end of file diff --git a/views/db/host_names/show.erb b/views/db/host_names/show.erb index e01ea8e..6ea8a12 100644 --- a/views/db/host_names/show.erb +++ b/views/db/host_names/show.erb @@ -45,3 +45,7 @@ + +<%= partial(:notes, notes: @host_name.notes) %> + +<%= partial(:add_note, parent: @host_name) %> \ No newline at end of file diff --git a/views/db/ip_addresses/show.erb b/views/db/ip_addresses/show.erb index db9cac8..60b0607 100644 --- a/views/db/ip_addresses/show.erb +++ b/views/db/ip_addresses/show.erb @@ -91,3 +91,7 @@ + +<%= partial(:notes, notes: @ip_address.notes) %> + +<%= partial(:add_note, parent: @ip_address) %> \ No newline at end of file diff --git a/views/db/mac_addresses/show.erb b/views/db/mac_addresses/show.erb index 9c746f9..45758ff 100644 --- a/views/db/mac_addresses/show.erb +++ b/views/db/mac_addresses/show.erb @@ -54,3 +54,7 @@ + +<%= partial(:notes, notes: @mac_address.notes) %> + +<%= partial(:add_note, parent: @mac_address) %> diff --git a/views/db/open_ports/show.erb b/views/db/open_ports/show.erb index 724d3ea..c650af7 100644 --- a/views/db/open_ports/show.erb +++ b/views/db/open_ports/show.erb @@ -79,3 +79,8 @@ + + +<%= partial(:notes, notes: @open_port.notes) %> + +<%= partial(:add_note, parent: @open_port) %> \ No newline at end of file diff --git a/views/db/passwords/show.erb b/views/db/passwords/show.erb index b27fc09..95b7c18 100644 --- a/views/db/passwords/show.erb +++ b/views/db/passwords/show.erb @@ -44,3 +44,8 @@ + + +<%= partial(:notes, notes: @password.notes) %> + +<%= partial(:add_note, parent: @password) %> \ No newline at end of file diff --git a/views/db/ports/show.erb b/views/db/ports/show.erb index c198263..db7cd01 100644 --- a/views/db/ports/show.erb +++ b/views/db/ports/show.erb @@ -62,3 +62,7 @@ + +<%= partial(:notes, notes: @port.notes) %> + +<%= partial(:add_note, parent: @port) %> diff --git a/views/db/services/show.erb b/views/db/services/show.erb index de31030..eed8ece 100644 --- a/views/db/services/show.erb +++ b/views/db/services/show.erb @@ -57,3 +57,7 @@ + +<%= partial(:notes, notes: @service.notes) %> + +<%= partial(:add_note, parent: @service) %> diff --git a/views/db/urls/show.erb b/views/db/urls/show.erb index 0586250..e5effbd 100644 --- a/views/db/urls/show.erb +++ b/views/db/urls/show.erb @@ -96,3 +96,7 @@ + +<%= partial(:notes, notes: @url.notes) %> + +<%= partial(:add_note, parent: @url) %> \ No newline at end of file diff --git a/views/db/user_names/show.erb b/views/db/user_names/show.erb index 7275ae7..1e2f6c4 100644 --- a/views/db/user_names/show.erb +++ b/views/db/user_names/show.erb @@ -41,3 +41,7 @@ + +<%= partial(:notes, notes: @user_name.notes) %> + +<%= partial(:add_note, parent: @user_name) %> \ No newline at end of file From d186ca13ec43be5626cb90753673338822977529 Mon Sep 17 00:00:00 2001 From: AI-Mozi Date: Fri, 3 Nov 2023 15:48:10 +0100 Subject: [PATCH 2/6] change btn text --- views/_add_note.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/views/_add_note.erb b/views/_add_note.erb index d328b77..f1d9d30 100644 --- a/views/_add_note.erb +++ b/views/_add_note.erb @@ -6,7 +6,7 @@

- +

From 6d5018203ff5c77e832783a66dc97161ef3101e0 Mon Sep 17 00:00:00 2001 From: AI-Mozi Date: Fri, 3 Nov 2023 15:50:17 +0100 Subject: [PATCH 3/6] make rubocop happy --- app.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/app.rb b/app.rb index d823577..b127fca 100644 --- a/app.rb +++ b/app.rb @@ -342,7 +342,6 @@ class App < Sinatra::Base end { - host_names: Ronin::DB::HostName, mac_addresses: Ronin::DB::MACAddress, ip_addresses: Ronin::DB::IPAddress, host_names: Ronin::DB::HostName, @@ -358,20 +357,20 @@ class App < Sinatra::Base }.each do |name, model| post "/db/#{name}/:id/notes" do @record = model.find(params[:id]) - + if @record if @record.notes.create!(body: params[:body]) flash[:success] = "Note added successfully." else flash[:danger] = "Something went wrong!" end - + redirect "/db/#{name}/#{params[:id]}" else halt 404 end end - + delete "/db/#{name}/:id/notes/:note_id" do @record = model.find(params[:id]) From 2d29fa8057e6b7a6d700c371d7f11dd2f37f81ab Mon Sep 17 00:00:00 2001 From: AI-Mozi Date: Sat, 4 Nov 2023 18:15:47 +0100 Subject: [PATCH 4/6] move js to public, move _add_notes to _notes --- app.rb | 2 +- public/javascript/notes.js | 28 ++++++++++++++++++++++++++++ views/_notes.erb | 31 ++++--------------------------- views/db/advisories/show.erb | 2 -- views/db/credentials/show.erb | 2 -- views/db/email_addresses/show.erb | 2 -- views/db/host_names/show.erb | 2 -- views/db/ip_addresses/show.erb | 2 -- views/db/mac_addresses/show.erb | 1 - views/db/open_ports/show.erb | 2 -- views/db/passwords/show.erb | 2 -- views/db/ports/show.erb | 1 - views/db/services/show.erb | 1 - views/db/urls/show.erb | 2 -- views/db/user_names/show.erb | 2 -- 15 files changed, 33 insertions(+), 49 deletions(-) create mode 100644 public/javascript/notes.js diff --git a/app.rb b/app.rb index b127fca..09ab729 100644 --- a/app.rb +++ b/app.rb @@ -362,7 +362,7 @@ class App < Sinatra::Base if @record.notes.create!(body: params[:body]) flash[:success] = "Note added successfully." else - flash[:danger] = "Something went wrong!" + flash[:danger] = "Failed to create Note." end redirect "/db/#{name}/#{params[:id]}" diff --git a/public/javascript/notes.js b/public/javascript/notes.js new file mode 100644 index 0000000..183a320 --- /dev/null +++ b/public/javascript/notes.js @@ -0,0 +1,28 @@ +const Notes = { + init() { + (document.querySelectorAll('.delete-note') || []).forEach(button => { + button.addEventListener('click', () => { + const noteId = button.getAttribute('data-note-id'); + Notes.delete(noteId, button) + }); + }); + }, + + delete(noteId, button) { + fetch(`${document.location}/notes/${noteId}`, { + method: 'DELETE' + }) + .then(response => { + if (response.ok) { + button.parentElement.parentElement.remove(); + } else { + console.error('Failed to delete note'); + } + }) + .catch(error => { + console.error('Error:', error); + }); + } +}; + +ready(Notes.init); diff --git a/views/_notes.erb b/views/_notes.erb index 2fec729..ef5199c 100644 --- a/views/_notes.erb +++ b/views/_notes.erb @@ -1,4 +1,6 @@ -<% if notes.present? %> + + +<% unless notes.empty? %>

Notes

<% notes.each do |note| %>
@@ -15,29 +17,4 @@ <% end %> <% end %> - \ No newline at end of file +<%= partial(:add_note) %> diff --git a/views/db/advisories/show.erb b/views/db/advisories/show.erb index ed11e42..768f468 100644 --- a/views/db/advisories/show.erb +++ b/views/db/advisories/show.erb @@ -100,5 +100,3 @@ <%= partial(:notes, notes: @advisory.notes) %> - -<%= partial(:add_note, parent: @advisory) %> diff --git a/views/db/credentials/show.erb b/views/db/credentials/show.erb index ba18d30..582dd4f 100644 --- a/views/db/credentials/show.erb +++ b/views/db/credentials/show.erb @@ -46,5 +46,3 @@ <%= partial(:notes, notes: @credential.notes) %> - -<%= partial(:add_note, parent: @credential) %> \ No newline at end of file diff --git a/views/db/email_addresses/show.erb b/views/db/email_addresses/show.erb index 7cf7a17..3e2a1e5 100644 --- a/views/db/email_addresses/show.erb +++ b/views/db/email_addresses/show.erb @@ -39,5 +39,3 @@ <%= partial(:notes, notes: @email_address.notes) %> - -<%= partial(:add_note, parent: @email_address) %> \ No newline at end of file diff --git a/views/db/host_names/show.erb b/views/db/host_names/show.erb index 6ea8a12..7bd1aa9 100644 --- a/views/db/host_names/show.erb +++ b/views/db/host_names/show.erb @@ -47,5 +47,3 @@ <%= partial(:notes, notes: @host_name.notes) %> - -<%= partial(:add_note, parent: @host_name) %> \ No newline at end of file diff --git a/views/db/ip_addresses/show.erb b/views/db/ip_addresses/show.erb index 60b0607..143d4fe 100644 --- a/views/db/ip_addresses/show.erb +++ b/views/db/ip_addresses/show.erb @@ -93,5 +93,3 @@ <%= partial(:notes, notes: @ip_address.notes) %> - -<%= partial(:add_note, parent: @ip_address) %> \ No newline at end of file diff --git a/views/db/mac_addresses/show.erb b/views/db/mac_addresses/show.erb index 45758ff..e5ccea2 100644 --- a/views/db/mac_addresses/show.erb +++ b/views/db/mac_addresses/show.erb @@ -57,4 +57,3 @@ <%= partial(:notes, notes: @mac_address.notes) %> -<%= partial(:add_note, parent: @mac_address) %> diff --git a/views/db/open_ports/show.erb b/views/db/open_ports/show.erb index c650af7..6801d0b 100644 --- a/views/db/open_ports/show.erb +++ b/views/db/open_ports/show.erb @@ -82,5 +82,3 @@ <%= partial(:notes, notes: @open_port.notes) %> - -<%= partial(:add_note, parent: @open_port) %> \ No newline at end of file diff --git a/views/db/passwords/show.erb b/views/db/passwords/show.erb index 95b7c18..6f57785 100644 --- a/views/db/passwords/show.erb +++ b/views/db/passwords/show.erb @@ -47,5 +47,3 @@ <%= partial(:notes, notes: @password.notes) %> - -<%= partial(:add_note, parent: @password) %> \ No newline at end of file diff --git a/views/db/ports/show.erb b/views/db/ports/show.erb index db7cd01..f8cafe7 100644 --- a/views/db/ports/show.erb +++ b/views/db/ports/show.erb @@ -65,4 +65,3 @@ <%= partial(:notes, notes: @port.notes) %> -<%= partial(:add_note, parent: @port) %> diff --git a/views/db/services/show.erb b/views/db/services/show.erb index eed8ece..4598362 100644 --- a/views/db/services/show.erb +++ b/views/db/services/show.erb @@ -60,4 +60,3 @@ <%= partial(:notes, notes: @service.notes) %> -<%= partial(:add_note, parent: @service) %> diff --git a/views/db/urls/show.erb b/views/db/urls/show.erb index e5effbd..48426d8 100644 --- a/views/db/urls/show.erb +++ b/views/db/urls/show.erb @@ -98,5 +98,3 @@ <%= partial(:notes, notes: @url.notes) %> - -<%= partial(:add_note, parent: @url) %> \ No newline at end of file diff --git a/views/db/user_names/show.erb b/views/db/user_names/show.erb index 1e2f6c4..5c2b12b 100644 --- a/views/db/user_names/show.erb +++ b/views/db/user_names/show.erb @@ -43,5 +43,3 @@ <%= partial(:notes, notes: @user_name.notes) %> - -<%= partial(:add_note, parent: @user_name) %> \ No newline at end of file From 50b86885411d29b0c4af19309886b49b65f6294c Mon Sep 17 00:00:00 2001 From: AI-Mozi Date: Sun, 5 Nov 2023 09:20:20 +0100 Subject: [PATCH 5/6] merge _add_note partial into _notes --- views/_add_note.erb | 14 -------------- views/_notes.erb | 16 +++++++++++++++- 2 files changed, 15 insertions(+), 15 deletions(-) delete mode 100644 views/_add_note.erb diff --git a/views/_add_note.erb b/views/_add_note.erb deleted file mode 100644 index f1d9d30..0000000 --- a/views/_add_note.erb +++ /dev/null @@ -1,14 +0,0 @@ -
-
method="POST"> -
-
- -
-
-

- -

-
-
-
-
diff --git a/views/_notes.erb b/views/_notes.erb index ef5199c..21529d8 100644 --- a/views/_notes.erb +++ b/views/_notes.erb @@ -17,4 +17,18 @@ <% end %> <% end %> -<%= partial(:add_note) %> +
+
method="POST"> +
+
+ +
+
+

+ +

+
+
+
+
+ From 627aabda1fb93ab15c2bec610421e5796d58f1c4 Mon Sep 17 00:00:00 2001 From: AI-Mozi Date: Sun, 5 Nov 2023 16:19:29 +0100 Subject: [PATCH 6/6] remove redundant new line --- views/_notes.erb | 1 - 1 file changed, 1 deletion(-) diff --git a/views/_notes.erb b/views/_notes.erb index 21529d8..5d74126 100644 --- a/views/_notes.erb +++ b/views/_notes.erb @@ -31,4 +31,3 @@
-