devise_active_directory_authenticatable 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -22,7 +22,7 @@ begin
22
22
  gemspec.homepage = "http://github.com/ajrkerr/devise_activedirectory_authenticatable"
23
23
  gemspec.authors = ["Adam Kerr"]
24
24
  gemspec.add_dependency "devise", ">= 1.1.5"
25
- gemspec.add_dependency "active_directory", ">= 1.2.0"
25
+ gemspec.add_dependency "active_directory", ">= 1.2.4"
26
26
  end
27
27
  Jeweler::GemcutterTasks.new
28
28
  rescue LoadError
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.0
1
+ 0.3.1
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{devise_active_directory_authenticatable}
8
- s.version = "0.3.0"
8
+ s.version = "0.3.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Adam Kerr"]
12
- s.date = %q{2011-02-17}
12
+ s.date = %q{2011-02-22}
13
13
  s.description = %q{Active Directory authentication module for Devise, based off of LDAP Authentication}
14
14
  s.email = %q{ajrkerr@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -34,7 +34,7 @@ Gem::Specification.new do |s|
34
34
  ]
35
35
  s.homepage = %q{http://github.com/ajrkerr/devise_activedirectory_authenticatable}
36
36
  s.require_paths = ["lib"]
37
- s.rubygems_version = %q{1.5.0}
37
+ s.rubygems_version = %q{1.5.2}
38
38
  s.summary = %q{Active Directory authentication module for Devise}
39
39
 
40
40
  if s.respond_to? :specification_version then
@@ -42,14 +42,14 @@ Gem::Specification.new do |s|
42
42
 
43
43
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
44
44
  s.add_runtime_dependency(%q<devise>, [">= 1.1.5"])
45
- s.add_runtime_dependency(%q<active_directory>, [">= 1.2.0"])
45
+ s.add_runtime_dependency(%q<active_directory>, [">= 1.2.4"])
46
46
  else
47
47
  s.add_dependency(%q<devise>, [">= 1.1.5"])
48
- s.add_dependency(%q<active_directory>, [">= 1.2.0"])
48
+ s.add_dependency(%q<active_directory>, [">= 1.2.4"])
49
49
  end
50
50
  else
51
51
  s.add_dependency(%q<devise>, [">= 1.1.5"])
52
- s.add_dependency(%q<active_directory>, [">= 1.2.0"])
52
+ s.add_dependency(%q<active_directory>, [">= 1.2.4"])
53
53
  end
54
54
  end
55
55
 
@@ -51,7 +51,7 @@ module Devise
51
51
  #Username attribute used for logging in
52
52
  #Will be automagicaly mapped to authentication_keys.first
53
53
  mattr_accessor :ad_username
54
- @@ad_username = :userPrincipalName
54
+ @@ad_username = :userprincipalname
55
55
 
56
56
  #Map Devise authentication key accordingly
57
57
  #Does this work when initializers are set too?
@@ -10,16 +10,17 @@ module Devise
10
10
  include AdObject
11
11
 
12
12
  module ClassMethods
13
- def activedirectory_class
14
- ActiveDirectory::Group
15
- end
16
-
13
+ # TODO find a way to get rid of this with metaprogramming
17
14
  def devise_model
18
15
  AdGroup
19
16
  end
20
17
 
18
+ def activedirectory_class
19
+ ActiveDirectory::Group
20
+ end
21
+
21
22
  def sync_all
22
- #return false unless connected_to_activedirectory?
23
+ return false unless connected_to_activedirectory?
23
24
  find_or_create_from_activedirectory.each do |gp|
24
25
  gp.save
25
26
  end
@@ -3,8 +3,6 @@ module Devise
3
3
  module AdObject
4
4
  extend ActiveSupport::Concern
5
5
 
6
- #Constants for easy access
7
- ADConnect = DeviseActiveDirectoryAuthenticatable
8
6
  Logger = DeviseActiveDirectoryAuthenticatable::Logger
9
7
 
10
8
  def klass
@@ -13,32 +11,36 @@ module Devise
13
11
 
14
12
  # Update the attributes of the current object from the AD
15
13
  # Defaults to current user if no parameters given
16
- def activedirectory_sync!(params = {})
14
+ def activedirectory_sync! params = {}
17
15
  params[:objectguid] = self.objectguid if params.empty?
18
- ad_objs = params[:object] || klass.find_in_activedirectory(params)
16
+ ad_obj = params[:object] || klass.find_in_activedirectory(params).first
17
+ copy_from_activedirectory ad_obj unless ad_obj.nil?
18
+ end
19
19
 
20
- return false if ad_objs.nil?
21
- ad_objs = Array(ad_objs) unless ad_objs.is_a? Array
20
+ # Update the attributes of the current object from the AD
21
+ # Defaults to current user if no parameters given
22
+ def update_from_activedirectory! params = {}
23
+ params[:objectguid] = self.objectguid if params.empty?
24
+ ad_obj = params[:object] || klass.find_in_activedirectory(params).first
25
+ copy_from_activedirectory ad_obj unless ad_obj.nil?
26
+ end
22
27
 
23
- #Grab attributes from Devise mapping
24
- ad_objs.each do |ad_obj|
25
- ::Devise.ad_attr_mapping[klass.devise_model_name.to_sym].each do |local_attr, active_directory_attr|
26
- self[local_attr] = ad_obj.send(active_directory_attr)
27
- end
28
+ # Update the local object using an Active Directory entry
29
+ def copy_from_activedirectory ad_obj
30
+ ::Devise.ad_attr_mapping[klass.devise_model_name.to_sym].each do |local_attr, active_directory_attr|
31
+ self[local_attr] = ad_obj.send(active_directory_attr)
28
32
  end
29
33
  end
30
34
 
31
- def activedirectory_self
32
- find_in_activedirectory :objectGUID => objectGUID
35
+ def find_in_activedirectory
36
+ klass.find_in_activedirectory :objectGUID => objectGUID
33
37
  end
34
38
 
35
39
  module ClassMethods
36
40
 
37
- # def devise_model
38
- # self.ancestors.each do |mod|
39
- # return mod if mod.include? self.class
40
- # end
41
- # end
41
+ def login_with
42
+ ::Devise.authentication_keys.first
43
+ end
42
44
 
43
45
  def devise_model_name
44
46
  devise_model.name[/.*::(.*)/, 1]
@@ -48,36 +50,6 @@ module Devise
48
50
  activedirectory_class.name[/.*::(.*)/, 1]
49
51
  end
50
52
 
51
- #TODO switch from reverse to rassoc to allow for multiple mappings
52
- def ad_field_to_local field_name
53
- @ad_to_local_map ||= ::Devise.ad_attr_mapping[devise_model_name.to_sym].invert
54
- return (@ad_to_local_map.has_key? field_name) ? @ad_to_local_map[field_name] : field_name
55
- end
56
-
57
- #TODO switch from reverse to rassoc to allow for multiple mappings
58
- def local_field_to_ad field_name
59
- @local_to_ad_map ||= ::Devise.ad_attr_mapping[devise_model_name.to_sym]
60
- return (@local_to_ad_map.has_key? field_name) ? @local_to_ad_map[field_name] : field_name
61
- end
62
-
63
- def ad_attrs_to_local ad_attrs
64
- local_attrs = {}
65
- ad_attrs.each do |ad_key, value|
66
- local_key = ad_field_to_local(ad_key)
67
- local_attrs[local_key] = value
68
- end
69
- local_attrs
70
- end
71
-
72
- def local_attrs_to_ad local_attrs
73
- ad_attrs = {}
74
- local_attrs.each do |local_key, value|
75
- ad_key = local_field_to_ad(local_key)
76
- ad_attrs[ad_key] = value
77
- end
78
- ad_attrs
79
- end
80
-
81
53
  #Search based on GUID, DN or Username primarily
82
54
  def find_in_activedirectory(local_params = {})
83
55
  #Reverse mappings for user
@@ -85,9 +57,7 @@ module Devise
85
57
 
86
58
  return find_all_in_activedirectory if ad_params.empty?
87
59
 
88
- ad_objs = activedirectory_class.find(:all, ad_params)
89
-
90
- return ad_objs
60
+ activedirectory_class.find(:all, ad_params)
91
61
  end
92
62
 
93
63
  def find_or_create_from_activedirectory params = {}
@@ -122,8 +92,41 @@ module Devise
122
92
 
123
93
  def activedirectory_connect
124
94
  ActiveDirectory::Base.setup(::Devise.ad_settings)
125
- raise DeviseActiveDirectoryAuthenticatable::ActiveDirectoryException, "Invliad Username or Password" unless ActiveDirectory::Base.connected?
95
+ raise DeviseActiveDirectoryAuthenticatable::ActiveDirectoryException, "Invalid Username or Password" unless ActiveDirectory::Base.connected?
96
+ end
97
+
98
+ private
99
+
100
+ #TODO switch from reverse to rassoc to allow for multiple mappings
101
+ def ad_field_to_local field_name
102
+ @ad_to_local_map ||= ::Devise.ad_attr_mapping[devise_model_name.to_sym].invert
103
+ return (@ad_to_local_map.has_key? field_name) ? @ad_to_local_map[field_name] : field_name
126
104
  end
105
+
106
+ #TODO switch from reverse to rassoc to allow for multiple mappings
107
+ def local_field_to_ad field_name
108
+ @local_to_ad_map ||= ::Devise.ad_attr_mapping[devise_model_name.to_sym]
109
+ return (@local_to_ad_map.has_key? field_name) ? @local_to_ad_map[field_name] : field_name
110
+ end
111
+
112
+ def ad_attrs_to_local ad_attrs
113
+ local_attrs = {}
114
+ ad_attrs.each do |ad_key, value|
115
+ local_key = ad_field_to_local(ad_key)
116
+ local_attrs[local_key] = value
117
+ end
118
+ local_attrs
119
+ end
120
+
121
+ def local_attrs_to_ad local_attrs
122
+ ad_attrs = {}
123
+ local_attrs.each do |local_key, value|
124
+ ad_key = local_field_to_ad(local_key)
125
+ ad_attrs[ad_key] = value
126
+ end
127
+ ad_attrs
128
+ end
129
+
127
130
  end
128
131
  end
129
132
  end
@@ -1,7 +1,6 @@
1
1
  require 'devise_active_directory_authenticatable/strategy'
2
2
  require 'devise_active_directory_authenticatable/exception'
3
3
  require 'devise_active_directory_authenticatable/models/ad_object'
4
- require 'devise_active_directory_authenticatable/models/ad_group'
5
4
 
6
5
  module Devise
7
6
  module Models
@@ -13,6 +12,10 @@ module Devise
13
12
 
14
13
  Logger = DeviseActiveDirectoryAuthenticatable::Logger
15
14
 
15
+ included do
16
+
17
+ end
18
+
16
19
  ## Devise key
17
20
  def login_with
18
21
  self[::Devise.authentication_keys.first]
@@ -26,26 +29,25 @@ module Devise
26
29
  end
27
30
 
28
31
  def authenticate_with_activedirectory params = {}
29
- params[:username] ||= self[login_with]
30
- set_activedirectory_credentials params
31
- activedirectory_connect
32
+ params[:username] ||= login_with
33
+ self.class.set_activedirectory_credentials params
34
+ self.class.activedirectory_connect
32
35
  end
33
36
 
34
37
  module ClassMethods
35
- def activedirectory_class
36
- ActiveDirectory::User
37
- end
38
-
38
+ # TODO find a way to get rid of this with metaprogramming
39
39
  def devise_model
40
40
  AdUser
41
41
  end
42
42
 
43
+ def activedirectory_class
44
+ ActiveDirectory::User
45
+ end
46
+
43
47
  # Authenticate a user based on configured attribute keys. Returns the
44
48
  # authenticated user if it's valid or nil.
45
49
  def authenticate_with_activedirectory(attributes={})
46
- @login_with = ::Devise.authentication_keys.first
47
-
48
- username = attributes[@login_with]
50
+ username = attributes[login_with]
49
51
  password = attributes[:password]
50
52
 
51
53
  Logger.send "Attempting to login :#{@login_with} => #{username}"
@@ -59,7 +61,6 @@ module Devise
59
61
 
60
62
  # Find them in the local database
61
63
  user = find_or_create_from_activedirectory(@login_with => attributes[@login_with]).first
62
- Logger.send "User: #{user.inspect}"
63
64
 
64
65
  # Check to see if we have the same user
65
66
  unless user.nil?
@@ -37,24 +37,32 @@ module DeviseActiveDirectoryAuthenticatable
37
37
  # }
38
38
 
39
39
 
40
+ # config.ad_attr_mapping = {
40
41
  ##Attribute mapping for user object
41
- # config.ad_user_mapping = {
42
- # :objectguid => :objectguid, #Required
43
- # :username => :userprincipalname,
44
- # :dn => :dn,
45
- # :firstname => :givenname,
46
- # :lastname => :sn
47
- # }
42
+ # :AdUser => {
43
+ # #Attributes are lowercase
44
+ # :objectguid => :objectguid, #Required
45
+ # :username => :userprincipalname,
46
+ # :dn => :dn,
47
+ # :firstname => :givenName,
48
+ # :lastname => :sn,
49
+ # :whenchanged => :whenchanged,
50
+ # :whencreated => :whencreated,
51
+ # },
48
52
 
49
- # config.ad_group_mapping = {
50
- # :objectguid => :objectguid, #Required
51
- # :dn => :dn,
52
- # :name => :name,
53
- # :description => :description,
54
- # :whencreated => :whencreated,
55
- # :whenchanged => :whenchanged,
53
+ ##Attribute mapping for group objects
54
+ # :AdGroup => {
55
+ # #Attributes are lowercase
56
+ # :objectguid => :objectguid, #Required
57
+ # :dn => :dn,
58
+ # :name => :name,
59
+ # :description => :description,
60
+ # :whencreated => :whencreated,
61
+ # :whenchanged => :whenchanged,
62
+ # }
56
63
  # }
57
64
 
65
+
58
66
  ##Username attribute
59
67
  ##Maps to :login_with in the devise configuration
60
68
  # config.ad_username = :userPrincipalName
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: devise_active_directory_authenticatable
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 17
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 3
9
- - 0
10
- version: 0.3.0
9
+ - 1
10
+ version: 0.3.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Adam Kerr
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-02-17 00:00:00 -05:00
18
+ date: 2011-02-22 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -42,12 +42,12 @@ dependencies:
42
42
  requirements:
43
43
  - - ">="
44
44
  - !ruby/object:Gem::Version
45
- hash: 31
45
+ hash: 23
46
46
  segments:
47
47
  - 1
48
48
  - 2
49
- - 0
50
- version: 1.2.0
49
+ - 4
50
+ version: 1.2.4
51
51
  type: :runtime
52
52
  version_requirements: *id002
53
53
  description: Active Directory authentication module for Devise, based off of LDAP Authentication
@@ -104,7 +104,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
104
104
  requirements: []
105
105
 
106
106
  rubyforge_project:
107
- rubygems_version: 1.5.0
107
+ rubygems_version: 1.5.2
108
108
  signing_key:
109
109
  specification_version: 3
110
110
  summary: Active Directory authentication module for Devise