separated out ip ban logic from regular bans, users can no longer register if an ip ban is in place

This commit is contained in:
albert
2010-03-18 17:55:57 -04:00
parent dcf8d0df4c
commit e46bfb3d76
12 changed files with 124 additions and 33 deletions

View File

@@ -5,14 +5,10 @@ class Ban < ActiveRecord::Base
attr_accessible :reason, :duration, :user_id
validate :user_is_inferior
def self.is_user_banned?(user)
def self.is_banned?(user)
exists?(["user_id = ? AND expires_at > ?", user.id, Time.now])
end
def self.is_ip_banned?(ip_addr)
exists?(["ip_addr = ? AND expires_at > ?", ip_addr, Time.now])
end
def user_is_inferior
if user
if user.is_admin?

9
app/models/ip_ban.rb Normal file
View File

@@ -0,0 +1,9 @@
class IpBan < ActiveRecord::Base
belongs_to :creator, :class_name => "User"
validates_presence_of :reason
validates_uniqueness_of :ip_addr
def self.is_banned?(ip_addr)
exists?(["ip_addr = ?", ip_addr])
end
end

View File

@@ -3,7 +3,7 @@ require 'digest/sha1'
class User < ActiveRecord::Base
class Error < Exception ; end
attr_accessor :password, :old_password
attr_accessor :password, :old_password, :ip_addr
attr_accessible :password, :old_password, :password_confirmation, :password_hash, :email, :last_logged_in_at, :last_forum_read_at, :has_mail, :receive_email_notifications, :comment_threshold, :always_resize_images, :favorite_tags, :blacklisted_tags, :name
validates_length_of :name, :within => 2..20, :on => :create
validates_format_of :name, :with => /\A[^\s;,]+\Z/, :on => :create, :message => "cannot have whitespace, commas, or semicolons"
@@ -13,6 +13,8 @@ class User < ActiveRecord::Base
validates_inclusion_of :default_image_size, :in => %w(medium large original)
validates_confirmation_of :password
validates_presence_of :email, :if => lambda {|rec| rec.new_record? && Danbooru.config.enable_email_verification?}
validates_presence_of :ip_addr, :on => :create
validate :validate_ip_addr_is_not_banned, :on => :create
before_save :encrypt_password
after_save :update_cache
before_create :promote_to_admin_if_first_user
@@ -21,6 +23,15 @@ class User < ActiveRecord::Base
belongs_to :inviter, :class_name => "User"
scope :named, lambda {|name| where(["lower(name) = ?", name])}
module BanMethods
def validate_ip_addr_is_not_banned
if IpBan.is_banned?(ip_addr)
self.errors[:base] << "IP address is banned"
return false
end
end
end
module NameMethods
module ClassMethods
def find_name(user_id)
@@ -222,6 +233,7 @@ class User < ActiveRecord::Base
end
end
include BanMethods
include NameMethods
include PasswordMethods
extend AuthenticationMethods

View File

@@ -72,7 +72,7 @@ class PostSetPresenter < Presenter
if current_page == total_pages
# do nothing
elsif current_page_max == total_pages
after_current_page.upto(total_pages) do|i|
after_current_page.upto(total_pages) do |i|
html << numbered_pagination_item(template, i)
end
else
@@ -80,8 +80,13 @@ class PostSetPresenter < Presenter
html << numbered_pagination_item(template, i)
end
html << "<li>...</li>"
html << numbered_pagination_item(template, total_pages)
if total_pages > 5
html << "<li>...</li>"
(after_current_page + 3).upto(total_pages) do |i|
html << numbered_pagination_item(template, i)
end
end
end
html << "</menu>"