Module: Entrance::Model::ClassMethods

Defined in:
lib/entrance/model.rb

Instance Method Summary collapse

Instance Method Details

#authenticate(username, password) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/entrance/model.rb', line 54

def authenticate(username, password)
  raise 'Local auth disabled!' unless Entrance.config.local_auth
  return if [username, password].any? { |v| v.nil? || v.strip == '' }

  query = {}
  query[Entrance.fields.username] = username.to_s.downcase.strip
  if u = where(query).first
    return u.authenticated?(password) ? u : nil
  end
end

#provides_entrance(options = {}) {|fields| ... } ⇒ Object

Yields:

  • (fields)


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
49
50
51
52
# File 'lib/entrance/model.rb', line 10

def provides_entrance(options = {}, &block)
  local  = options.delete(:local) != false # true by default
  remote = options.delete(:remote) == true # false by default
  skip_checks = options.delete(:skip_checks)

  if local === false && remote === false
    raise "You have to enable either local or remote auth via `provides_entrance`."
  end

  Entrance.config.model       = self.name
  Entrance.config.local_auth  = local
  Entrance.config.remote_auth = remote

  # if the target model class does not have a Model.where() method,
  # then login_by_session wont work, nor the ClassMethods below.
  # won't work so we cannot continue.
  unless self.respond_to?(:where)
    raise "#{self.name} does not have a .where() finder class method. Cannot continue."
  end

  fields = Entrance.fields
  yield fields if block_given?

  # username and remember token are used both for local and remote (omniauth)
  fields.validate(:username) unless skip_checks
  include Entrance::Model::RememberMethods if fields.validate_option(:remember)

  if local # allows password & reset
    fields.validate(:password) unless skip_checks
    include Entrance::Model::ResetMethods if fields.validate_option(:reset)

    if self.respond_to?(:validates)
      validates :password, :presence => true, :length => 6..32, :if => :password_required?
      validates :password, :confirmation => true, :if => :password_required?
      validates :password_confirmation, :presence => true, :if => :password_required?
    end
  end

  if remote
    fields.validate(:auth_provider, :auth_uid) unless skip_checks
    include RemoteAuthMethods if local # no need to if only remote
  end
end

#with_password_reset_token(token) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/entrance/model.rb', line 65

def with_password_reset_token(token)
  Entrance.config.permit!(:reset)
  return if token.nil?

  query = {}
  query[Entrance.fields.reset_token] = token.to_s.strip
  if u = where(query).first \
    and (!Entrance.fields.reset_until || u.send(Entrance.fields.reset_until) > Time.now)
      return u
  end
end