Skip to content
This repository has been archived by the owner on Mar 23, 2023. It is now read-only.

Commit

Permalink
Merge pull request #95 from mena-devs/block-access-from-ip
Browse files Browse the repository at this point in the history
Add function to block access to website from certain IP addresses
  • Loading branch information
constantine-nikolaou authored Jun 6, 2022
2 parents c2c4318 + e40686d commit 25e6925
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
12 changes: 12 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ class ApplicationController < ActionController::Base
# API access
before_action :authenticate_with_token!, if: :api_request

# Block signups from unwanted IP addresses
before_action :block_ip_addresses

def after_sign_in_path_for(resource)
session[:custom_identifier] = resource.custom_identifier
root_path
Expand Down Expand Up @@ -91,6 +94,7 @@ def render_json_422
end

private

def authenticate_with_token!
auth_token = params[:auth_token].presence

Expand All @@ -111,4 +115,12 @@ def authenticate_with_token!
end
end
end

def block_ip_addresses
head :unauthorized if AppSettings.ip_addresses_black_list.include?(current_ip_address)
end

def current_ip_address
request.env['HTTP_X_REAL_IP'] || request.env['REMOTE_ADDR']
end
end
5 changes: 5 additions & 0 deletions config/settings/development.yml.sample
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ admin_email: ''

google_analytics_code: ''

ip_addresses_black_list:
- 123.123.123.123

developers_emails:
- [email protected]

sender_email: [email protected]

meta_tags_keywords: ''
5 changes: 5 additions & 0 deletions config/settings/production.yml.sample
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ admin_email: ''

google_analytics_code: ''

ip_addresses_black_list:
- 123.123.123.123

developers_emails:
- [email protected]

sender_email: [email protected]

meta_tags_keywords: ''
11 changes: 5 additions & 6 deletions config/settings/test.yml.sample
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@ admin_email: '[email protected]'

google_analytics_code: ''

ip_addresses_black_list:
- 123.123.123.123

developers_emails:
- [email protected]

sender_email: '[email protected]'
sender_email: [email protected]

meta_tags_keywords: 'web developer, mobile developer, software engineer,
technical director, CIO, software developer,
back-end developer, front-end developer,
technical project manager, CTO, jobs, freelance,
online community, lebanon, UAE, Egypt, Jordan, Syria'
meta_tags_keywords: ''

0 comments on commit 25e6925

Please sign in to comment.