Permalink
Browse files

User from the XYZ office IPs is taken to XYZ board

  • Loading branch information...
1 parent 0c1497d commit c44c430cd767a34586c659c53e18e3eec68940e6 Daniel Grippi and Jeff Saracco committed Nov 6, 2012
@@ -1,5 +1,5 @@
class StandupsController < ApplicationController
- before_filter :load_standup, except: [:index, :new, :create]
+ before_filter :load_standup, except: [:index, :new, :create, :route]
def create
@standup = Standup.new(params[:standup])
@@ -43,6 +43,17 @@ def destroy
redirect_to standups_path
end
+ def route
+ ip_key = AuthorizedIps.corresponding_ip_key(request.env['REMOTE_ADDR'])
+ standup = Standup.find_by_ip_key(ip_key)
+
+ if ip_key && standup
+ redirect_to standup_items_path(standup)
+ else
+ redirect_to standups_path
+ end
+ end
+
private
View
@@ -1,5 +1,5 @@
class Standup < ActiveRecord::Base
- attr_accessible :title, :to_address, :subject_prefix
+ attr_accessible :title, :to_address, :subject_prefix, :ip_key
has_many :items, dependent: :destroy
has_many :posts, dependent: :destroy
@@ -38,6 +38,9 @@
</ul>
<% if session['logged_in'] %>
<ul class="nav pull-right">
+ <li>
+ <%= link_to "All Standups", standups_path %>
+ </li>
<li>
<%= link_to '/logout' do %>
<i class='icon-signout hidden-phone'></i>
@@ -14,6 +14,13 @@
</div>
</div>
+ <div class='ip_key'>
+ <div class='controls'>
+ <%= form.label :ip_key %>
+ <%= form.select(:ip_key, AuthorizedIps::AUTHORIZED_IP_ADDRESSES.keys) %>
+ </div>
+ </div>
+
<div class='to_address'>
<div class='controls'>
<%= form.label :to_address %>
View
@@ -26,5 +26,5 @@
match '/auth/:provider/callback', to: 'sessions#create'
match '/logout', to: 'sessions#destroy'
- root :to => 'standups#index'
+ root :to => 'standups#route'
end
@@ -0,0 +1,5 @@
+class AddIpKeyToStandups < ActiveRecord::Migration
+ def change
+ add_column :standups, :ip_key, :string
+ end
+end
View
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20121105161650) do
+ActiveRecord::Schema.define(:version => 20121106195229) do
create_table "items", :force => true do |t|
t.text "title"
@@ -45,6 +45,7 @@
t.string "to_address"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
+ t.string "ip_key"
end
end
View
@@ -7,7 +7,15 @@ def self.authorized_ip?(address)
AUTHORIZED_IP_ADDRESSES.each_value.to_a.flatten.any? { |block| block.include?(address) }
end
+ def self.corresponding_ip_key(address)
+ matching_standup = nil
+ AUTHORIZED_IP_ADDRESSES.each do |standup, ip_list|
+ matching_standup = standup.to_s if ip_list.any? {|ip| ip.include?(address)}
+ end
+ matching_standup
+ end
+
def development?
@request.env["REMOTE_ADDR"] == "127.0.0.1" && @request.env["HTTP_X_REAL_IP"].to_s == ""
end
-end
+end
@@ -86,4 +86,25 @@
response.should redirect_to standups_path
end
end
+
+ describe "#route" do
+ let!(:new_york) { create(:standup, ip_key: "nyc") }
+ let!(:san_fran) { create(:standup, ip_key: "sf") }
+
+ it "redirects to the standup that corresponds with the given ip" do
+ with_authorized_ips({nyc: [IPAddr.new("127.0.0.1/32")], sf: [IPAddr.new("123.4.5.6/32")]}) do
+ request.env['REMOTE_ADDR'] = '123.4.5.6'
+ get :route
+ response.should redirect_to standup_items_path(san_fran)
+ end
+ end
+
+ it "redirects to the standup index page if no standup with a corresponding ip" do
+ with_authorized_ips do
+ request.env['REMOTE_ADDR'] = '111.9.9.9'
+ get :route
+ response.should redirect_to standups_path
+ end
+ end
+ end
end
@@ -6,4 +6,10 @@
AuthorizedIps.authorized_ip?('127.0.0.1').should == true
end
end
-end
+
+ it "can tell what standup corresponds to an ip" do
+ with_authorized_ips({nyc: [IPAddr.new("127.0.0.1/32")]}) do
+ AuthorizedIps.corresponding_ip_key('127.0.0.1/32').should == "nyc"
+ end
+ end
+end
@@ -8,19 +8,22 @@
end
it "creates new standups", js: true do
- page.should have_content 'Choose a Standup'
- click_link('New Standup')
+ with_authorized_ips({london: [IPAddr.new("127.0.0.1/32")]}) do
+ page.should have_content 'Choose a Standup'
+ click_link('New Standup')
- fill_in 'standup_title', with: "London"
- fill_in 'standup_subject_prefix', with: "[Standup][ENG]"
- fill_in 'standup_to_address', with: "all@pivtoallabs.com"
- click_button 'Create Standup'
+ fill_in 'standup_title', with: "London"
+ fill_in 'standup_subject_prefix', with: "[Standup][ENG]"
+ select 'london', from: "standup_ip_key"
+ fill_in 'standup_to_address', with: "all@pivtoallabs.com"
+ click_button 'Create Standup'
- current_page = current_url
- current_page.should match(/http:\/\/127\.0\.0\.1:\d*\/standups\/\d*/)
- page.should have_content 'London'
- page.find('a.posts', text: 'London').click
- click_link('Current Post')
- current_page.should match(/http:\/\/127\.0\.0\.1:\d*\/standups\/\d*/)
+ current_page = current_url
+ current_page.should match(/http:\/\/127\.0\.0\.1:\d*\/standups\/\d*/)
+ page.should have_content 'London'
+ page.find('a.posts', text: 'London').click
+ click_link('Current Post')
+ current_page.should match(/http:\/\/127\.0\.0\.1:\d*\/standups\/\d*/)
+ end
end
end
@@ -1,10 +1,12 @@
module WithAuthorizedIps
- def with_authorized_ips(ips)
+ def with_authorized_ips(ips=nil)
old_value = AuthorizedIps::AUTHORIZED_IP_ADDRESSES
- Kernel::silence_warnings { AuthorizedIps.const_set('AUTHORIZED_IP_ADDRESSES', {sf: [IPAddr.new("127.0.0.1/27")]}) }
+
+ ips ||= {sf: [IPAddr.new("127.0.0.1/27")]}
+ Kernel::silence_warnings { AuthorizedIps.const_set('AUTHORIZED_IP_ADDRESSES', ips) }
yield
Kernel::silence_warnings { AuthorizedIps.const_set('AUTHORIZED_IP_ADDRESSES', old_value) }
end
-end
+end

0 comments on commit c44c430

Please sign in to comment.