Skip to content

Instantly share code, notes, and snippets.

@bsodmike
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bsodmike/7f7d130b0c38964dfabd to your computer and use it in GitHub Desktop.
Save bsodmike/7f7d130b0c38964dfabd to your computer and use it in GitHub Desktop.
Initialise Authentication via Rails Engine
require 'initialise_authentication'
module Refinery
module AuthenticateIdentities
class Engine < Rails::Engine
extend Refinery::Engine
isolate_namespace Refinery::AuthenticateIdentities
engine_name :refinery_authenticate_identities
before_inclusion do
Refinery::Plugin.register do |plugin|
plugin.name = "authenticate_identities"
plugin.url = proc { Refinery::Core::Engine.routes.url_helpers.authenticate_identities_admin_authenticate_identities_path }
plugin.pathname = root
plugin.activity = {
:class_name => :'refinery/authenticate_identities/authenticate_identity',
:title => 'name'
}
plugin.hide_from_menu = true
end
end
config.before_initialize do
# NOTE Secure frontend access with Omniauth Identity strategy
::Authentication.secure_frontend
end
config.after_initialize do
Refinery.register_extension(Refinery::AuthenticateIdentities)
end
config.to_prepare do
Decorators.register! Engine.root
end
end
end
end
puts ">> Initialising Authentication: Omniauth Identity"
module Authentication
def self.secure_frontend
ActionController::Base.class_eval do
class << self
def you_shall_not_pass!
include ::Authentication::ApplicationController
end
end
end
end
module ApplicationController
def self.included _klass
_klass.class_eval do
include InstanceMethods
_klass.send(:before_filter, :set_current_user)
helper_method :set_current_user, :current_user, :user_signed_in?
private :set_current_user, :current_user, :user_signed_in?
end
end
module InstanceMethods
def set_current_user
current_user
end
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
def user_signed_in?
@current_user.present?
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment