applications_controller.rb 1.74 KB
Newer Older
Valery Sizov's avatar
Valery Sizov committed
1
class Oauth::ApplicationsController < Doorkeeper::ApplicationsController
2
  include Gitlab::GonHelper
3
  include Gitlab::Allowable
4
  include PageLayoutHelper
5
  include OauthApplications
6

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

12 13
  helper_method :can?

14
  layout 'profile'
Valery Sizov's avatar
Valery Sizov committed
15 16

  def index
17
    set_index_vars
18 19
  end

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

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

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

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
33 34
  private

35
  def verify_user_oauth_applications_enabled
36
    return if Gitlab::CurrentSettings.user_oauth_applications?
37

38
    redirect_to profile_path
39 40
  end

41 42 43 44 45 46 47 48 49 50 51
  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
52 53 54 55 56 57 58
  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
59 60 61 62 63 64

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