-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add basci authentication concern and improve application controller c…
…ontent
- Loading branch information
ismail Akbudak
committed
Jun 21, 2016
1 parent
0da5a11
commit c459f7e
Showing
4 changed files
with
66 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
18
templates/app/controllers/concerns/basic_authentication.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
49
templates/lib/templates/rails/responders_controller/controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 -%> |