applications_controller.rb 1.9 KB
Newer Older
1 2
# frozen_string_literal: true

V
Valery Sizov 已提交
3
class Oauth::ApplicationsController < Doorkeeper::ApplicationsController
4
  include Gitlab::GonHelper
5
  include Gitlab::Allowable
6
  include PageLayoutHelper
7
  include OauthApplications
8
  include Gitlab::Experimentation::ControllerConcern
9

10
  before_action :verify_user_oauth_applications_enabled, except: :index
11
  before_action :authenticate_user!
12
  before_action :add_gon_variables
13
  before_action :load_scopes, only: [:index, :create, :edit, :update]
14

15 16
  helper_method :can?

17
  layout 'profile'
V
Valery Sizov 已提交
18 19

  def index
20
    set_index_vars
21 22
  end

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

J
James Lopez 已提交
26 27
    if @application.persisted?
      flash[:notice] = I18n.t(:notice, scope: [:doorkeeper, :flash, :applications, :create])
J
James Lopez 已提交
28 29

      redirect_to oauth_application_url(@application)
V
Valery Sizov 已提交
30
    else
31 32
      set_index_vars
      render :index
V
Valery Sizov 已提交
33 34 35
    end
  end

D
Dmitriy Zaporozhets 已提交
36 37
  private

38
  def verify_user_oauth_applications_enabled
39
    return if Gitlab::CurrentSettings.user_oauth_applications?
40

41
    redirect_to profile_path
42 43
  end

44 45 46 47 48 49 50 51 52 53 54
  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 已提交
55 56 57 58 59
  def set_application
    @application = current_user.oauth_applications.find(params[:id])
  end

  rescue_from ActiveRecord::RecordNotFound do |exception|
60
    render "errors/not_found", layout: "errors", status: :not_found
D
Dmitriy Zaporozhets 已提交
61
  end
J
James Lopez 已提交
62 63 64 65 66 67

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