more robust validation for ip bans
This commit is contained in:
@@ -7,7 +7,12 @@ class IpBansController < ApplicationController
|
|||||||
|
|
||||||
def create
|
def create
|
||||||
@ip_ban = IpBan.create(params[:ip_ban])
|
@ip_ban = IpBan.create(params[:ip_ban])
|
||||||
redirect_to ip_bans_path
|
|
||||||
|
if @ip_ban.errors.any?
|
||||||
|
render :action => "new"
|
||||||
|
else
|
||||||
|
redirect_to ip_bans_path
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def index
|
def index
|
||||||
|
|||||||
@@ -1,18 +1,20 @@
|
|||||||
class IpBan < ActiveRecord::Base
|
class IpBan < ActiveRecord::Base
|
||||||
|
IP_ADDR_REGEX = /\A(?:[0-9]{1,3}\.){3}[0-9]{1,3}\Z/
|
||||||
belongs_to :creator, :class_name => "User"
|
belongs_to :creator, :class_name => "User"
|
||||||
before_validation :initialize_creator, :on => :create
|
before_validation :initialize_creator, :on => :create
|
||||||
validates_presence_of :reason, :creator
|
validates_presence_of :reason, :creator, :ip_addr
|
||||||
validates_uniqueness_of :ip_addr
|
validates_format_of :ip_addr, :with => IP_ADDR_REGEX
|
||||||
|
validates_uniqueness_of :ip_addr, :if => lambda {|rec| rec.ip_addr =~ IP_ADDR_REGEX}
|
||||||
|
|
||||||
def self.is_banned?(ip_addr)
|
def self.is_banned?(ip_addr)
|
||||||
exists?(["ip_addr = ?", ip_addr])
|
exists?("ip_addr = ?", ip_addr)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.search(params)
|
def self.search(params)
|
||||||
q = scoped
|
q = scoped
|
||||||
return q if params.blank?
|
return q if params.blank?
|
||||||
|
|
||||||
if params[:ip_addr]
|
if params[:ip_addr].present?
|
||||||
q = q.where("ip_addr = ?", params[:ip_addr])
|
q = q.where("ip_addr = ?", params[:ip_addr])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -2,8 +2,10 @@
|
|||||||
<div id="a-new">
|
<div id="a-new">
|
||||||
<h1>New IP Ban</h1>
|
<h1>New IP Ban</h1>
|
||||||
|
|
||||||
|
<%= error_messages_for "ip_ban" %>
|
||||||
|
|
||||||
<%= simple_form_for(@ip_ban) do |f| %>
|
<%= simple_form_for(@ip_ban) do |f| %>
|
||||||
<%= f.input :ip_addr %>
|
<%= f.input :ip_addr, :label => "IP Address" %>
|
||||||
<%= f.input :reason, :input_html => {:size => "50x5"} %>
|
<%= f.input :reason, :input_html => {:size => "50x5"} %>
|
||||||
<%= f.button :submit, "Submit" %>
|
<%= f.button :submit, "Submit" %>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|||||||
Reference in New Issue
Block a user