Commit c184e2ee authored by Peter Leitzen's avatar Peter Leitzen

Merge branch 'refactor-ee-app-services' into 'master'

Refactor EE app services

See merge request gitlab-org/gitlab!30425
parents 0df5ceb0 cbe5c813
...@@ -11,3 +11,5 @@ module Emails ...@@ -11,3 +11,5 @@ module Emails
end end
end end
end end
Emails::BaseService.prepend_if_ee('::EE::Emails::BaseService')
---
title: Move prepend to last line in ee/services
merge_request: 30425
author: Rajendra Kadam
type: fixed
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
module EE module EE
module ConfirmationsController module ConfirmationsController
include EE::Audit::Changes # rubocop: disable Cop/InjectEnterpriseEditionModule include ::Audit::Changes
extend ::Gitlab::Utils::Override extend ::Gitlab::Utils::Override
protected protected
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
module ApprovalRules module ApprovalRules
module Updater module Updater
include EE::Audit::Changes # rubocop: disable Cop/InjectEnterpriseEditionModule include Audit::Changes
def action def action
filter_eligible_users! filter_eligible_users!
......
...@@ -4,7 +4,6 @@ module EE ...@@ -4,7 +4,6 @@ module EE
module Emails module Emails
module CreateService module CreateService
extend ::Gitlab::Utils::Override extend ::Gitlab::Utils::Override
include ::EE::Emails::BaseService # rubocop: disable Cop/InjectEnterpriseEditionModule
override :execute override :execute
def execute(extra_params = {}) def execute(extra_params = {})
......
...@@ -4,7 +4,6 @@ module EE ...@@ -4,7 +4,6 @@ module EE
module Emails module Emails
module DestroyService module DestroyService
extend ::Gitlab::Utils::Override extend ::Gitlab::Utils::Override
include ::EE::Emails::BaseService # rubocop: disable Cop/InjectEnterpriseEditionModule
override :execute override :execute
def execute(email) def execute(email)
......
...@@ -4,8 +4,8 @@ module EE ...@@ -4,8 +4,8 @@ module EE
module Users module Users
module UpdateService module UpdateService
extend ::Gitlab::Utils::Override extend ::Gitlab::Utils::Override
include EE::Audit::Changes # rubocop: disable Cop/InjectEnterpriseEditionModule
include ::Gitlab::Utils::StrongMemoize include ::Gitlab::Utils::StrongMemoize
include Audit::Changes
attr_reader :group_id_for_saml attr_reader :group_id_for_saml
......
# frozen_string_literal: true
module Audit
module Changes
# Records an audit event in DB for model changes
#
# @param [Symbol] column column name to be audited
# @param [Hash] options the options to create an event with
# @option options [Symbol] :column column name to be audited
# @option options [User, Project, Group] :target_model scope the event belongs to
# @option options [Object] :model object being audited
# @option options [Boolean] :skip_changes whether to record from/to values
#
# @return [SecurityEvent, nil] the resulting object or nil if there is no
# change detected
def audit_changes(column, options = {})
column = options[:column] || column
# rubocop:disable Gitlab/ModuleWithInstanceVariables
@entity = options[:entity]
@model = options[:model]
# rubocop:enable Gitlab/ModuleWithInstanceVariables
return unless audit_required?(column)
audit_event(parse_options(column, options))
end
protected
def entity
@entity || model # rubocop:disable Gitlab/ModuleWithInstanceVariables
end
def model
@model
end
private
def audit_required?(column)
not_recently_created? && changed?(column)
end
def not_recently_created?
!model.previous_changes.has_key?(:id)
end
def changed?(column)
model.previous_changes.has_key?(column)
end
def changes(column)
model.previous_changes[column]
end
def parse_options(column, options)
options.tap do |options_hash|
options_hash[:column] = column
options_hash[:action] = :update
unless options[:skip_changes]
options_hash[:from] = changes(column).first
options_hash[:to] = changes(column).last
end
end
end
def audit_event(options)
::AuditEventService.new(@current_user, entity, options) # rubocop:disable Gitlab/ModuleWithInstanceVariables
.for_changes(model).security_event
end
end
end
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
module EE module EE
module Audit module Audit
class BaseChangesAuditor class BaseChangesAuditor
include Changes include ::Audit::Changes
def initialize(current_user, model) def initialize(current_user, model)
@model = model @model = model
......
# frozen_string_literal: true
module EE
module Audit
module Changes
# Records an audit event in DB for model changes
#
# @param [Symbol] column column name to be audited
# @param [Hash] options the options to create an event with
# @option options [Symbol] :column column name to be audited
# @option options [User, Project, Group] :target_model scope the event belongs to
# @option options [Object] :model object being audited
# @option options [Boolean] :skip_changes whether to record from/to values
#
# @return [SecurityEvent, nil] the resulting object or nil if there is no
# change detected
def audit_changes(column, options = {})
column = options[:column] || column
# rubocop:disable Gitlab/ModuleWithInstanceVariables
@entity = options[:entity]
@model = options[:model]
# rubocop:enable Gitlab/ModuleWithInstanceVariables
return unless audit_required?(column)
audit_event(parse_options(column, options))
end
protected
def entity
@entity || model # rubocop:disable Gitlab/ModuleWithInstanceVariables
end
def model
@model
end
private
def audit_required?(column)
not_recently_created? && changed?(column)
end
def not_recently_created?
!model.previous_changes.has_key?(:id)
end
def changed?(column)
model.previous_changes.has_key?(column)
end
def changes(column)
model.previous_changes[column]
end
def parse_options(column, options)
options.tap do |options_hash|
options_hash[:column] = column
options_hash[:action] = :update
unless options[:skip_changes]
options_hash[:from] = changes(column).first
options_hash[:to] = changes(column).last
end
end
end
def audit_event(options)
::AuditEventService.new(@current_user, entity, options) # rubocop:disable Gitlab/ModuleWithInstanceVariables
.for_changes(model).security_event
end
end
end
end
...@@ -2,8 +2,8 @@ ...@@ -2,8 +2,8 @@
require 'spec_helper' require 'spec_helper'
describe EE::Audit::Changes do describe Audit::Changes do
subject(:foo_instance) { Class.new { include EE::Audit::Changes }.new } subject(:foo_instance) { Class.new { include Audit::Changes }.new }
describe '.audit_changes' do describe '.audit_changes' do
let(:current_user) { create(:user, name: 'Mickey Mouse') } let(:current_user) { create(:user, name: 'Mickey Mouse') }
......
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