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

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

10 11 12 13
  # Defined by the `Doorkeeper::ApplicationsController` and is redundant as we call `authenticate_user!` below. Not
  # defining or skipping this will result in a `403` response to all requests.
  skip_before_action :authenticate_admin!

14 15
  prepend_before_action :verify_user_oauth_applications_enabled, except: :index
  prepend_before_action :authenticate_user!
16
  before_action :add_gon_variables
17
  before_action :load_scopes, only: [:index, :create, :edit, :update]
18 19

  layout 'profile'
V
Valery Sizov 已提交
20 21

  def index
22
    set_index_vars
23 24
  end

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

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

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

D
Dmitriy Zaporozhets 已提交
38 39
  private

40
  def verify_user_oauth_applications_enabled
41
    return if Gitlab::CurrentSettings.user_oauth_applications?
42

43
    redirect_to profile_path
44 45
  end

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

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

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