2021-08-02 11:08:56 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Users
|
|
|
|
class BannedUserBaseService < BaseService
|
|
|
|
def initialize(current_user)
|
|
|
|
@current_user = current_user
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute(user)
|
|
|
|
return permission_error unless allowed?
|
2021-08-26 05:11:15 -04:00
|
|
|
return state_error(user) unless valid_state?(user)
|
2021-08-02 11:08:56 -04:00
|
|
|
|
|
|
|
if update_user(user)
|
|
|
|
log_event(user)
|
|
|
|
success
|
|
|
|
else
|
|
|
|
messages = user.errors.full_messages
|
|
|
|
error(messages.uniq.join('. '))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
attr_reader :current_user
|
|
|
|
|
2021-08-26 05:11:15 -04:00
|
|
|
def state_error(user)
|
|
|
|
error(_("You cannot %{action} %{state} users." % { action: action.to_s, state: user.state }), :forbidden)
|
|
|
|
end
|
|
|
|
|
2021-08-02 11:08:56 -04:00
|
|
|
def allowed?
|
|
|
|
can?(current_user, :admin_all_resources)
|
|
|
|
end
|
|
|
|
|
|
|
|
def permission_error
|
|
|
|
error(_("You are not allowed to %{action} a user" % { action: action.to_s }), :forbidden)
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_event(user)
|
|
|
|
Gitlab::AppLogger.info(message: "User #{action}", user: "#{user.username}", email: "#{user.email}", "#{action}_by": "#{current_user.username}", ip_address: "#{current_user.current_sign_in_ip}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|