debian-mirror-gitlab/app/controllers/admin/sessions_controller.rb

79 lines
2.2 KiB
Ruby
Raw Normal View History

2019-12-21 20:55:43 +05:30
# frozen_string_literal: true
class Admin::SessionsController < ApplicationController
2021-01-03 14:25:43 +05:30
include AuthenticatesWithTwoFactorForAdminMode
2019-12-21 20:55:43 +05:30
include InternalRedirect
2020-05-24 23:13:21 +05:30
include RendersLdapServers
2019-12-21 20:55:43 +05:30
before_action :user_is_admin!
2021-01-03 14:25:43 +05:30
feature_category :authentication_and_authorization
2019-12-21 20:55:43 +05:30
def new
2020-01-01 13:55:28 +05:30
if current_user_mode.admin_mode?
redirect_to redirect_path, notice: _('Admin mode already enabled')
else
current_user_mode.request_admin_mode! unless current_user_mode.admin_mode_requested?
store_location_for(:redirect, redirect_path)
end
2019-12-21 20:55:43 +05:30
end
def create
2020-04-08 14:13:33 +05:30
if two_factor_enabled_for_user?
admin_mode_authenticate_with_two_factor
elsif current_user_mode.enable_admin_mode!(password: user_params[:password])
2020-01-01 13:55:28 +05:30
redirect_to redirect_path, notice: _('Admin mode enabled')
2019-12-21 20:55:43 +05:30
else
2020-01-01 13:55:28 +05:30
flash.now[:alert] = _('Invalid login or password')
2019-12-21 20:55:43 +05:30
render :new
end
2020-01-01 13:55:28 +05:30
rescue Gitlab::Auth::CurrentUserMode::NotRequestedError
redirect_to new_admin_session_path, alert: _('Re-authentication period expired or never requested. Please try again')
2019-12-21 20:55:43 +05:30
end
def destroy
current_user_mode.disable_admin_mode!
redirect_to root_path, status: :found, notice: _('Admin mode disabled')
end
private
def user_is_admin!
render_404 unless current_user&.admin?
end
2020-01-01 13:55:28 +05:30
2020-04-08 14:13:33 +05:30
def two_factor_enabled_for_user?
current_user&.two_factor_enabled?
end
2020-01-01 13:55:28 +05:30
def redirect_path
redirect_to_path = safe_redirect_path(stored_location_for(:redirect)) || safe_redirect_path_for_url(request.referer)
if redirect_to_path &&
excluded_redirect_paths.none? { |excluded| redirect_to_path.include?(excluded) }
redirect_to_path
else
admin_root_path
end
end
def excluded_redirect_paths
[new_admin_session_path, admin_session_path]
end
2020-04-08 14:13:33 +05:30
def user_params
params.fetch(:user, {}).permit(:password, :otp_attempt, :device_response)
end
def valid_otp_attempt?(user)
2021-01-03 14:25:43 +05:30
otp_validation_result =
::Users::ValidateOtpService.new(user).execute(user_params[:otp_attempt])
valid_otp_attempt = otp_validation_result[:status] == :success
2020-04-22 19:07:51 +05:30
return valid_otp_attempt if Gitlab::Database.read_only?
valid_otp_attempt || user.invalidate_otp_backup_code!(user_params[:otp_attempt])
2020-04-08 14:13:33 +05:30
end
2019-12-21 20:55:43 +05:30
end