Class: CASino::ActiveRecordAuthenticator

Inherits:
Object
  • Object
show all
Defined in:
lib/casino/activerecord_authenticator/version.rb,
lib/casino/activerecord_authenticator.rb

Defined Under Namespace

Classes: AuthDatabase

Constant Summary collapse

VERSION =
'4.0.1'

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ ActiveRecordAuthenticator

Returns a new instance of ActiveRecordAuthenticator.

Parameters:

  • options (Hash)

Raises:

  • (ArgumentError)


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
# File 'lib/casino/activerecord_authenticator.rb', line 13

def initialize(options)
  if !options.respond_to?(:deep_symbolize_keys)
    raise ArgumentError, "When assigning attributes, you must pass a hash as an argument."
  end
  @options = options.deep_symbolize_keys
  raise ArgumentError, "Table name is missing" unless @options[:table]
  if @options[:model_name]
    model_name = @options[:model_name]
  else
    model_name = @options[:table]
    if @options[:connection].kind_of?(Hash) && @options[:connection][:database]
      model_name = "#{@options[:connection][:database].gsub(/[^a-zA-Z]+/, '')}_#{model_name}"
    end
    model_name = model_name.classify
  end
  model_class_name = "#{self.class.to_s}::#{model_name}"
  eval <<-END
    class #{model_class_name} < AuthDatabase
      self.table_name = "#{@options[:table]}"
      self.inheritance_column = :_type_disabled
    end
  END

  @model = model_class_name.constantize
  @model.establish_connection @options[:connection]
end

Instance Method Details

#load_user_data(username) ⇒ Object



54
55
56
57
58
59
# File 'lib/casino/activerecord_authenticator.rb', line 54

def load_user_data(username)
  user = @model.send("find_by_#{@options[:username_column]}!", username)
  user_data(user)
rescue ActiveRecord::RecordNotFound
  nil
end

#validate(username, password) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/casino/activerecord_authenticator.rb', line 40

def validate(username, password)
  user = @model.send("find_by_#{@options[:username_column]}!", username)
  password_from_database = user.send(@options[:password_column])

  if valid_password?(password, password_from_database)
    user_data(user)
  else
    false
  end

rescue ActiveRecord::RecordNotFound
  false
end