Skip to content

Commit

Permalink
Released v1.4.0. A new I18n soluton has been added. Please see Authlo…
Browse files Browse the repository at this point in the history
…gic::I18n for more info. The old solution via configuration is no longer supported.
  • Loading branch information
binarylogic committed Jan 29, 2009
1 parent b5dd374 commit 7717a1b
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 192 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.rdoc
@@ -1,10 +1,11 @@
== 1.3.10
== 1.4.0 released 2009-1-28

* Added support for cookie domain, based on your frameworks session domain configuration
* Updated test helper functions to use the persistence token config value
* Check for UTC times when using Time.now for current_login_at and last_request_at
* Single access now looks for a single_access_allowed? method in your controllers to determine if single access should be allowed or not. Allowing you to define exactly when single access is allowed.
* Finding the authenticated record uses klass.primary_key instead of assuming id.
* BREAKS BACKWARDS COMPATIBILITY: New I18n solution implemented. See Authlogic::I18n for more information.

== 1.3.9 released 2009-1-9

Expand Down
4 changes: 4 additions & 0 deletions README.rdoc
Expand Up @@ -477,6 +477,10 @@ Obviously there is a little more to it than this, but hopefully this clarifies a

When things come together like this I think its a sign that you are doing something right. Put that in your pipe and smoke it!

== Internationalization (I18n)

Please see Authlogic::I18n for more information. Internationalization is very easy to implement, in fact if you are using the default rails I18n library then you don't need to do anything other than defining the messages in your localization configuration files. See Authlogic::I18n for a complete list of keys you need to define.

== Testing

Testing with authlogic is easy, there is a helper file that will add some convenient test helpers for you. In your test_helper.rb file do the following:
Expand Down
1 change: 1 addition & 0 deletions Rakefile
Expand Up @@ -10,4 +10,5 @@ Echoe.new 'authlogic' do |p|
p.summary = "A clean, simple, and unobtrusive ruby authentication solution."
p.url = "http://github.com/binarylogic/authlogic"
p.dependencies = %w(activesupport echoe)
p.install_message = "BREAKS BACKWARDS COMPATIBILITY! This is only for those using I18n. If you were using the Authlogic configuration to implement I18n you need to update your configuration. A new cleaner approach has been implemented for I18n in Authlogic. See Authlogic::I18n for more details."
end
1 change: 1 addition & 0 deletions lib/authlogic.rb
@@ -1,6 +1,7 @@
require "active_support"

require File.dirname(__FILE__) + "/authlogic/version"
require File.dirname(__FILE__) + "/authlogic/i18n"

require File.dirname(__FILE__) + "/authlogic/controller_adapters/abstract_adapter"
require File.dirname(__FILE__) + "/authlogic/controller_adapters/rails_adapter" if defined?(Rails)
Expand Down
52 changes: 52 additions & 0 deletions lib/authlogic/i18n.rb
@@ -0,0 +1,52 @@
module Authlogic
# I18n
#
# Allows any message, error message, etc to use internationalization. In earlier versions of Authlogic each message was translated via configuration.
# This cluttered up the configuration and cluttered up Authlogic. So all translation has been extracted out into this class. Now all messages pass through
# this class, making it much easier to implement in I18n library / plugin you want. Use this as a layer that sits between Authlogic and whatever I18n
# library you want to use.
#
# By default this uses the rails I18n library, if it exists. If it doesnt exist it just returns the default english message. Using the Authlogic I18n class
# works EXACTLY like the rails I18n class. Here is how all messages are translated internally with Authlogic:
#
# Authlogic::I18n.t('error_messages.password_invalid', :default => "is invalid")
#
# If you use a different I18n library or plugin just redefine the t method in the Authlogic::I18n class to do whatever you want with those options. For example:
#
# # config/initializers/authlogic.rb
# module MyAuthlogicI18nAdapter
# def t(key, options = {})
# # you will have key which will be something like: "error_messages.password_invalid"
# # you will also have options[:default], which will be the default english version of the message
# # do whatever you want here with the arguments passed to you.
# end
# end
#
# Authlogic::I18n.extend MyAuthlogicI18nAdapter
#
# That it's! Here is a complete list of the keys that are passed. Just defined these however you wish:
#
# authlogic:
# error_messages:
# login_blank: can not be blank
# login_not_found: does not exist
# password_blank: can not be blank
# password_invlid: is not valid
# not_active: Your account is not active
# not_confirmed: Your account is not confirmed
# not_approved: Your account is not approved
# blank_record: You can not login with a blank record
# new_record: You can not login with a new record
class I18n
class << self
# All message translation is passed to this method. The first argument is the key for the message. The second is options, see the rails I18n library for a list of options used.
def t(key, options = {})
if defined?(::I18n)
::I18n.t(key, options.merge(:scope => :authlogic))
else
options[:default]
end
end
end
end
end
14 changes: 7 additions & 7 deletions lib/authlogic/session/base.rb
Expand Up @@ -412,19 +412,19 @@ def valid_credentials?

case authenticating_with
when :password
errors.add(login_field, login_blank_message) if send(login_field).blank?
errors.add(password_field, password_blank_message) if send("protected_#{password_field}").blank?
errors.add(login_field, I18n.t('error_messages.login_blank', :default => "can not be blank")) if send(login_field).blank?
errors.add(password_field, I18n.t('error_messages.password_blank', :default => "can not be blank")) if send("protected_#{password_field}").blank?
return false if errors.count > 0

unchecked_record = search_for_record(find_by_login_method, send(login_field))

if unchecked_record.blank?
errors.add(login_field, login_not_found_message)
errors.add(login_field, I18n.t('error_messages.login_not_found', :default => "does not exist"))
return false
end

unless unchecked_record.send(verify_password_method, send("protected_#{password_field}"))
errors.add(password_field, password_invalid_message)
errors.add(password_field, I18n.t('error_messages.password_invalid', :default => "is not valid"))
return false
end

Expand All @@ -433,12 +433,12 @@ def valid_credentials?
unchecked_record = unauthorized_record

if unchecked_record.blank?
errors.add_to_base("You can not login with a blank record.")
errors.add_to_base(I18n.t('error_messages.blank_record', :default => "You can not login with a blank record"))
return false
end

if unchecked_record.new_record?
errors.add_to_base("You can not login with a new record.")
errors.add_to_base(I18n.t('error_messages.new_record', :default => "You can not login with a new record"))
return false
end

Expand All @@ -452,7 +452,7 @@ def valid_record?
return true if disable_magic_states?
[:active, :approved, :confirmed].each do |required_status|
if record.respond_to?("#{required_status}?") && !record.send("#{required_status}?")
errors.add_to_base(send("not_#{required_status}_message"))
errors.add_to_base(I18n.t("errors_messages.not_#{required_status}", :default => "Your account is not #{required_status}"))
return false
end
end
Expand Down
117 changes: 19 additions & 98 deletions lib/authlogic/session/config.rb
Expand Up @@ -134,29 +134,13 @@ def last_request_at_threshold(value = nil)
end
alias_method :last_request_at_threshold=, :last_request_at_threshold

# The error message used when the login is left blank.
#
# * <tt>Default:</tt> "can not be blank"
# * <tt>Accepts:</tt> String
def login_blank_message(value = nil)
if value.nil?
read_inheritable_attribute(:login_blank_message) || login_blank_message("can not be blank")
else
write_inheritable_attribute(:login_blank_message, value)
end
def login_blank_message(value = nil) # :nodoc:
new_i18n_error
end
alias_method :login_blank_message=, :login_blank_message

# The error message used when the login could not be found in the database.
#
# * <tt>Default:</tt> "does not exist"
# * <tt>Accepts:</tt> String
def login_not_found_message(value = nil)
if value.nil?
read_inheritable_attribute(:login_not_found_message) || login_not_found_message("does not exist")
else
write_inheritable_attribute(:login_not_found_message, value)
end
def login_not_found_message(value = nil) # :nodoc:
new_i18n_error
end
alias_method :login_not_found_message=, :login_not_found_message

Expand Down Expand Up @@ -196,42 +180,18 @@ def logout_on_timeout(value = nil)
end
alias_method :logout_on_timeout=, :logout_on_timeout

# The error message used when the record returns false to active?
#
# * <tt>Default:</tt> "Your account is not active"
# * <tt>Accepts:</tt> String
def not_active_message(value = nil)
if value.nil?
read_inheritable_attribute(:not_active_message) || not_active_message("Your account is not active")
else
write_inheritable_attribute(:not_active_message, value)
end
def not_active_message(value = nil) # :nodoc:
new_i18n_error
end
alias_method :not_active_message=, :not_active_message

# The error message used when the record returns false to approved?
#
# * <tt>Default:</tt> "Your account is not approved"
# * <tt>Accepts:</tt> String
def not_approved_message(value = nil)
if value.nil?
read_inheritable_attribute(:not_approved_message) || not_approved_message("Your account is not approved")
else
write_inheritable_attribute(:not_approved_message, value)
end
def not_approved_message(value = nil) # :nodoc:
new_i18n_error
end
alias_method :not_approved_message=, :not_approved_message

# The error message used when the record returns false to confirmed?
#
# * <tt>Default:</tt> "Your account is not confirmed"
# * <tt>Accepts:</tt> String
def not_confirmed_message(value = nil)
if value.nil?
read_inheritable_attribute(:not_confirmed_message) || not_confirmed_message("Your account is not confirmed")
else
write_inheritable_attribute(:not_confirmed_message, value)
end
def not_confirmed_message(value = nil) # :nodoc:
new_i18n_error
end
alias_method :not_confirmed_message=, :not_confirmed_message

Expand All @@ -253,16 +213,8 @@ def params_key(value = nil)
end
alias_method :params_key=, :params_key

# The error message used when the password is left blank.
#
# * <tt>Default:</tt> "can not be blank"
# * <tt>Accepts:</tt> String
def password_blank_message(value = nil)
if value.nil?
read_inheritable_attribute(:password_blank_message) || password_blank_message("can not be blank")
else
write_inheritable_attribute(:password_blank_message, value)
end
def password_blank_message(value = nil) # :nodoc:
new_i18n_error
end
alias_method :password_blank_message=, :password_blank_message

Expand All @@ -279,16 +231,8 @@ def password_field(value = nil)
end
alias_method :password_field=, :password_field

# The error message used when the password is invalid.
#
# * <tt>Default:</tt> "is invalid"
# * <tt>Accepts:</tt> String
def password_invalid_message(value = nil)
if value.nil?
read_inheritable_attribute(:password_invalid_message) || password_invalid_message("is invalid")
else
write_inheritable_attribute(:password_invalid_message, value)
end
def password_invalid_message(value = nil) # :nodoc:
new_i18n_error
end
alias_method :password_invalid_message=, :password_invalid_message

Expand Down Expand Up @@ -357,6 +301,11 @@ def verify_password_method(value = nil)
end
end
alias_method :verify_password_method=, :verify_password_method

private
def new_i18n_error
raise NotImplementedError.new("As of v 1.4.0 Authlogic implements a new I18n solution that is much cleaner and easier. Please see Authlogic::I18n for more information on how to provide internationalization in Authlogic.")
end
end

module InstanceMethods # :nodoc:
Expand All @@ -383,14 +332,6 @@ def find_with
def last_request_at_threshold
self.class.last_request_at_threshold
end

def login_blank_message
self.class.login_blank_message
end

def login_not_found_message
self.class.login_not_found_message
end

def login_field
self.class.login_field
Expand All @@ -400,38 +341,18 @@ def logout_on_timeout?
self.class.logout_on_timeout == true
end

def not_active_message
self.class.not_active_message
end

def not_approved_message
self.class.not_approved_message
end

def not_confirmed_message
self.class.not_confirmed_message
end

def params_allowed_request_types
build_key(self.class.params_allowed_request_types)
end

def params_key
build_key(self.class.params_key)
end

def password_blank_message
self.class.password_blank_message
end

def password_field
self.class.password_field
end

def password_invalid_message
self.class.password_invalid_message
end

def perishable_token_field
klass.acts_as_authentic_config[:perishable_token_field]
end
Expand Down
4 changes: 2 additions & 2 deletions lib/authlogic/version.rb
Expand Up @@ -43,8 +43,8 @@ def to_a
end

MAJOR = 1
MINOR = 3
TINY = 9
MINOR = 4
TINY = 0

# The current version as a Version instance
CURRENT = new(MAJOR, MINOR, TINY)
Expand Down

0 comments on commit 7717a1b

Please sign in to comment.