Class: Users::ApproveService

Inherits:
BaseService show all
Defined in:
app/services/users/approve_service.rb

Instance Attribute Summary

Attributes inherited from BaseService

#params, #project

Instance Method Summary collapse

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) ⇒ ApproveService

Returns a new instance of ApproveService.



5
6
7
# File 'app/services/users/approve_service.rb', line 5

def initialize(current_user)
  @current_user = current_user
end

Instance Method Details

#execute(user) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/services/users/approve_service.rb', line 9

def execute(user)
  return error(_('You are not allowed to approve a user'), :forbidden) unless allowed?
  return error(_('The user you are trying to approve is not pending approval'), :conflict) if user.active? || !approval_required?(user)

  if user.activate
    # Resends confirmation email if the user isn't confirmed yet.
    # Please see Devise's implementation of `resend_confirmation_instructions` for detail.
    user.resend_confirmation_instructions
    user.accept_pending_invitations! if user.active_for_authentication?
    DeviseMailer.user_admin_approval(user).deliver_later

    if user.created_by_id
      reset_token = user.generate_reset_token
      NotificationService.new.new_user(user, reset_token)
    end

    log_event(user)
    after_approve_hook(user)
    success(message: 'Success', http_status: :created)
  else
    error(user.errors.full_messages.uniq.join('. '), :unprocessable_entity)
  end
end