Module: Devise::Models::LdapAuthenticatable

Extended by:
ActiveSupport::Concern
Defined in:
lib/devise_ldap_multiple/model.rb

Overview

LDAP Module, responsible for validating the user credentials via LDAP.

Examples:

User.authenticate('[email protected]', 'password123')  # returns authenticated user or nil
User.find(1).valid_password?('password123')         # returns true/false

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#after_ldap_authenticationObject

Called after a successful LDAP authentication



91
92
# File 'lib/devise_ldap_multiple/model.rb', line 91

def after_ldap_authentication
end

#change_password!(current_password) ⇒ Object



33
34
35
36
# File 'lib/devise_ldap_multiple/model.rb', line 33

def change_password!(current_password)
  raise "Need to set new password first" if @password.blank?
  Devise::LDAP::Adapter.update_own_password(, @password, current_password, current_scope)
end

#current_scopeObject

Returns the current scope / mapping from devise (eg: ‘admin’, ‘user’, ‘staff’)



19
20
21
# File 'lib/devise_ldap_multiple/model.rb', line 19

def current_scope
  Devise.mappings.find {|k,v| v.class_name == self.class.name}.last.name.downcase
end

#in_ldap_group?(group_name, group_attribute = LDAP::DEFAULT_GROUP_UNIQUE_MEMBER_LIST_KEY) ⇒ Boolean



66
67
68
# File 'lib/devise_ldap_multiple/model.rb', line 66

def in_ldap_group?(group_name, group_attribute = LDAP::DEFAULT_GROUP_UNIQUE_MEMBER_LIST_KEY)
  Devise::LDAP::Adapter.in_ldap_group?(, group_name, group_attribute, current_scope)
end

#ldap_dnObject



70
71
72
# File 'lib/devise_ldap_multiple/model.rb', line 70

def ldap_dn
  ldap_entry ? ldap_entry.dn : nil
end

#ldap_entryObject



58
59
60
# File 'lib/devise_ldap_multiple/model.rb', line 58

def ldap_entry
  @ldap_entry ||= Devise::LDAP::Adapter.get_ldap_entry(, current_scope)
end

#ldap_get_param(param) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/devise_ldap_multiple/model.rb', line 74

def ldap_get_param(param)
  if ldap_entry && !ldap_entry[param].empty?
    value = ldap_entry.send(param)
  else
    nil
  end
end

#ldap_groupsObject



62
63
64
# File 'lib/devise_ldap_multiple/model.rb', line 62

def ldap_groups
  Devise::LDAP::Adapter.get_groups(current_scope, )
end

#login_withObject



28
29
30
31
# File 'lib/devise_ldap_multiple/model.rb', line 28

def 
  @login_with ||= Devise.mappings.find {|k,v| v.class_name == self.class.name}.last.to.authentication_keys.first
  self[@login_with]
end

#password=(new_password) ⇒ Object



46
47
48
49
50
51
# File 'lib/devise_ldap_multiple/model.rb', line 46

def password=(new_password)
  @password = new_password
  if defined?(password_digest) && @password.present? && respond_to?(:encrypted_password=)
    self.encrypted_password = password_digest(@password) 
  end
end

#reset_password!(new_password, new_password_confirmation) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/devise_ldap_multiple/model.rb', line 38

def reset_password!(new_password, new_password_confirmation)
  if new_password == new_password_confirmation && Devise::LDAP::Adapter.password_updatable?(, current_scope)
    Devise::LDAP::Adapter.update_password(, new_password, current_scope)
  end
  clear_reset_password_token if valid?
  save
end

#valid_ldap_authentication?(password) ⇒ Boolean

Checks if a resource is valid upon authentication.



54
55
56
# File 'lib/devise_ldap_multiple/model.rb', line 54

def valid_ldap_authentication?(password)
  Devise::LDAP::Adapter.valid_credentials?(, password, current_scope)
end