Skip to content

Commit

Permalink
add basci authentication concern and improve application controller c…
Browse files Browse the repository at this point in the history
…ontent
  • Loading branch information
ismail Akbudak committed Jun 21, 2016
1 parent 0da5a11 commit c459f7e
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 29 deletions.
1 change: 1 addition & 0 deletions lib/cybele/app_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
27 changes: 20 additions & 7 deletions templates/app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -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
18 changes: 18 additions & 0 deletions templates/app/controllers/concerns/basic_authentication.rb
Original file line number Diff line number Diff line change
@@ -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
49 changes: 27 additions & 22 deletions templates/lib/templates/rails/responders_controller/controller.rb
Original file line number Diff line number Diff line change
@@ -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 -%>

0 comments on commit c459f7e

Please sign in to comment.