kch (owner)

Revisions

gist: 111517 Download_button fork
public
Description:
UR DOIN' IT WRONG (untested)
Public Clone URL: git://gist.github.com/111517.git
Embed All Files: show embed
cas-sql-authenticator-original.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
cas-sql-authenticator-refactor.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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