Commit 354256d0 authored by Grzegorz Bizon's avatar Grzegorz Bizon
Browse files

Merge branch 'refactor-services-for-audit-events-ce' into 'master'

[CE] Refactor controller calls into services

Closes gitlab-ee#3544

See merge request gitlab-org/gitlab-ce!15023
......@@ -19,10 +19,12 @@ def edit
end
def create
@application = Doorkeeper::Application.new(application_params)
@application = Applications::CreateService.new(current_user, application_params).execute(request)
if @application.save
redirect_to_admin_page
if @application.persisted?
flash[:notice] = I18n.t(:notice, scope: [:doorkeeper, :flash, :applications, :create])
redirect_to admin_application_url(@application)
else
render :new
end
......@@ -41,13 +43,6 @@ def destroy
redirect_to admin_applications_url, status: 302, notice: 'Application was successfully destroyed.'
end
protected
def redirect_to_admin_page
flash[:notice] = I18n.t(:notice, scope: [:doorkeeper, :flash, :applications, :create])
redirect_to admin_application_url(@application)
end
private
def set_application
......
......@@ -16,25 +16,18 @@ def index
end
def create
@application = Doorkeeper::Application.new(application_params)
@application = Applications::CreateService.new(current_user, create_application_params).execute(request)
@application.owner = current_user
if @application.persisted?
flash[:notice] = I18n.t(:notice, scope: [:doorkeeper, :flash, :applications, :create])
if @application.save
redirect_to_oauth_application_page
redirect_to oauth_application_url(@application)
else
set_index_vars
render :index
end
end
protected
def redirect_to_oauth_application_page
flash[:notice] = I18n.t(:notice, scope: [:doorkeeper, :flash, :applications, :create])
redirect_to oauth_application_url(@application)
end
private
def verify_user_oauth_applications_enabled
......@@ -61,4 +54,10 @@ def set_application
rescue_from ActiveRecord::RecordNotFound do |exception|
render "errors/not_found", layout: "errors", status: 404
end
def create_application_params
application_params.tap do |params|
params[:owner] = current_user
end
end
end
......@@ -11,10 +11,10 @@ def show
end
def create
@key = Keys::CreateService.new(current_user, key_params).execute
@key = Keys::CreateService.new(current_user, key_params.merge(ip_address: request.remote_ip)).execute
if @key.persisted?
redirect_to_profile_key_path
redirect_to profile_key_path(@key)
else
@keys = current_user.keys.select(&:persisted?)
render :index
......@@ -50,12 +50,6 @@ def get_keys
end
end
protected
def redirect_to_profile_key_path
redirect_to profile_key_path(@key)
end
private
def key_params
......
module Applications
class CreateService
def initialize(current_user, params)
@current_user = current_user
@params = params
@ip_address = @params.delete(:ip_address)
end
def execute(request = nil)
Doorkeeper::Application.create(@params)
end
end
end
......@@ -4,6 +4,7 @@ class BaseService
def initialize(user, params)
@user, @params = user, params
@ip_address = @params.delete(:ip_address)
end
def notification_service
......
require 'spec_helper'
describe ::Applications::CreateService do
let(:user) { create(:user) }
let(:params) { attributes_for(:application) }
let(:request) { ActionController::TestRequest.new(remote_ip: '127.0.0.1') }
subject { described_class.new(user, params) }
it 'creates an application' do
expect { subject.execute(request) }.to change { Doorkeeper::Application.count }.by(1)
end
end
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment