Class: Decidim::System::ApiUsersController

Inherits:
ApplicationController show all
Defined in:
decidim-system/app/controllers/decidim/system/api_users_controller.rb

Instance Method Summary collapse

Methods included from Headers::HttpCachingDisabler

#disable_http_caching

Methods included from TranslatableAttributes

#attachment?, #default_locale?

Instance Method Details

#createObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'decidim-system/app/controllers/decidim/system/api_users_controller.rb', line 43

def create
  @form = ::Decidim::System::ApiUserForm.from_params(params.merge!(name: params[:admin][:name], organization: organization))
  CreateApiUser.call(@form, current_admin) do
    on(:ok) do |api_user, secret|
      flash[:notice] = I18n.t("api_user.create.success", scope: "decidim.system", user: api_user.api_key)
      session[:api_user] = { id: api_user.id, secret: secret }
      redirect_to action: :index
    end

    on(:invalid) do
      flash[:error] = I18n.t("api_user.create.error", scope: "decidim.system")
      render :new, status: :unprocessable_entity
    end
  end
end

#destroyObject



18
19
20
21
22
23
24
25
26
# File 'decidim-system/app/controllers/decidim/system/api_users_controller.rb', line 18

def destroy
  Decidim.traceability.perform_action!("delete", api_user, current_admin) do
    api_user.destroy!
  end

  flash[:notice] = I18n.t("api_user.destroy.success", scope: "decidim.system")

  redirect_to action: :index
end

#indexObject



9
10
11
12
# File 'decidim-system/app/controllers/decidim/system/api_users_controller.rb', line 9

def index
  @api_users = api_users
  @secret_user = session.delete(:api_user)&.with_indifferent_access
end

#newObject



14
15
16
# File 'decidim-system/app/controllers/decidim/system/api_users_controller.rb', line 14

def new
  @form = form(::Decidim::System::ApiUserForm).instance
end

#updateObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'decidim-system/app/controllers/decidim/system/api_users_controller.rb', line 28

def update
  RefreshApiUserSecret.call(api_user, current_admin) do
    on(:ok) do |secret|
      flash[:notice] = I18n.t("api_user.refresh.success", scope: "decidim.system", user: api_user.api_key)
      session[:api_user] = { id: api_user.id, secret: secret }
      redirect_to action: :index
    end

    on(:invalid) do
      flash[:notice] = I18n.t("api_user.refresh.error", scope: "decidim.system")
      redirect_to action: :index
    end
  end
end