summaryrefslogtreecommitdiff
path: root/app/controllers/admin/user_bans_controller.rb
blob: 8bcac2f0deb9232a2257a989edadff77c0ecd104 (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
99
# frozen_string_literal: true

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

  def index
    @title = 'Admin - User Bans'
    authorize! :mod_read, UserBan
    @bans = if params[:q].present?
      # Search user names, ban reasons, and ban notes.
      UserBan.where('(user_id IN (?)) OR (generated_ban_id IN (?)) OR (to_tsvector(reason) @@ plainto_tsquery(?)) OR (to_tsvector(note) @@ plainto_tsquery(?))',
                    User.where('name ILIKE ?', params[:q] + '%').select(:id),
                    params[:q], params[:q], params[:q])
    elsif params[:user_id]
      User.find(params[:user_id]).bans
    else
      UserBan
    end.order(created_at: :desc).includes(:user).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.username}"
    authorize! :manage, UserBan
  end

  def new
    authorize! :mod_read, UserBan
    @ban = UserBan.new
    @user = if params[:username]
      User.find_by(name: params[:username])
    elsif params[:user_id]
      User.find_by(id: params[:user_id])
    end

    @title = (@user ? "Ban #{@user.name}" : 'New User Ban')
    respond_to do |format|
      format.html
      format.json { render json: @ban }
    end
  end

  def create
    @ban = UserBan.new(user_ban_params)
    @ban.banning_user = current_user
    authorize! :create, @ban
    respond_to do |format|
      if @ban.save
        format.html { redirect_to admin_user_bans_path, notice: 'User was successfully banned.' }
        format.json { render json: @ban, status: :created, location: admin_user_bans_path }
      else
        format.html { render action: 'new' }
        format.json { render json: @ban.errors, status: :unprocessable_entity }
      end
    end
  end

  def edit
    @title = "Editing User Ban: #{@ban.username}"
    authorize! :edit, UserBan
  end

  def update
    authorize! :edit, UserBan
    respond_to do |format|
      if @ban.update(user_ban_params)
        format.html { redirect_to admin_user_bans_path, notice: 'User ban successfully updated.' }
        format.json { render json: @ban, status: :created, location: admin_user_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_user_bans_path }
      format.json { head :ok }
    end
  end

  private

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

  def user_ban_params
    params.require(:user_ban).permit(:reason, :note, :enabled, :until, :username, :user_id, :override_ip_ban)
  end
end