Commit cbe5c813 authored by Rajendra Kadam's avatar Rajendra Kadam Committed by Peter Leitzen

Refactor EE app services

parent c3e08d1c
......@@ -11,3 +11,5 @@ module Emails
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 @@
module EE
module ConfirmationsController
include EE::Audit::Changes # rubocop: disable Cop/InjectEnterpriseEditionModule
include ::Audit::Changes
extend ::Gitlab::Utils::Override
protected
......
......@@ -2,7 +2,7 @@
module ApprovalRules
module Updater
include EE::Audit::Changes # rubocop: disable Cop/InjectEnterpriseEditionModule
include Audit::Changes
def action
filter_eligible_users!
......
......@@ -4,7 +4,6 @@ module EE
module Emails
module CreateService
extend ::Gitlab::Utils::Override
include ::EE::Emails::BaseService # rubocop: disable Cop/InjectEnterpriseEditionModule
override :execute
def execute(extra_params = {})
......
......@@ -4,7 +4,6 @@ module EE
module Emails
module DestroyService
extend ::Gitlab::Utils::Override
include ::EE::Emails::BaseService # rubocop: disable Cop/InjectEnterpriseEditionModule
override :execute
def execute(email)
......
......@@ -4,8 +4,8 @@ module EE
module Users
module UpdateService
extend ::Gitlab::Utils::Override
include EE::Audit::Changes # rubocop: disable Cop/InjectEnterpriseEditionModule
include ::Gitlab::Utils::StrongMemoize
include Audit::Changes
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 @@
module EE
module Audit
class BaseChangesAuditor
include Changes
include ::Audit::Changes
def initialize(current_user, 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 @@
require 'spec_helper'
describe EE::Audit::Changes do
subject(:foo_instance) { Class.new { include EE::Audit::Changes }.new }
describe Audit::Changes do
subject(:foo_instance) { Class.new { include Audit::Changes }.new }
describe '.audit_changes' do
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