From c5e02a51be3932026e219c32970191ba37ed64f2 Mon Sep 17 00:00:00 2001 From: Will Bradley Date: Mon, 23 Sep 2013 18:51:22 -0700 Subject: [PATCH] Making it possible for users to easily register existing MACs --- app/controllers/macs_controller.rb | 7 ++++++- app/models/user.rb | 1 + app/views/macs/_form.html.erb | 3 ++- app/views/users/show.html.erb | 8 ++++++++ 4 files changed, 17 insertions(+), 2 deletions(-) diff --git a/app/controllers/macs_controller.rb b/app/controllers/macs_controller.rb index d26bc89..19fdea6 100644 --- a/app/controllers/macs_controller.rb +++ b/app/controllers/macs_controller.rb @@ -12,6 +12,8 @@ end load_and_authorize_resource :mac, :except => :create load_and_authorize_resource :user, :through => :mac, :except => [:index, :show, :scan, :import] +before_filter :arp_lookup, :only => :new + #require "active_record" require "optparse" #require "rubygems" @@ -136,7 +138,10 @@ end end end - +def arp_lookup + @ip = request.env['REMOTE_ADDR'] + @arp = %x(/usr/sbin/arp -a | grep #{@ip}) +end def scan Rails.logger.info "starting scan..." diff --git a/app/models/user.rb b/app/models/user.rb index 069582d..2604e15 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -15,6 +15,7 @@ class User < ActiveRecord::Base has_many :user_certifications has_many :certifications, :through => :user_certifications has_many :payments + has_many :macs validates_format_of [:twitter_url, :facebook_url, :github_url, :website_url], :with => URI::regexp(%w(http https)), :allow_blank => true diff --git a/app/views/macs/_form.html.erb b/app/views/macs/_form.html.erb index 61586dd..8287339 100644 --- a/app/views/macs/_form.html.erb +++ b/app/views/macs/_form.html.erb @@ -17,7 +17,8 @@
<%= f.label :mac %>
- <%= f.text_field :mac %> + <%= f.text_field :mac, :value => (@mac.mac || @arp || nil) %> + <% if @arp.present? %>
Autodetected from your IP of <%= @ip %><% end %>
<%= f.label :note %>
diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb index bd47b8e..4b03487 100644 --- a/app/views/users/show.html.erb +++ b/app/views/users/show.html.erb @@ -93,6 +93,14 @@ <% end %>

+Computers: <%= link_to "+ Add", new_mac_path, :class => 'btn' if can? :create, Mac %> + +

<% if current_user.admin? then %> Payments: