From c459f7ef956861a4a15bf2eefd14825ffe74aa27 Mon Sep 17 00:00:00 2001 From: ismail Akbudak Date: Tue, 21 Jun 2016 09:46:44 +0300 Subject: [PATCH] add basci authentication concern and improve application controller content --- lib/cybele/app_builder.rb | 1 + .../app/controllers/application_controller.rb | 27 +++++++--- .../concerns/basic_authentication.rb | 18 +++++++ .../rails/responders_controller/controller.rb | 49 ++++++++++--------- 4 files changed, 66 insertions(+), 29 deletions(-) create mode 100644 templates/app/controllers/concerns/basic_authentication.rb diff --git a/lib/cybele/app_builder.rb b/lib/cybele/app_builder.rb index 1f0e0d0..ee470a8 100644 --- a/lib/cybele/app_builder.rb +++ b/lib/cybele/app_builder.rb @@ -44,6 +44,7 @@ def install_responder_gem copy_file 'lib/application_responder.rb', 'lib/application_responder.rb' remove_file 'app/controllers/application_controller.rb' copy_file 'app/controllers/application_controller.rb', 'app/controllers/application_controller.rb' + copy_file 'app/controllers/concerns/basic_authentication.rb', 'app/controllers/concerns/basic_authentication.rb' copy_file 'lib/templates/rails/responders_controller/controller.rb', 'lib/templates/rails/responders_controller/controller.rb' copy_file 'config/locales/responders.en.yml', 'config/locales/responders.en.yml' copy_file 'config/locales/responders.tr.yml', 'config/locales/responders.tr.yml' diff --git a/templates/app/controllers/application_controller.rb b/templates/app/controllers/application_controller.rb index bd3658d..84679e9 100644 --- a/templates/app/controllers/application_controller.rb +++ b/templates/app/controllers/application_controller.rb @@ -1,19 +1,32 @@ require 'application_responder' class ApplicationController < ActionController::Base + include BasicAuthentication + + rescue_from Exception, with: :server_error if Rails.env.production? or Rails.env.staging? + rescue_from ActiveRecord::RecordNotFound, with: :page_not_found if Rails.env.production? or Rails.env.staging? + rescue_from ActionController::RoutingError, with: :page_not_found if Rails.env.production? or Rails.env.staging? + self.responder = ApplicationResponder respond_to :html, :json - WillPaginate.per_page = 10 # Prevent CSRF attacks by raising an exception. # For APIs, you may want to use :null_session instead. protect_from_forgery with: :exception - def after_sign_in_path_for(resource_or_scope) - if current_user - super - else - hq_dashboard_index_path - end + def server_error(exception) + Rollbar.error "ApplicationController#server_error --exception: #{exception}" + render template: 'errors/internal_server_error', status: 500 + end + + def page_not_found + render template: 'errors/not_found', status: 404 end + + protected + + def set_user_time_zone + Time.zone = current_user.time_zone if student_signed_in? && current_student.time_zone.present? + end + end \ No newline at end of file diff --git a/templates/app/controllers/concerns/basic_authentication.rb b/templates/app/controllers/concerns/basic_authentication.rb new file mode 100644 index 0000000..f1080fe --- /dev/null +++ b/templates/app/controllers/concerns/basic_authentication.rb @@ -0,0 +1,18 @@ +module BasicAuthentication + extend ActiveSupport::Concern + + included do + before_filter :authenticate + end + + private + + def authenticate + if Rails.env.staging? and ENV['BASIC_AUTH_IS_ACTIVE'] == 'yes' + authenticate_or_request_with_http_basic do |username, password| + username == Settings.basic_auth.username && password == Settings.basic_auth.password + end + end + end + +end \ No newline at end of file diff --git a/templates/lib/templates/rails/responders_controller/controller.rb b/templates/lib/templates/rails/responders_controller/controller.rb index 97ce0dd..18a2167 100644 --- a/templates/lib/templates/rails/responders_controller/controller.rb +++ b/templates/lib/templates/rails/responders_controller/controller.rb @@ -1,51 +1,56 @@ # encoding: UTF-8 <% module_namespacing do -%> class <%= controller_class_name %>Controller < ApplicationController - before_action :<%= "set_#{singular_table_name}" %>, only: [:show, :edit, :update, :destroy] - +before_action :<%= "set_#{singular_table_name}" %>, only: [:show, :edit, :update, :destroy] +add_breadcrumb I18n.t('activerecord.models.<%= singular_table_name %>'), :<%= table_name %>_path <% unless options[:singleton] -%> - def index - @<%= table_name %> = <%= class_name %>.all.page(params[:page]) - respond_with(@<%= table_name %>) +def index + @search = <%= class_name %>.order(id: :desc).search(params[:q]) + @<%= table_name %> = @search.result(distinct: true).paginate(page: params[:page]) + respond_with(@<%= table_name %>) end <% end -%> def show + add_breadcrumb @<%= file_name %>.<%= attributes.first.name %>, <%= singular_table_name %>_path(@<%= file_name %>) respond_with(@<%= file_name %>) - end +end - def new +def new + add_breadcrumb t('tooltips.new'), new_<%= singular_table_name %>_path @<%= file_name %> = <%= orm_class.build(class_name) %> respond_with(@<%= file_name %>) - end +end - def edit - end +def edit + add_breadcrumb @<%= singular_table_name %>.id, <%= singular_table_name %>_path(@<%= singular_table_name %>) + add_breadcrumb t('tooltips.edit'), edit_<%= singular_table_name %>_path +end - def create - @<%= file_name %> = <%= orm_class.build(class_name, "#{singular_table_name}_params") %> +def create + @<%= file_name %> = <%= orm_class.build(class_name, "#{singular_table_name}_params") %> @<%= orm_instance.save %> respond_with(@<%= file_name %>) - end +end - def update - @<%= orm_instance.update_attributes("#{singular_table_name}_params") %> +def update + @<%= orm_instance.update("#{singular_table_name}_params") %> respond_with(@<%= file_name %>) - end +end - def destroy - @<%= orm_instance.destroy %> +def destroy + @<%= orm_instance.destroy %> respond_with(@<%= file_name %>) - end +end - private +private - def <%= "set_#{singular_table_name}" %> +def <%= "set_#{singular_table_name}" %> @<%= file_name %> = <%= orm_class.find(class_name, "params[:id]") %> end def <%= "#{singular_table_name}_params" %> params.require(<%= ":#{singular_table_name}" %>).permit(<%= attributes.map {|a| ":#{a.name}" }.sort.join(', ') %>) - end +end end <% end -%> \ No newline at end of file