Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List Notes and add Add note button. #59

Merged
merged 6 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,47 @@ class App < Sinatra::Base
end
end

{
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] = "Failed to create Note."
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

Expand Down
28 changes: 28 additions & 0 deletions public/javascript/notes.js
Original file line number Diff line number Diff line change
@@ -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);
33 changes: 33 additions & 0 deletions views/_notes.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<script type="text/javascript" src="/javascript/notes.js"></script>

<% unless notes.empty? %>
<h3>Notes</h3>
<% notes.each do |note| %>
<div class="box">
<div class="columns is-full">
<div class="column">
<h4 class="mb-0"><%= note.body %></h4>
<small>Created at: <%= note.created_at %></small>
</div>
<div class="column is-one-fifth has-text-right">
<button class="button is-danger is-small delete-note " data-note-id=<%= note.id %>>X</button>
</div>
</div>
</div>
<% end %>
<% end %>

<div class="control mt-4">
<form action=<%="#{request.path_info}/notes"%> method="POST">
<div class="media-content">
<div class="field">
<textarea class="textarea" name="body" placeholder="Add a note..."></textarea>
</div>
<div class="field">
<p class="control">
<button type="submit" class="button is-primary">Add note</button>
</p>
</div>
</div>
</form>
</div>
2 changes: 2 additions & 0 deletions views/db/advisories/show.erb
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,5 @@
</tr>
</tbody>
</table>

<%= partial(:notes, notes: @advisory.notes) %>
2 changes: 2 additions & 0 deletions views/db/credentials/show.erb
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,5 @@
</tr>
</tbody>
</table>

<%= partial(:notes, notes: @credential.notes) %>
3 changes: 3 additions & 0 deletions views/db/email_addresses/show.erb
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,6 @@
</tr>
</tbody>
</table>


<%= partial(:notes, notes: @email_address.notes) %>
2 changes: 2 additions & 0 deletions views/db/host_names/show.erb
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,5 @@
</tr>
</tbody>
</table>

<%= partial(:notes, notes: @host_name.notes) %>
2 changes: 2 additions & 0 deletions views/db/ip_addresses/show.erb
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,5 @@
</tr>
</tbody>
</table>

<%= partial(:notes, notes: @ip_address.notes) %>
3 changes: 3 additions & 0 deletions views/db/mac_addresses/show.erb
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,6 @@
</tr>
</tbody>
</table>

<%= partial(:notes, notes: @mac_address.notes) %>

3 changes: 3 additions & 0 deletions views/db/open_ports/show.erb
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,6 @@
</tbody>
</tbody>
</table>


<%= partial(:notes, notes: @open_port.notes) %>
3 changes: 3 additions & 0 deletions views/db/passwords/show.erb
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,6 @@
</tr>
</tbody>
</table>


<%= partial(:notes, notes: @password.notes) %>
3 changes: 3 additions & 0 deletions views/db/ports/show.erb
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,6 @@
</tr>
</tbody>
</table>

<%= partial(:notes, notes: @port.notes) %>

3 changes: 3 additions & 0 deletions views/db/services/show.erb
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,6 @@
</tr>
</tbody>
</table>

<%= partial(:notes, notes: @service.notes) %>

2 changes: 2 additions & 0 deletions views/db/urls/show.erb
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,5 @@
</tr>
</tbody>
</table>

<%= partial(:notes, notes: @url.notes) %>
2 changes: 2 additions & 0 deletions views/db/user_names/show.erb
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,5 @@
</tr>
</tbody>
</table>

<%= partial(:notes, notes: @user_name.notes) %>