diff --git a/app/controllers/hosts_controller.rb b/app/controllers/hosts_controller.rb index e2aa742..d3dee99 100644 --- a/app/controllers/hosts_controller.rb +++ b/app/controllers/hosts_controller.rb @@ -1,113 +1,114 @@ class HostsController < ApplicationController before_action :require_logged_in before_action :fetch_host, only: [:show, :edit, :update, :destroy, :submit_config, :revoke, :disable] before_action :fetch_hosts_of_user, only: [:new, :edit, :create] # GET /hosts/new def new @host = Host.new @host.port = 9102 end # POST /hosts def create @host = Host.new(fetch_params) @host.verified = current_user.needs_host_list? if user_can_add_this_host? && @host.save flash[:success] = 'Host created successfully' current_user.hosts << @host + UserMailer.notify_admin(current_user, @host.fqdn).deliver redirect_to host_path @host else flash[:error] = 'Host was not created' render :new end end # GET /hosts/1 def show @schedules = @host.job_templates.map(&:schedule) @filesets = @host.job_templates.map(&:fileset) 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 if @host.bacula_ready? 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/disable def disable if @host.disable_jobs_and_update flash[:success] = 'Client disabled' else flash[:error] = 'Something went wrong, try again later' end redirect_to host_path(@host) 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 private def fetch_hosts_of_user return if not current_user.needs_host_list? @hosts_of_user = session[:vms] - current_user.hosts.pluck(:fqdn) end 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 def user_can_add_this_host? !current_user.needs_host_list? || @hosts_of_user.include?(@host.fqdn) end end diff --git a/app/mailers/user_mailer.rb b/app/mailers/user_mailer.rb index 0102de0..bd51c31 100644 --- a/app/mailers/user_mailer.rb +++ b/app/mailers/user_mailer.rb @@ -1,14 +1,24 @@ class UserMailer < ActionMailer::Base default from: Archiving.settings[:default_sender] # Notifies the host's owners that the host has been verified by an admin # and is now ready to be configured # # @param user_emails[Array] the owners' emails # @param host[String] the host's FQDN def notify_for_verification(user_emails, host) @host = host s = "[Archiving] Host #{host} verification" mail(to: user_emails, subject: s) end + + # Notifies admin about a new host that needs verification + # + # @param user[User] the user that created the host + # @param host[String] the host's FQDN + def notify_admin(user, host) + @user = user + @host = host + mail(to: Archiving.settings[:admin_email], subject: 'New host pending verification') + end end diff --git a/app/views/user_mailer/notify_admin.text.erb b/app/views/user_mailer/notify_admin.text.erb new file mode 100644 index 0000000..96f84fe --- /dev/null +++ b/app/views/user_mailer/notify_admin.text.erb @@ -0,0 +1,9 @@ +New Host pending admin approval. + +user: <%= @user.username %> <%= admin_user_url(@user.id) %> +host <%= @host %> + +<%= unverified_admin_hosts_url %> + +--------- +Archiving diff --git a/config/environments/development.rb b/config/environments/development.rb index 9911314..d722fda 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -1,41 +1,43 @@ Rails.application.configure do # Settings specified here will take precedence over those in config/application.rb. # In the development environment your application's code is reloaded on # every request. This slows down response time but is perfect for development # since you don't have to restart the web server when you make code changes. config.cache_classes = false # Do not eager load code on boot. config.eager_load = false # Show full error reports and disable caching. config.consider_all_requests_local = true config.action_controller.perform_caching = false # Don't care if the mailer can't send. config.action_mailer.raise_delivery_errors = false # Print deprecation notices to the Rails logger. config.active_support.deprecation = :log # Raise an error on page load if there are pending migrations. config.active_record.migration_error = :page_load # Debug mode disables concatenation and preprocessing of assets. # This option may cause significant delays in view rendering with a large # number of complex assets. config.assets.debug = true # Adds additional error checking when serving assets at runtime. # Checks for improperly declared sprockets dependencies. # Raises helpful error messages. config.assets.raise_runtime_errors = true # Raises error for missing translations # config.action_view.raise_on_missing_translations = true config.action_mailer.delivery_method = :smtp config.action_mailer.smtp_settings = YAML::load(File.open("#{Rails.root}/config/mailer.yml"))[Rails.env].symbolize_keys + + routes.default_url_options = { host: '127.0.0.1', port: '3000' } end diff --git a/config/environments/production.rb b/config/environments/production.rb index a24c97d..3c59f0a 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -1,82 +1,84 @@ Rails.application.configure do # Settings specified here will take precedence over those in config/application.rb. # Code is not reloaded between requests. config.cache_classes = true # Eager load code on boot. This eager loads most of Rails and # your application in memory, allowing both threaded web servers # and those relying on copy on write to perform better. # Rake tasks automatically ignore this option for performance. config.eager_load = true # Full error reports are disabled and caching is turned on. config.consider_all_requests_local = true config.action_controller.perform_caching = true # Enable Rack::Cache to put a simple HTTP cache in front of your application # Add `rack-cache` to your Gemfile before enabling this. # For large-scale production use, consider using a caching reverse proxy like nginx, varnish or squid. # config.action_dispatch.rack_cache = true # Disable Rails's static asset server (Apache or nginx will already do this). config.serve_static_assets = false # Compress JavaScripts and CSS. config.assets.js_compressor = :uglifier # config.assets.css_compressor = :sass # Do not fallback to assets pipeline if a precompiled asset is missed. config.assets.compile = false # Generate digests for assets URLs. config.assets.digest = true # `config.assets.precompile` and `config.assets.version` have moved to config/initializers/assets.rb # Specifies the header that your server uses for sending files. # config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies. # config.force_ssl = true # Set to :debug to see everything in the log. config.log_level = :info # Prepend all log lines with the following tags. # config.log_tags = [ :subdomain, :uuid ] # Use a different logger for distributed setups. # config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new) # Use a different cache store in production. # config.cache_store = :mem_cache_store # Enable serving of images, stylesheets, and JavaScripts from an asset server. # config.action_controller.asset_host = "http://assets.example.com" # Ignore bad email addresses and do not raise email delivery errors. # Set this to true and configure the email server for immediate delivery to raise delivery errors. # config.action_mailer.raise_delivery_errors = false # Enable locale fallbacks for I18n (makes lookups for any locale fall back to # the I18n.default_locale when a translation cannot be found). config.i18n.fallbacks = true # Send deprecation notices to registered listeners. config.active_support.deprecation = :notify # Disable automatic flushing of the log to improve performance. # config.autoflush_log = false # Use default logging formatter so that PID and timestamp are not suppressed. config.log_formatter = ::Logger::Formatter.new # Do not dump schema after migrations. config.active_record.dump_schema_after_migration = false config.action_mailer.delivery_method = :test config.action_mailer.smtp_settings = YAML::load(File.open("#{Rails.root}/config/mailer.yml"))[Rails.env].symbolize_keys + + routes.default_url_options = { host: 'archiving.grnet.gr' } end diff --git a/config/environments/test.rb b/config/environments/test.rb index 053f5b6..47fefbb 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -1,39 +1,40 @@ Rails.application.configure do # Settings specified here will take precedence over those in config/application.rb. # The test environment is used exclusively to run your application's # test suite. You never need to work with it otherwise. Remember that # your test database is "scratch space" for the test suite and is wiped # and recreated between test runs. Don't rely on the data there! config.cache_classes = true # Do not eager load code on boot. This avoids loading your whole application # just for the purpose of running a single test. If you are using a tool that # preloads Rails for running tests, you may have to set it to true. config.eager_load = false # Configure static asset server for tests with Cache-Control for performance. config.serve_static_assets = true config.static_cache_control = 'public, max-age=3600' # Show full error reports and disable caching. config.consider_all_requests_local = true config.action_controller.perform_caching = false # Raise exceptions instead of rendering exception templates. config.action_dispatch.show_exceptions = false # Disable request forgery protection in test environment. config.action_controller.allow_forgery_protection = false # Tell Action Mailer not to deliver emails to the real world. # The :test delivery method accumulates sent emails in the # ActionMailer::Base.deliveries array. config.action_mailer.delivery_method = :test # Print deprecation notices to the stderr. config.active_support.deprecation = :stderr # Raises error for missing translations # config.action_view.raise_on_missing_translations = true + routes.default_url_options = { host: '127.0.1.1' } end diff --git a/config/initializers/00_settings.rb b/config/initializers/00_settings.rb index 7f889dd..d0ff3be 100644 --- a/config/initializers/00_settings.rb +++ b/config/initializers/00_settings.rb @@ -1,8 +1,9 @@ Archiving.settings director_name: YAML.load_file(Rails.root.join('config', 'bacula.yml'))[Rails.env]. symbolize_keys[:director] Archiving.settings vima_oauth_enabled: true Archiving.settings institutional_authentication_enabled: true Archiving.settings okeanos_authentication_enabled: false Archiving.settings default_sender: 'admin@archiving.grnet.gr' +Archiving.settings admin_email: 'admin@archiving.grnet.gr'