about summary refs log tree commit diff
path: root/app/controllers/settings/applications_controller.rb
blob: ed3f82a8e06e693e8107c0fbdf2211d7ec48a3a0 (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
64
65
66
67
68
69
70
71
72
# frozen_string_literal: true

class Settings::ApplicationsController < Settings::BaseController
  layout 'admin'

  before_action :authenticate_user!
  before_action :set_application, only: [:show, :update, :destroy, :regenerate]
  before_action :prepare_scopes, only: [:create, :update]

  def index
    @applications = current_user.applications.order(id: :desc).page(params[:page])
  end

  def new
    @application = Doorkeeper::Application.new(
      redirect_uri: Doorkeeper.configuration.native_redirect_uri,
      scopes: 'read write follow'
    )
  end

  def show; end

  def create
    @application = current_user.applications.build(application_params)

    if @application.save
      redirect_to settings_applications_path, notice: I18n.t('applications.created')
    else
      render :new
    end
  end

  def update
    if @application.update(application_params)
      redirect_to settings_applications_path, notice: I18n.t('generic.changes_saved_msg')
    else
      render :show
    end
  end

  def destroy
    @application.destroy
    redirect_to settings_applications_path, notice: I18n.t('applications.destroyed')
  end

  def regenerate
    @access_token = current_user.token_for_app(@application)
    @access_token.destroy

    redirect_to settings_application_path(@application), notice: I18n.t('applications.token_regenerated')
  end

  private

  def set_application
    @application = current_user.applications.find(params[:id])
  end

  def application_params
    params.require(:doorkeeper_application).permit(
      :name,
      :redirect_uri,
      :scopes,
      :website
    )
  end

  def prepare_scopes
    scopes = params.fetch(:doorkeeper_application, {}).fetch(:scopes, nil)
    params[:doorkeeper_application][:scopes] = scopes.join(' ') if scopes.is_a? Array
  end
end