2019-09-26 08:06:00 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Admin::SessionsController < ApplicationController
|
|
|
|
include InternalRedirect
|
|
|
|
|
|
|
|
before_action :user_is_admin!
|
|
|
|
|
|
|
|
def new
|
2019-12-11 07:08:10 -05:00
|
|
|
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-09-26 08:06:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
if current_user_mode.enable_admin_mode!(password: params[:password])
|
2019-12-11 07:08:10 -05:00
|
|
|
redirect_to redirect_path, notice: _('Admin mode enabled')
|
2019-09-26 08:06:00 -04:00
|
|
|
else
|
2019-12-11 07:08:10 -05:00
|
|
|
flash.now[:alert] = _('Invalid login or password')
|
2019-09-26 08:06:00 -04:00
|
|
|
render :new
|
|
|
|
end
|
2019-12-11 07:08:10 -05:00
|
|
|
rescue Gitlab::Auth::CurrentUserMode::NotRequestedError
|
|
|
|
redirect_to new_admin_session_path, alert: _('Re-authentication period expired or never requested. Please try again')
|
2019-09-26 08:06:00 -04:00
|
|
|
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
|
2019-12-11 07:08:10 -05:00
|
|
|
|
|
|
|
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
|
2019-09-26 08:06:00 -04:00
|
|
|
end
|