diff --git a/app/controllers/admin/base_controller.rb b/app/controllers/admin/base_controller.rb index 968a0c2..678b1f9 100644 --- a/app/controllers/admin/base_controller.rb +++ b/app/controllers/admin/base_controller.rb @@ -1,26 +1,31 @@ class Admin::BaseController < ApplicationController - before_action :require_admin + before_action :require_admin, except: [:login] # GET /admin # POST /admin def index @client_ids = Client.pluck(:ClientId) get_charts render 'admin/index' end + # GET /admin/login + def login + render 'admin/login' + end + protected def get_charts days_ago = params.fetch(:days_back, 7).to_i rescue 7 @job_status = ChartGenerator.job_statuses(@client_ids, days_ago) @job_stats = ChartGenerator.job_stats(@client_ids, days_ago - 1) end def require_admin return if current_user.try(:admin?) flash[:alert] = 'You need to log in first' - redirect_to root_path + redirect_to admin_login_path end end diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index f8f1ed9..81ca1e0 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,78 +1,92 @@ class ApplicationController < ActionController::Base # Prevent CSRF attacks by raising an exception. # For APIs, you may want to use :null_session instead. protect_from_forgery with: :exception helper_method :current_user, :warden def index redirect_to clients_path if current_user end def unauthenticated flash[:error] = warden.message - redirect_to root_path + if attempted_path == '/grnet' + redirect_to admin_login_path + else + redirect_to root_path + end end - # POST /login - def login - if params[:admin] == 'admin' - warden.authenticate(:admin) - current_user + # POST /grnet + def grnet + if current_user + warden.logout + reset_current_user + end + begin + warden.authenticate!(:admin) + rescue + return unauthenticated end + current_user redirect_to admin_path end # POST /vima def vima begin warden.authenticate!(:vima) rescue return unauthenticated end current_user redirect_to clients_path end def logout warden.logout reset_current_user redirect_to root_path end protected def warden request.env['warden'] end def current_user @current_user ||= warden.user end def reset_current_user @current_user = nil end def fetch_logs days_ago = params.fetch(:days_back, 7).to_i rescue 7 if @client @logs = Log.includes(:job).joins(job: :client).where(Client: { ClientId: @client.id }) else @logs = Log.includes(:job).joins(job: { client: { host: :users } }). where(users: { id: current_user.id }) end @logs = @logs.where('Time > ?', days_ago.days.ago). order(Time: :desc, LogId: :desc).page(params[:page]) end private def require_logged_in return if current_user flash[:alert] = 'You need to log in first' redirect_to root_path end + + def attempted_path + (request.env['warden.options'] || {})[:attempted_path] + end end diff --git a/app/views/admin/login.html.erb b/app/views/admin/login.html.erb new file mode 100644 index 0000000..1adf61a --- /dev/null +++ b/app/views/admin/login.html.erb @@ -0,0 +1,25 @@ +