added ip bans controller

This commit is contained in:
albert
2011-01-14 15:50:17 -05:00
parent 541163685d
commit dceda1b073
8 changed files with 87 additions and 4 deletions

View File

@@ -1,2 +1,23 @@
class IpBansController < ApplicationController
before_filter :admin_only
def new
@ip_ban = IpBan.new
end
def create
@ip_ban = IpBan.create(params[:ip_ban])
redirect_to ip_bans_path
end
def index
@search = IpBan.search(params[:search])
@ip_bans = @search.paginate(:page => params[:page])
end
def destroy
@ip_ban = IpBan.find(params[:id])
@ip_ban.destroy
redirect_to ip_bans_path
end
end

View File

@@ -1,13 +1,14 @@
class IpBan < ActiveRecord::Base
belongs_to :creator, :class_name => "User"
validates_presence_of :reason
before_validation :initialize_creator, :on => :create
validates_presence_of :reason, :creator
validates_uniqueness_of :ip_addr
def self.is_banned?(ip_addr)
exists?(["ip_addr = ?", ip_addr])
end
def self.search(user_ids)
def self.query(user_ids)
comments = count_by_ip_addr("comments", user_ids, "creator_id", "ip_addr")
notes = count_by_ip_addr("note_versions", user_ids, "updater_id", "updater_ip_addr")
pools = count_by_ip_addr("pool_versions", user_ids, "updater_id", "updater_ip_addr")
@@ -24,4 +25,8 @@ class IpBan < ActiveRecord::Base
def self.count_by_ip_addr(table, user_ids, user_id_field = "user_id", ip_addr_field = "ip_addr")
select_all_sql("SELECT #{ip_addr_field}, count(*) FROM #{table} WHERE #{user_id_field} IN (?) GROUP BY #{ip_addr_field} ORDER BY count(*) DESC", user_ids)
end
def initialize_creator
self.creator_id = CurrentUser.id
end
end

View File

View File