about summary refs log tree commit diff
path: root/app/controllers/admin/relays_controller.rb
blob: c1297c8b991bb1a99e785f8d256f98e7425c90e7 (plain) (blame)
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
# frozen_string_literal: true

module Admin
  class RelaysController < BaseController
    before_action :set_relay, except: [:index, :new, :create]
    before_action :warn_signatures_not_enabled!, only: [:new, :create, :enable]

    def index
      authorize :relay, :update?
      @relays = Relay.all
    end

    def new
      authorize :relay, :update?
      @relay = Relay.new
    end

    def create
      authorize :relay, :update?

      @relay = Relay.new(resource_params)

      if @relay.save
        @relay.enable!
        redirect_to admin_relays_path
      else
        render action: :new
      end
    end

    def destroy
      authorize :relay, :update?
      @relay.destroy
      redirect_to admin_relays_path
    end

    def enable
      authorize :relay, :update?
      @relay.enable!
      redirect_to admin_relays_path
    end

    def disable
      authorize :relay, :update?
      @relay.disable!
      redirect_to admin_relays_path
    end

    private

    def set_relay
      @relay = Relay.find(params[:id])
    end

    def resource_params
      params.require(:relay).permit(:inbox_url)
    end

    def warn_signatures_not_enabled!
      flash.now[:error] = I18n.t('admin.relays.signatures_not_enabled') if authorized_fetch_mode?
    end
  end
end