summaryrefslogtreecommitdiff
path: root/app/controllers/groups/variables_controller.rb
blob: 10038ff3ad9b60084add00c752b818c1290e8467 (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
module Groups
  class VariablesController < Groups::ApplicationController
    before_action :variable, only: [:show, :update, :destroy]
    before_action :authorize_admin_build!

    def index
      redirect_to group_settings_ci_cd_path(group)
    end

    def show
    end

    def update
      if variable.update(variable_params)
        redirect_to group_variables_path(group),
                    notice: 'Variable was successfully updated.'
      else
        render "show"
      end
    end

    def create
      @variable = group.variables.create(variable_params)
        .present(current_user: current_user)

      if @variable.persisted?
        redirect_to group_settings_ci_cd_path(group),
                    notice: 'Variable was successfully created.'
      else
        render "show"
      end
    end

    def destroy
      if variable.destroy
        redirect_to group_settings_ci_cd_path(group),
                    status: 302,
                    notice: 'Variable was successfully removed.'
      else
        redirect_to group_settings_ci_cd_path(group),
                    status: 302,
                    notice: 'Failed to remove the variable.'
      end
    end

    private

    def variable_params
      params.require(:variable).permit(*variable_params_attributes)
    end

    def variable_params_attributes
      %i[key value protected]
    end

    def variable
      @variable ||= group.variables.find(params[:id]).present(current_user: current_user)
    end

    def authorize_admin_build!
      return render_404 unless can?(current_user, :admin_build, group)
    end
  end
end