Restrict access to admin functions by IP
There are many possible ways to implement this feature: * Adding a custom middleware * Using rack-attack with a blocklist * Using routes constraints We're choosing to use a controller concern with a redirect because it's what we do to handle unauthorized cancancan exceptions.
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
class Admin::BaseController < ApplicationController
|
||||
include IpDeniedHandler
|
||||
layout "admin"
|
||||
before_action :authenticate_user!
|
||||
|
||||
|
||||
17
app/controllers/concerns/ip_denied_handler.rb
Normal file
17
app/controllers/concerns/ip_denied_handler.rb
Normal file
@@ -0,0 +1,17 @@
|
||||
module IpDeniedHandler
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
before_action :restrict_ip, unless: :allowed_ip?
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def restrict_ip
|
||||
redirect_to root_path, alert: t("ip_denied_handler.unauthorized")
|
||||
end
|
||||
|
||||
def allowed_ip?
|
||||
RestrictAdminIps.new(request.remote_ip).allowed?
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user