Skip to content

Instantly share code, notes, and snippets.

@TheNicholasNick
Created December 28, 2008 23:13
Show Gist options
  • Save TheNicholasNick/41079 to your computer and use it in GitHub Desktop.
Save TheNicholasNick/41079 to your computer and use it in GitHub Desktop.
irb(main):002:0* Merb::Authentication.default_strategy_order
=> [Merb::Authentication::Strategies::Basic::Base, AuthenticateOnCompany, Merb::Authentication::Strategies::Basic::Form]
irb(main):003:0> Merb::Slices::config[:"merb-auth-slice-password"][:no_default_strategies]
=> true
# This file is specifically setup for use with the merb-auth plugin.
# This file should be used to setup and configure your authentication stack.
# It is not required and may safely be deleted.
#
# To change the parameter names for the password or login field you may set either of these two options
#
# Merb::Plugins.config[:"merb-auth"][:login_param] = :email
# Merb::Plugins.config[:"merb-auth"][:password_param] = :my_password_field_name
begin
# Sets the default class ofr authentication. This is primarily used for
# Plugins and the default strategies
Merb::Authentication.user_class = User
# Mixin the salted user mixin
require 'merb-auth-more/mixins/salted_user'
Merb::Authentication.user_class.class_eval{ include Merb::Authentication::Mixins::SaltedUser }
# Setup the session serialization
class Merb::Authentication
def fetch_user(session_user_id)
Merb::Authentication.user_class.get(session_user_id)
end
def store_user(user)
user.nil? ? user : user.id
end
end
rescue
Merb.logger.error <<-TEXT
You need to setup some kind of user class with merb-auth.
Merb::Authentication.user_class = User
If you want to fully customize your authentication you should use merb-core directly.
See merb/merb-auth/setup.rb and strategies.rb to customize your setup
TEXT
end
Merb::Authentication.after_authentication do |user, request, params|
user.update_attributes(:logged_in_at => Time.now)
user
end
# This file is specifically for you to define your strategies
#
# You should declare you strategies directly and/or use
# Merb::Authentication.activate!(:label_of_strategy)
#
# To load and set the order of strategy processing
Merb::Slices::config[:"merb-auth-slice-password"][:no_default_strategies] = true
#Merb::Authentication.activate!(:default_password_form)
class AuthenticateOnCompany < Merb::Authentication::Strategy
def run!
if params[:login] && params[:password] && params[:company]
user = user_class.authenticate_on_company(params[:login], params[:password], params[:company])
if !user
request.session.authentication.errors.clear!
request.session.authentication.errors.add(:login, "Try again")
end
user
end
end # run!
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment