mr.coder
mr.coder

Reputation: 59

Rails 5: Preventing access to /admin pages based on IP address

I have been building a rails 5.2 app, and so far I want to separate or restrict access to /admin pages to only be accessible through office computers (local ips).

How can I achieve that, knowing I used activeadmin and petergate for authorization?

Upvotes: 2

Views: 151

Answers (1)

NM Pennypacker
NM Pennypacker

Reputation: 6942

You can create a before_action like this:

class ApplicationController < ActionController::Base
  before_action :filter_ip_address

  protected

  def filter_ip_address
    current_ip_address = request.env['HTTP_X_REAL_IP']
    head :unauthorized unless current_ip_address == "XX.XX.XX.XX"
  end
end

The example given is for application_controller, but you can put it wherever you need if you don't want to call it everywhere.

This is a modified version of the example I found here: https://coderwall.com/p/v980ha/restrict-ip-access-in-rails

Upvotes: 4

Related Questions