Commit 0ee002c7 authored by James Lopez's avatar James Lopez

more refactoring

parent 4c75fe5d
...@@ -2,9 +2,7 @@ class Profiles::AvatarsController < Profiles::ApplicationController ...@@ -2,9 +2,7 @@ class Profiles::AvatarsController < Profiles::ApplicationController
def destroy def destroy
@user = current_user @user = current_user
Users::UpdateService.new(@user, @user).execute do |user| Users::UpdateService.new(@user, @user).execute { |user| user.remove_avatar! }
user.remove_avatar!
end
redirect_to profile_path, status: 302 redirect_to profile_path, status: 302
end end
......
...@@ -5,9 +5,9 @@ class Profiles::EmailsController < Profiles::ApplicationController ...@@ -5,9 +5,9 @@ class Profiles::EmailsController < Profiles::ApplicationController
end end
def create def create
@email = current_user.emails.new(email_params) @email = Emails::CreateService.new(current_user, current_user, email_params).execute
if Emails::CreateService.new(current_user, current_user, email_params).execute if @email.errors.blank?
NotificationService.new.new_email(@email) NotificationService.new.new_email(@email)
else else
flash[:alert] = @email.errors.full_messages.first flash[:alert] = @email.errors.full_messages.first
...@@ -18,6 +18,7 @@ class Profiles::EmailsController < Profiles::ApplicationController ...@@ -18,6 +18,7 @@ class Profiles::EmailsController < Profiles::ApplicationController
def destroy def destroy
@email = current_user.emails.find(params[:id]) @email = current_user.emails.find(params[:id])
Emails::DestroyService.new(current_user, current_user, email: @email.email).execute Emails::DestroyService.new(current_user, current_user, email: @email.email).execute
respond_to do |format| respond_to do |format|
......
...@@ -41,8 +41,7 @@ class Profiles::TwoFactorAuthsController < Profiles::ApplicationController ...@@ -41,8 +41,7 @@ class Profiles::TwoFactorAuthsController < Profiles::ApplicationController
def create def create
if current_user.validate_and_consume_otp!(params[:pin_code]) if current_user.validate_and_consume_otp!(params[:pin_code])
Users::UpdateService.new(current_user, current_user).execute! do |user| Users::UpdateService.new(current_user, current_user, otp_required_for_login: true).execute! do |user|
user.otp_required_for_login = true
@codes = user.generate_otp_backup_codes! @codes = user.generate_otp_backup_codes!
end end
......
...@@ -17,10 +17,12 @@ module Users ...@@ -17,10 +17,12 @@ module Users
end end
end end
def execute!(skip_authorization: false, &block) def execute!(*args, &block)
result = execute(*args, &block) result = execute(*args, &block)
raise SomeCustomException(result[:message]) unless result[:status] == :success raise ActiveRecord::RecordInvalid(result[:message]) unless result[:status] == :success
true
end end
private private
......
...@@ -17,15 +17,5 @@ describe Emails::CreateService, services: true do ...@@ -17,15 +17,5 @@ describe Emails::CreateService, services: true do
expect(user.emails).to eq(Email.where(opts)) expect(user.emails).to eq(Email.where(opts))
end end
it 'does not create an email if the user has no permissions' do
expect { described_class.new(create(:user), user, opts).execute }.to raise_error(Gitlab::Access::AccessDeniedError)
end
it 'creates an email if we skip authorization' do
expect do
described_class.new(create(:user), user, opts).execute(skip_authorization: true)
end.to change { Email.count }.by(1)
end
end end
end end
...@@ -10,17 +10,5 @@ describe Emails::DestroyService, services: true do ...@@ -10,17 +10,5 @@ describe Emails::DestroyService, services: true do
it 'removes an email' do it 'removes an email' do
expect { service.execute }.to change { user.emails.count }.by(-1) expect { service.execute }.to change { user.emails.count }.by(-1)
end end
it 'does not remove an email if the user has no permissions' do
expect do
described_class.new(create(:user), user, email: email.email).execute
end.to raise_error(Gitlab::Access::AccessDeniedError)
end
it 'removes an email if we skip authorization' do
expect do
described_class.new(create(:user), user, email: email.email).execute(skip_authorization: true)
end.to change { Email.count }.by(-1)
end
end end
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