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

Commit 25e6925

Browse files
Merge pull request #95 from mena-devs/block-access-from-ip
Add function to block access to website from certain IP addresses
2 parents c2c4318 + e40686d commit 25e6925

File tree

4 files changed

+27
-6
lines changed

4 files changed

+27
-6
lines changed

app/controllers/application_controller.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ class ApplicationController < ActionController::Base
88
# API access
99
before_action :authenticate_with_token!, if: :api_request
1010

11+
# Block signups from unwanted IP addresses
12+
before_action :block_ip_addresses
13+
1114
def after_sign_in_path_for(resource)
1215
session[:custom_identifier] = resource.custom_identifier
1316
root_path
@@ -91,6 +94,7 @@ def render_json_422
9194
end
9295

9396
private
97+
9498
def authenticate_with_token!
9599
auth_token = params[:auth_token].presence
96100

@@ -111,4 +115,12 @@ def authenticate_with_token!
111115
end
112116
end
113117
end
118+
119+
def block_ip_addresses
120+
head :unauthorized if AppSettings.ip_addresses_black_list.include?(current_ip_address)
121+
end
122+
123+
def current_ip_address
124+
request.env['HTTP_X_REAL_IP'] || request.env['REMOTE_ADDR']
125+
end
114126
end

config/settings/development.yml.sample

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@ admin_email: ''
2323

2424
google_analytics_code: ''
2525

26+
ip_addresses_black_list:
27+
- 123.123.123.123
28+
2629
developers_emails:
2730
2831

2932
sender_email: [email protected]
33+
34+
meta_tags_keywords: ''

config/settings/production.yml.sample

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@ admin_email: ''
2323

2424
google_analytics_code: ''
2525

26+
ip_addresses_black_list:
27+
- 123.123.123.123
28+
2629
developers_emails:
2730
2831

2932
sender_email: [email protected]
33+
34+
meta_tags_keywords: ''

config/settings/test.yml.sample

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,12 @@ admin_email: '[email protected]'
2424

2525
google_analytics_code: ''
2626

27+
ip_addresses_black_list:
28+
- 123.123.123.123
29+
2730
developers_emails:
2831
2932

30-
sender_email: '[email protected]'
33+
sender_email: [email protected]
3134

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

0 commit comments

Comments
 (0)