Class: Users::SetNamespaceCommitEmailService

Inherits:
Object
  • Object
show all
Includes:
Gitlab::Allowable
Defined in:
app/services/users/set_namespace_commit_email_service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Gitlab::Allowable

#can?

Constructor Details

#initialize(current_user, namespace, email_id, params) ⇒ SetNamespaceCommitEmailService

Returns a new instance of SetNamespaceCommitEmailService.



9
10
11
12
13
14
# File 'app/services/users/set_namespace_commit_email_service.rb', line 9

def initialize(current_user, namespace, email_id, params)
  @current_user = current_user
  @target_user = params.delete(:user) || current_user
  @namespace = namespace
  @email_id = email_id
end

Instance Attribute Details

#current_userObject (readonly)

Returns the value of attribute current_user.



7
8
9
# File 'app/services/users/set_namespace_commit_email_service.rb', line 7

def current_user
  @current_user
end

#email_idObject (readonly)

Returns the value of attribute email_id.



7
8
9
# File 'app/services/users/set_namespace_commit_email_service.rb', line 7

def email_id
  @email_id
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



7
8
9
# File 'app/services/users/set_namespace_commit_email_service.rb', line 7

def namespace
  @namespace
end

#target_userObject (readonly)

Returns the value of attribute target_user.



7
8
9
# File 'app/services/users/set_namespace_commit_email_service.rb', line 7

def target_user
  @target_user
end

Instance Method Details

#executeObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/services/users/set_namespace_commit_email_service.rb', line 16

def execute
  return error(_('Namespace must be provided.')) if namespace.nil?

  unless can?(current_user, :admin_user_email_address, target_user)
    return error(_("User doesn't exist or you don't have permission to change namespace commit emails."))
  end

  unless can?(target_user, :read_namespace_via_membership, namespace)
    return error(_("Namespace doesn't exist or you don't have permission."))
  end

  email = target_user.emails.find_by(id: email_id) unless email_id.nil? # rubocop: disable CodeReuse/ActiveRecord
  existing_namespace_commit_email = target_user.namespace_commit_email_for_namespace(namespace)
  if existing_namespace_commit_email.nil?
    return error(_('Email must be provided.')) if email.nil?

    create_namespace_commit_email(email)
  elsif email_id.nil?
    remove_namespace_commit_email(existing_namespace_commit_email)
  else
    update_namespace_commit_email(existing_namespace_commit_email, email)
  end
end