summaryrefslogtreecommitdiff
path: root/app/controllers/admin/subnet_bans_controller.rb
blob: 50b9c0863645485b8af5e7ff7a6939d1c221e09f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# frozen_string_literal: true

require 'ipaddr'
require 'core_ext/ipaddr'

class Admin::SubnetBansController < ApplicationController
  before_action :load_ban, only: [:show, :edit, :update, :destroy]

  def index
    @title = 'Admin - Subnet Bans'
    authorize! :mod_read, SubnetBan
    @bans = if params[:q].present?
      # Only check the PG table if this looks like an IP address
      ip = (IPAddr.new(params[:q]) rescue nil)
      if ip
        SubnetBan.where('specification <<= :ip OR specification >>= :ip', ip: params[:q])
      else
        SubnetBan.where('(generated_ban_id IN (:q)) OR (to_tsvector(reason) @@ plainto_tsquery(:q)) OR (to_tsvector(note) @@ plainto_tsquery(:q))', q: params[:q])
      end
    else
      SubnetBan
    end.order(created_at: :desc).page(params[:page]).per(25)

    respond_to do |format|
      format.html
      format.json { render json: @bans }
    end
  end

  # TODO: Unfinished page
  def show
    @title = "Ban History: #{@ban.specification}"
    authorize! :manage, SubnetBan
  end

  def new
    @title = 'New Subnet Ban'
    authorize! :mod_read, SubnetBan
    @ban = SubnetBan.new
    respond_to do |format|
      format.html
      format.json { render json: @ban }
    end
  end

  def edit
    authorize! :edit, SubnetBan
  end

  def create
    @ban = SubnetBan.new(subnet_ban_params)
    @ban.banning_user = current_user
    authorize! :create, @ban
    respond_to do |format|
      @ban.specification = @ban.specification.mask(64) if @ban.specification.ipv6? # Putting this in a subnet_ban model callback breaks IPAddr in horrible ways.

      if @ban.save
        format.html { redirect_to admin_subnet_bans_path, notice: 'Subnet Ban was successfully created.' }
        format.json { render json: @ban, status: :created, location: admin_subnet_bans_path }
      else
        format.html { render action: 'new' }
        format.json { render json: @ban.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    authorize! :edit, SubnetBan
    respond_to do |format|
      if @ban.update(subnet_ban_params)
        format.html { redirect_to admin_subnet_bans_path, notice: 'Subnet Ban was successfully updated.' }
        format.json { render json: @ban, status: :created, location: admin_subnet_bans_path }
      else
        format.html { render action: 'new' }
        format.json { render json: @ban.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    authorize! :destroy, @ban
    @ban.destroy
    respond_to do |format|
      format.html { redirect_to admin_subnet_bans_url }
      format.json { head :ok }
    end
  end

  private

  def subnet_ban_params
    params.require(:subnet_ban).permit(:reason, :note, :enabled, :specification, :until)
  end

  def load_ban
    @ban = SubnetBan.find_by(id: params[:id])
  end
end