Created
May 14, 2009 06:10
-
-
Save kch/111517 to your computer and use it in GitHub Desktop.
UR DOIN' IT WRONG (untested)
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
class CASServer::Authenticators::SQL < CASServer::Authenticators::Base | |
def validate(credentials) | |
read_standard_credentials(credentials) | |
raise CASServer::AuthenticatorError, "Cannot validate credentials because the authenticator hasn't yet been configured" unless @options | |
raise CASServer::AuthenticatorError, "Invalid authenticator configuration!" unless @options[:database] | |
CASUser.establish_connection @options[:database] | |
CASUser.set_table_name @options[:user_table] || "users" | |
username_column = @options[:username_column] || 'username' | |
password_column = @options[:password_column] || 'password' | |
results = CASUser.find(:all, :conditions => ["#{username_column} = ? AND #{password_column} = ?", @username, @password]) | |
if results.size > 0 | |
$LOG.warn("#{self.class}: Multiple matches found for user #{@username.inspect}") if results.size > 1 | |
unless @options[:extra_attributes].blank? | |
if results.size > 1 | |
$LOG.warn("#{self.class}: Unable to extract extra_attributes because multiple matches were found for #{@username.inspect}") | |
else | |
user = results.first | |
@extra_attributes = {} | |
extra_attributes_to_extract.each do |col| | |
@extra_attributes[col] = user.send(col) | |
end | |
if @extra_attributes.empty? | |
$LOG.warn("#{self.class}: Did not read any extra_attributes for user #{@username.inspect} even though an :extra_attributes option was provided.") | |
else | |
$LOG.debug("#{self.class}: Read the following extra_attributes for user #{@username.inspect}: #{@extra_attributes.inspect}") | |
end | |
end | |
end | |
return true | |
else | |
return false | |
end | |
end | |
class CASUser < ActiveRecord::Base | |
end | |
end |
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
class CASServer::Authenticators::SQL < CASServer::Authenticators::Base | |
class CASUser < ActiveRecord::Base; end | |
def self.option_reader(k, default = nil) | |
define_method(k) { @options.fetch(k, default) } | |
end | |
option_reader :username_column, "username" | |
option_reader :password_column, "password" | |
option_reader :user_table, "users" | |
option_reader :database | |
option_reader :extra_attributes | |
def validate(credentials) | |
# should we even run? | |
@options or raise CASServer::AuthenticatorError, "Cannot validate credentials because the authenticator hasn't yet been configured" | |
database or raise CASServer::AuthenticatorError, "Invalid authenticator configuration!" | |
# initialize | |
read_standard_credentials(credentials) | |
CASUser.establish_connection database | |
CASUser.set_table_name user_table | |
# do some real work at last | |
user, multiple_users = CASUser.send("find_all_by_#{username_column}_and_#{password_column}", @username, @password) | |
return false unless user | |
go_ahead_and_tell_everyone if multiple_users | |
@extra_attributes = extra_attributes_to_extract.inject({}) { |h, k| h[k] = user.send(k); h } if extra_attributes.blank? | |
return true | |
end | |
def go_ahead_and_tell_everyone | |
s = "#{self.class}: Multiple matches found for user #{@username.inspect}." | |
s << " Unable to extract extra_attributes because of it." unless extra_attributes.blank? | |
$LOG.warn(s) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment