2021-01-03 14:25:43 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Users
|
|
|
|
class ApproveService < BaseService
|
|
|
|
def initialize(current_user)
|
|
|
|
@current_user = current_user
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute(user)
|
2021-02-22 17:27:13 +05:30
|
|
|
return error(_('You are not allowed to approve a user'), :forbidden) unless allowed?
|
2021-03-11 19:13:27 +05:30
|
|
|
return error(_('The user you are trying to approve is not pending approval'), :conflict) if user.active? || !approval_required?(user)
|
2021-01-03 14:25:43 +05:30
|
|
|
|
|
|
|
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?
|
2021-01-29 00:20:46 +05:30
|
|
|
DeviseMailer.user_admin_approval(user).deliver_later
|
2021-01-03 14:25:43 +05:30
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
if user.created_by_id
|
|
|
|
reset_token = user.generate_reset_token
|
|
|
|
NotificationService.new.new_user(user, reset_token)
|
|
|
|
end
|
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
log_event(user)
|
2021-01-29 00:20:46 +05:30
|
|
|
after_approve_hook(user)
|
2021-02-22 17:27:13 +05:30
|
|
|
success(message: 'Success', http_status: :created)
|
2021-01-03 14:25:43 +05:30
|
|
|
else
|
2021-02-22 17:27:13 +05:30
|
|
|
error(user.errors.full_messages.uniq.join('. '), :unprocessable_entity)
|
2021-01-03 14:25:43 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
attr_reader :current_user
|
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
def after_approve_hook(user)
|
|
|
|
# overridden by EE module
|
|
|
|
end
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
def allowed?
|
|
|
|
can?(current_user, :approve_user)
|
|
|
|
end
|
|
|
|
|
|
|
|
def approval_required?(user)
|
|
|
|
user.blocked_pending_approval?
|
|
|
|
end
|
2021-03-11 19:13:27 +05:30
|
|
|
|
|
|
|
def log_event(user)
|
2023-03-04 22:38:38 +05:30
|
|
|
Gitlab::AppLogger.info(message: "User instance access request approved", user: user.username.to_s, email: user.email.to_s, approved_by: current_user.username.to_s, ip_address: current_user.current_sign_in_ip.to_s)
|
2021-03-11 19:13:27 +05:30
|
|
|
end
|
2021-01-03 14:25:43 +05:30
|
|
|
end
|
|
|
|
end
|
2021-01-29 00:20:46 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
Users::ApproveService.prepend_mod_with('Users::ApproveService')
|