Commit 17c7f7cd by Ivan Lan

Version 1

parent 7c23df47
/.bundle/
/.yardoc
/Gemfile.lock
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/
/mysql.yml
/.rspec_status
\ No newline at end of file
IpWhitelist.redis = Redic.new
rails g ip_whitelist:migrations
include IpWhitelist::Interceptor
\ No newline at end of file
class CreateIpWhitelistList < ActiveRecord::Migration[5.1]
def change
create_table :ip_whitelist_list do |t|
create_table :ip_whitelist_lists do |t|
t.string :descr
t.string :ips
end
......
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'shotengai/version'
require 'ip_whitelist/version'
Gem::Specification.new do |spec|
spec.name = 'shotengai'
spec.version = Shotengai::VERSION
spec.name = 'ip_whitelist'
spec.version = IpWhitelist::VERSION
spec.authors = ['ivan Lan']
spec.email = ['mumumumushu@gmail.com']
......@@ -22,6 +22,4 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'bundler', '~> 1.14'
spec.add_development_dependency 'rake', '~> 10.0'
spec.add_development_dependency 'mysql2'
end
......@@ -12,7 +12,7 @@ module IpWhitelist
Time.now.utc.strftime('%Y%m%d%H%M%S')
end
source_root File.expand_path('../../../../db/migrate', __dir__)
source_root File.expand_path('../../../../db/migrate', __FILE__)
def copy_migrations
# Use sort() to order the migrations by seq
......
require "ip_whitelist/version"
require 'ip_whitelist/version'
require 'rails'
require 'active_record'
module Shotengai
module IpWhitelist
autoload :List, 'ip_whitelist/list'
autoload :Interceptor, 'ip_whitelist/interceptor'
autoload :Error, 'ip_whitelist/error'
def self.redis
@redis ||= Redic.new
@redis ||= Redis.new
end
def self.redis=(redis)
......
......@@ -3,16 +3,16 @@ module IpWhitelist
extend ActiveSupport::Concern
included do
private :ip_intercept
before_action :ip_intercept
rescue_from IpWhitelist::Error do |e|
render json: { code: e.code, message: e.message }, status: e.status
end
end
def ip_intercept
unless IpWhitelist::List.ips.includes(request.ip)
raise IpWhitelist::Error.new(status: 403, message: 'Invaild Request')
def ip_intercept
unless IpWhitelist::List.ips.include?(request.ip)
raise IpWhitelist::Error.new(status: 403, message: 'Invaild Request')
end
end
end
end
......
module IpWhitelist
class List < ActiveRecord::Base
self.table_name = 'ip_whitelist_lists'
REDIS_KEY = 'ip-whitelist-redis'
validates_presence_of :ips
......@@ -10,11 +12,10 @@ module IpWhitelist
end
def load
ips_ary = all.pluck(:ips).map { |ips_str| ips_str.split(',')}.reduce(:+)
ips_ary = all.pluck(:ips).map { |ips_str| ips_str.split(',') }.reduce(:+) || []
IpWhitelist.redis.set(REDIS_KEY, ips_ary.join(','))
ips_ary
end
end
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment