diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 2adc47c..845f889 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,62 +1,66 @@ 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 redirect_to root_path end # POST /login def login if params[:admin] == 'admin' warden.authenticate(:admin) current_user end redirect_to admin_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 end diff --git a/app/controllers/clients_controller.rb b/app/controllers/clients_controller.rb index 273c25d..6c20cd0 100644 --- a/app/controllers/clients_controller.rb +++ b/app/controllers/clients_controller.rb @@ -1,49 +1,50 @@ class ClientsController < ApplicationController + before_action :require_logged_in before_action :set_client, only: [:show, :jobs, :logs, :stats] before_action :fetch_logs, only: [:logs] # GET /clients # POST /clients def index @client_ids = Client.for_user(current_user.id).pluck(:ClientId) @clients = Client.where(ClientId: @client_ids).includes(:jobs) @hosts = current_user.hosts.not_baculized fetch_jobs_info get_charts end # GET /clients/1 def show end # GET /clients/1/jobs def jobs @jobs = @client.recent_jobs.page(params[:page]) end # GET /clients/1/logs def logs; end # GET /clients/1/stats # POST /clients/1/stats def stats get_charts end private def set_client @client = Client.for_user(current_user.id).find(params[:id]) @client_ids = [@client.id] end def fetch_jobs_info @stats = JobStats.new(@client_ids) end 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 end diff --git a/app/controllers/filesets_controller.rb b/app/controllers/filesets_controller.rb index 9054e7e..bcb88c9 100644 --- a/app/controllers/filesets_controller.rb +++ b/app/controllers/filesets_controller.rb @@ -1,45 +1,46 @@ class FilesetsController < ApplicationController + before_action :require_logged_in before_action :fetch_host, only: [:new, :create] before_action :fetch_job_id, only: [:new, :create] def new @fileset = @host.filesets.new end def show end def create @fileset = @host.filesets.new(fetch_params) if @fileset.save flash[:success] = 'Fileset created' if @job_id.present? redirect_to edit_host_job_path(@host, @job_id, fileset_id: @fileset.id) else redirect_to new_host_job_path(@host, fileset_id: @fileset.id) end else @fileset.include_files = nil @fileset.exclude_directions = nil render :new end end def destroy end private def fetch_host @host = current_user.hosts.find(params[:host_id]) end def fetch_job_id @job_id = @host.job_templates.find(params[:job_id]).id if params[:job_id].present? end def fetch_params params.require(:fileset).permit(:name, exclude_directions: [], include_files: []) end end diff --git a/app/controllers/hosts_controller.rb b/app/controllers/hosts_controller.rb index 9bd7605..2a05c42 100644 --- a/app/controllers/hosts_controller.rb +++ b/app/controllers/hosts_controller.rb @@ -1,103 +1,104 @@ class HostsController < ApplicationController + before_action :require_logged_in before_action :fetch_host, only: [:show, :edit, :update, :destroy, :submit_config, :revoke, :restore, :run_restore] # GET /hosts/new def new @host = Host.new end # POST /hosts def create @host = Host.new(fetch_params) if @host.save flash[:success] = 'Host created successfully' current_user.hosts << @host redirect_to host_path @host else flash[:error] = 'Host was not created' render :new end end # GET /hosts/1 def show; end # GET /hosts/1/edit def edit; end # PATCH /hosts/1 def update updates = fetch_params.slice(:port, :password) if updates.present? && @host.update_attributes(updates) @host.recalculate flash[:success] = 'Host updated successfully. You must update your file deamon accordingly.' redirect_to host_path @host else render :edit end end # DELETE /hosts/1 def destroy if @host.destroy flash[:success] = 'Host destroyed successfully' else flash[:error] = 'Host not destroyed' end redirect_to root_path end # POST /hosts/1/submit_config def submit_config if @host.dispatch_to_bacula flash[:success] = 'Host configuration sent to Bacula successfully' else flash[:error] = 'Something went wrong, try again later' end redirect_to host_path(@host) end # DELETE /hosts/1/revoke def revoke if @host.remove_from_bacula flash[:success] = 'Host configuration removed from Bacula successfully' else flash[:error] = 'Something went wrong, try again later' end redirect_to root_path end # GET /hosts/1/restore def restore if !@host.restorable? flash[:error] = "Can not issue a restore for this client" redirect_to @host.client.present? ? client_path(@host.client) : root_path end end # POST /hosts/1/run_estore def run_restore location = params[:restore_location] if location.present? && @host.restore(location) flash[:success] = "Restore job issued successfully, files will be soon available in #{location}" else flash[:error] = 'Something went wrong, try again later' end redirect_to client_path(@host.client) end private def fetch_host @host = current_user.hosts.includes(job_templates: [:fileset, :schedule]).find(params[:id]) end def fetch_params params.require(:host).permit(:fqdn, :port, :password) end end diff --git a/app/controllers/jobs_controller.rb b/app/controllers/jobs_controller.rb index 22cf5e2..6074d2e 100644 --- a/app/controllers/jobs_controller.rb +++ b/app/controllers/jobs_controller.rb @@ -1,76 +1,77 @@ class JobsController < ApplicationController + before_action :require_logged_in before_action :fetch_host, only: [:new, :edit, :show, :create, :update, :toggle_enable, :backup_now] before_action :fetch_job, only: [:show, :edit, :update, :destroy, :toggle_enable, :backup_now] # GET /jobs def new @job = @host.job_templates.new end # POST /jobs def create @job = @host.job_templates.new(fetch_params) if @job.save flash[:success] = 'Job created successfully' redirect_to host_path(@host) else render :new end end # GET /jobs/1 def show; end # GET /jobs/1/edit def edit;end # PUT /jobs/1 def update if @job.update_attributes(fetch_params) flash[:success] = 'Job updated' redirect_to host_job_path(@host, @job) else render :edit end end # DELETE /jobs/1 def destroy end # PATCH /hosts/1/jobs/1/enable def toggle_enable @job.enabled = !@job.enabled @job.save flash[:success] = @job.enabled? ? 'Job enabled' : 'Job disabled' redirect_to host_path(@host) end # POST /hosts/1/jobs/1/backup_now def backup_now if @job.backup_now flash[:success] = 'Backup directive was sent to bacula. Backup will be taken in a while' else flash[:error] = 'Backup was not sent, try again later' end redirect_to client_path(@host.client) end private def fetch_job @job = @host.job_templates.find(params[:id]) end def fetch_host @host = current_user.hosts.find(params[:host_id]) end def fetch_params params.require(:job_template). permit(:name, :fileset_id, :schedule_id, :client_before_run_file, :client_after_run_file) end end diff --git a/app/controllers/schedules_controller.rb b/app/controllers/schedules_controller.rb index 4824a49..04403d6 100644 --- a/app/controllers/schedules_controller.rb +++ b/app/controllers/schedules_controller.rb @@ -1,52 +1,53 @@ class SchedulesController < ApplicationController + before_action :require_logged_in before_action :fetch_host, only: [:new, :create] before_action :fetch_job_id, only: [:new, :create] def new @schedule = @host.schedules.new end def show end def edit end def update end def create @schedule = @host.schedules.new(fetch_params) @schedule.runtime = params[:schedule][:runtime] if params[:schedule][:runtime] if @schedule.save flash[:success] = 'Schedule created successfully' if @job_id.present? redirect_to edit_host_job_path(@host, @job_id, schedule_id: @schedule.id) else redirect_to new_host_job_path(@host, schedule_id: @schedule.id) end else render :new end end def destroy end private def fetch_host @host = current_user.hosts.find(params[:host_id]) end def fetch_job_id @job_id = @host.job_templates.find(params[:job_id]).id if params[:job_id].present? end def fetch_params params.require(:schedule). permit(:name, { schedule_runs_attributes: [[:level, :month, :day, :time]] }) end end diff --git a/app/views/application/index.html.erb b/app/views/application/index.html.erb new file mode 100644 index 0000000..06b971b --- /dev/null +++ b/app/views/application/index.html.erb @@ -0,0 +1,2 @@ +<%= link_to 'admin', login_path(admin: 'admin'), method: :post, role: :button, + class: 'btn btn-primary' %> diff --git a/app/views/shared/_nav.html.erb b/app/views/shared/_nav.html.erb index 776cf87..1129172 100644 --- a/app/views/shared/_nav.html.erb +++ b/app/views/shared/_nav.html.erb @@ -1,51 +1,53 @@