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

Valery Sizov's avatar
Valery Sizov committed
3
class Oauth::ApplicationsController < Doorkeeper::ApplicationsController
4
  include Gitlab::GonHelper
5
  include Gitlab::Allowable
6
  include PageLayoutHelper
7
  include OauthApplications
8

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

14 15
  helper_method :can?

16
  layout 'profile'
Valery Sizov's avatar
Valery Sizov committed
17 18

  def index
19
    set_index_vars
20 21
  end

Valery Sizov's avatar
Valery Sizov committed
22
  def create
James Lopez's avatar
James Lopez committed
23
    @application = Applications::CreateService.new(current_user, create_application_params).execute(request)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
24

25 26
    if @application.persisted?
      flash[:notice] = I18n.t(:notice, scope: [:doorkeeper, :flash, :applications, :create])
James Lopez's avatar
James Lopez committed
27 28

      redirect_to oauth_application_url(@application)
Valery Sizov's avatar
Valery Sizov committed
29
    else
30 31
      set_index_vars
      render :index
Valery Sizov's avatar
Valery Sizov committed
32 33 34
    end
  end

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
35 36
  private

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

40
    redirect_to profile_path
41 42
  end

43 44 45 46 47 48 49 50 51 52 53
  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
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
54 55 56 57 58 59 60
  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
61 62 63 64 65 66

  def create_application_params
    application_params.tap do |params|
      params[:owner] = current_user
    end
  end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
67
end