Class: Users::UpdateService

Inherits:
BaseService show all
Includes:
NewUserNotifier
Defined in:
app/services/users/update_service.rb

Constant Summary collapse

ATTRS_REQUIRING_PASSWORD_CHECK =
%w[email].freeze

Instance Attribute Summary collapse

Attributes inherited from BaseService

#current_user, #params, #project

Instance Method Summary collapse

Methods included from NewUserNotifier

#notify_new_user

Methods included from BaseServiceUtility

#deny_visibility_level, #event_service, #log_error, #log_info, #notification_service, #system_hook_service, #todo_service, #visibility_level

Methods included from Gitlab::Allowable

#can?

Constructor Details

#initialize(current_user, params = {}) ⇒ UpdateService

Returns a new instance of UpdateService.


10
11
12
13
14
15
16
17
# File 'app/services/users/update_service.rb', line 10

def initialize(current_user, params = {})
  @current_user = current_user
  @validation_password = params.delete(:validation_password)
  @user = params.delete(:user)
  @status_params = params.delete(:status)
  @identity_params = params.slice(*identity_attributes)
  @params = params.dup
end

Instance Attribute Details

#identity_paramsObject (readonly)

Returns the value of attribute identity_params.


6
7
8
# File 'app/services/users/update_service.rb', line 6

def identity_params
  @identity_params
end

#userObject (readonly)

Returns the value of attribute user.


6
7
8
# File 'app/services/users/update_service.rb', line 6

def user
  @user
end

Instance Method Details

#execute(validate: true, check_password: false) {|@user| ... } ⇒ Object

Yields:


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/services/users/update_service.rb', line 19

def execute(validate: true, check_password: false, &block)
  yield(@user) if block_given?

  user_exists = @user.persisted?
  @user.user_detail # prevent assignment

  discard_read_only_attributes
  assign_attributes

  if check_password && require_password_check? && !@user.valid_password?(@validation_password)
    return error(s_("Profiles|Invalid password"))
  end

  assign_identity
  build_canonical_email

  if @user.save(validate: validate) && update_status
    notify_success(user_exists)
  else
    messages = @user.errors.full_messages + Array(@user.status&.errors&.full_messages)
    error(messages.uniq.join('. '))
  end
end

#execute!(*args, **kargs, &block) ⇒ Object

Raises:

  • (ActiveRecord::RecordInvalid)

43
44
45
46
47
48
49
# File 'app/services/users/update_service.rb', line 43

def execute!(*args, **kargs, &block)
  result = execute(*args, **kargs, &block)

  raise ActiveRecord::RecordInvalid, @user unless result[:status] == :success

  true
end