Muktadir
Muktadir

Reputation: 303

Restricting Access to Rails App based on IP-Address

I want to make an application that will be accessible only from a specific network connection. More clearly, it will be a attendance application. Users can only check in from a specific network connection. If they are connected with other network, they can not able to check in. How can I do it in Ruby on Rails. Please give me some idea. Actually I need to know that how to detect a specific network connection?

Thanks in advance.

Upvotes: 7

Views: 4470

Answers (1)

davidb
davidb

Reputation: 8954

In your ApplicationController add this:

before_filter :block_foreign_hosts

def whitelisted?(ip)
  return true if [123.43.65.1, 123.43.65.77].include?(ip)
  false
end

def block_foreign_hosts
  return false if whitelisted?(request.remote_ip)
  redirect_to "https://www.google.com" unless request.remote_ip.start_with?("123.456.789")
end

Of cause you have to replace the IP-Network I used in this example with the one you want to have access.

Upvotes: 8

Related Questions