applications_controller.rb 1.7 KB
Newer Older
V
Valery Sizov 已提交
1
class Oauth::ApplicationsController < Doorkeeper::ApplicationsController
2
  include Gitlab::GonHelper
3
  include PageLayoutHelper
4
  include OauthApplications
5

6
  before_action :verify_user_oauth_applications_enabled
7
  before_action :authenticate_user!
8
  before_action :add_gon_variables
9
  before_action :load_scopes, only: [:index, :create, :edit]
10 11

  layout 'profile'
V
Valery Sizov 已提交
12 13

  def index
14
    set_index_vars
15 16
  end

V
Valery Sizov 已提交
17
  def create
J
James Lopez 已提交
18
    @application = Applications::CreateService.new(current_user, create_application_params).execute(request)
D
Dmitriy Zaporozhets 已提交
19

J
James Lopez 已提交
20 21
    if @application.persisted?
      flash[:notice] = I18n.t(:notice, scope: [:doorkeeper, :flash, :applications, :create])
J
James Lopez 已提交
22 23

      redirect_to oauth_application_url(@application)
V
Valery Sizov 已提交
24
    else
25 26
      set_index_vars
      render :index
V
Valery Sizov 已提交
27 28 29
    end
  end

D
Dmitriy Zaporozhets 已提交
30 31
  private

32
  def verify_user_oauth_applications_enabled
33
    return if Gitlab::CurrentSettings.user_oauth_applications?
34

35
    redirect_to profile_path
36 37
  end

38 39 40 41 42 43 44 45 46 47 48
  def set_index_vars
    @applications = current_user.oauth_applications
    @authorized_tokens = current_user.oauth_authorized_tokens
    @authorized_anonymous_tokens = @authorized_tokens.reject(&:application)
    @authorized_apps = @authorized_tokens.map(&:application).uniq.reject(&:nil?)

    # Don't overwrite a value possibly set by `create`
    @application ||= Doorkeeper::Application.new
  end

  # Override Doorkeeper to scope to the current user
D
Dmitriy Zaporozhets 已提交
49 50 51 52 53 54 55
  def set_application
    @application = current_user.oauth_applications.find(params[:id])
  end

  rescue_from ActiveRecord::RecordNotFound do |exception|
    render "errors/not_found", layout: "errors", status: 404
  end
J
James Lopez 已提交
56 57 58 59 60 61

  def create_application_params
    application_params.tap do |params|
      params[:owner] = current_user
    end
  end
D
Dmitriy Zaporozhets 已提交
62
end