devise_ldap_authenticatable 0.6.1 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (104) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +1 -0
  3. data/CHANGELOG.md +7 -0
  4. data/Gemfile +1 -4
  5. data/README.md +45 -92
  6. data/Rakefile +8 -7
  7. data/devise_ldap_authenticatable.gemspec +15 -3
  8. data/lib/devise_ldap_authenticatable/ldap_adapter.rb +51 -27
  9. data/lib/devise_ldap_authenticatable/model.rb +5 -1
  10. data/lib/devise_ldap_authenticatable/strategy.rb +3 -1
  11. data/lib/devise_ldap_authenticatable/version.rb +1 -1
  12. data/lib/generators/devise_ldap_authenticatable/templates/ldap.yml +3 -3
  13. data/spec/ldap/.gitignore +2 -0
  14. data/{test → spec}/ldap/base.ldif +0 -0
  15. data/{test → spec}/ldap/clear.ldif +0 -0
  16. data/{test → spec}/ldap/local.schema +0 -0
  17. data/spec/ldap/openldap-data/.gitignore +2 -0
  18. data/spec/ldap/openldap-data/run/.gitignore +2 -0
  19. data/{test → spec}/ldap/openldap-data/run/.gitkeep +0 -0
  20. data/spec/ldap/run-server +31 -0
  21. data/{test → spec}/ldap/server.pem +0 -0
  22. data/{test/ldap/slapd-test.conf → spec/ldap/slapd-test.conf.erb} +16 -16
  23. data/{test → spec}/rails_app/Rakefile +0 -0
  24. data/spec/rails_app/app/controllers/application_controller.rb +7 -0
  25. data/{test → spec}/rails_app/app/controllers/posts_controller.rb +0 -0
  26. data/{test → spec}/rails_app/app/helpers/application_helper.rb +0 -0
  27. data/{test → spec}/rails_app/app/helpers/posts_helper.rb +0 -0
  28. data/{test → spec}/rails_app/app/models/post.rb +0 -0
  29. data/{test → spec}/rails_app/app/models/user.rb +0 -0
  30. data/{test → spec}/rails_app/app/views/layouts/application.html.erb +0 -0
  31. data/{test → spec}/rails_app/app/views/posts/index.html.erb +0 -0
  32. data/{test → spec}/rails_app/config.ru +0 -0
  33. data/{test → spec}/rails_app/config/application.rb +0 -0
  34. data/{test → spec}/rails_app/config/boot.rb +1 -1
  35. data/{test → spec}/rails_app/config/cucumber.yml +0 -0
  36. data/{test → spec}/rails_app/config/database.yml +0 -0
  37. data/{test → spec}/rails_app/config/environment.rb +0 -0
  38. data/{test → spec}/rails_app/config/environments/development.rb +0 -1
  39. data/{test → spec}/rails_app/config/environments/production.rb +0 -0
  40. data/{test → spec}/rails_app/config/environments/test.rb +1 -1
  41. data/{test → spec}/rails_app/config/initializers/backtrace_silencers.rb +0 -0
  42. data/spec/rails_app/config/initializers/devise.rb +242 -0
  43. data/{test → spec}/rails_app/config/initializers/inflections.rb +0 -0
  44. data/{test → spec}/rails_app/config/initializers/mime_types.rb +0 -0
  45. data/{test → spec}/rails_app/config/initializers/secret_token.rb +0 -0
  46. data/{test → spec}/rails_app/config/initializers/session_store.rb +0 -0
  47. data/{test → spec}/rails_app/config/ldap.yml +0 -0
  48. data/{test → spec}/rails_app/config/ldap_with_boolean_ssl.yml +0 -0
  49. data/{test → spec}/rails_app/config/ldap_with_erb.yml +0 -0
  50. data/{test → spec}/rails_app/config/ldap_with_uid.yml +0 -0
  51. data/spec/rails_app/config/locales/devise.en.yml +58 -0
  52. data/{test → spec}/rails_app/config/locales/en.yml +0 -0
  53. data/{test → spec}/rails_app/config/routes.rb +0 -0
  54. data/{test → spec}/rails_app/config/ssl_ldap.yml +0 -0
  55. data/{test → spec}/rails_app/config/ssl_ldap_with_erb.yml +0 -0
  56. data/{test → spec}/rails_app/config/ssl_ldap_with_uid.yml +0 -0
  57. data/{test → spec}/rails_app/db/migrate/20100708120448_devise_create_users.rb +18 -4
  58. data/{test → spec}/rails_app/db/schema.rb +6 -13
  59. data/{test → spec}/rails_app/features/manage_logins.feature +0 -0
  60. data/{test → spec}/rails_app/features/step_definitions/login_steps.rb +0 -0
  61. data/{test → spec}/rails_app/features/step_definitions/web_steps.rb +0 -0
  62. data/{test → spec}/rails_app/features/support/env.rb +0 -0
  63. data/{test → spec}/rails_app/features/support/paths.rb +0 -0
  64. data/{test → spec}/rails_app/lib/tasks/.gitkeep +0 -0
  65. data/{test → spec}/rails_app/lib/tasks/cucumber.rake +0 -0
  66. data/{test → spec}/rails_app/public/404.html +0 -0
  67. data/{test → spec}/rails_app/public/422.html +0 -0
  68. data/{test → spec}/rails_app/public/500.html +0 -0
  69. data/{test → spec}/rails_app/public/images/rails.png +0 -0
  70. data/{test → spec}/rails_app/public/javascripts/application.js +0 -0
  71. data/{test → spec}/rails_app/public/javascripts/controls.js +0 -0
  72. data/{test → spec}/rails_app/public/javascripts/dragdrop.js +0 -0
  73. data/{test → spec}/rails_app/public/javascripts/effects.js +0 -0
  74. data/{test → spec}/rails_app/public/javascripts/prototype.js +0 -0
  75. data/{test → spec}/rails_app/public/javascripts/rails.js +0 -0
  76. data/{test → spec}/rails_app/public/stylesheets/.gitkeep +0 -0
  77. data/{test → spec}/rails_app/script/cucumber +0 -0
  78. data/{test → spec}/rails_app/script/rails +0 -0
  79. data/spec/spec_helper.rb +47 -0
  80. data/spec/support/factories.rb +16 -0
  81. data/spec/unit/user_spec.rb +303 -0
  82. metadata +292 -179
  83. data/Gemfile.lock +0 -92
  84. data/rails/init.rb +0 -2
  85. data/test/devise_ldap_authenticatable_test.rb +0 -8
  86. data/test/ldap/run-server.sh +0 -10
  87. data/test/ldap/slapd-ssl-test.conf +0 -107
  88. data/test/rails_app/Gemfile +0 -22
  89. data/test/rails_app/Gemfile.lock +0 -159
  90. data/test/rails_app/app/controllers/application_controller.rb +0 -4
  91. data/test/rails_app/config/initializers/devise.rb +0 -140
  92. data/test/rails_app/config/ldap_with_check_membership_off.yml +0 -23
  93. data/test/rails_app/config/ldap_with_check_membership_on.yml +0 -23
  94. data/test/rails_app/config/locales/devise.en.yml +0 -39
  95. data/test/rails_app/db/migrate/20100708120302_create_posts.rb +0 -14
  96. data/test/rails_app/db/seeds.rb +0 -7
  97. data/test/rails_app/test/factories/users.rb +0 -14
  98. data/test/rails_app/test/functional/posts_controller_test.rb +0 -58
  99. data/test/rails_app/test/performance/browsing_test.rb +0 -9
  100. data/test/rails_app/test/test_helper.rb +0 -36
  101. data/test/rails_app/test/unit/helpers/posts_helper_test.rb +0 -4
  102. data/test/rails_app/test/unit/post_test.rb +0 -4
  103. data/test/rails_app/test/unit/user_test.rb +0 -314
  104. data/test/test_helper.rb +0 -3
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 68e24de8c225dff39bf6640f3249b17c28d15a2b
4
+ data.tar.gz: 68335d1a9e0bb5137a1629053f655cc051e019ec
5
+ SHA512:
6
+ metadata.gz: 16cc901376b48de4f8eeefd811e13272dd10763fe0ae66c42b59ca79cb0543c6a9c985142b43d52db3fc4e9c1e7c461bca9f907904664a6f2b25e34d4d191f46
7
+ data.tar.gz: 9344876e0cf44b2b90fefef75d49346f5f0bffb01d2f4868fdfb9a56fcb68b58d0890ab7025acf8233600e98a4cf8b98476e4f09333fefa50d0b6cc986b9d933
data/.gitignore CHANGED
@@ -1,4 +1,5 @@
1
1
  .bundle
2
+ Gemfile.lock
2
3
  log
3
4
  *.sqlite3
4
5
  test/ldap/openldap-data/*
data/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ CHANGELOG
2
+ =========
3
+
4
+ v0.8
5
+ ----
6
+
7
+ [Issue #102](https://github.com/cschiewek/devise_ldap_authenticatable/pull/102): Extract method in_group? from in_required_groups? and expose it to the model
data/Gemfile CHANGED
@@ -2,10 +2,7 @@ source "http://rubygems.org"
2
2
 
3
3
  gemspec
4
4
 
5
- gem 'devise', '~> 2.0.0'
6
- gem 'net-ldap', '~> 0.2.2'
7
-
8
- group :test do
5
+ group :development, :test do
9
6
  gem 'ruby-debug', '>= 0.10.3', :platform => :mri_18
10
7
  gem 'debugger', :platform => :ruby_19
11
8
  end
data/README.md CHANGED
@@ -1,39 +1,24 @@
1
1
  Devise LDAP Authenticatable
2
2
  ===========================
3
-
4
3
  Devise LDAP Authenticatable is a LDAP based authentication strategy for the [Devise](http://github.com/plataformatec/devise) authentication framework.
5
4
 
6
5
  If you are building applications for use within your organization which require authentication and you want to use LDAP, this plugin is for you.
7
6
 
8
- For a screencast with an example application, please visit: [http://random-rails.blogspot.com/2010/07/ldap-authentication-with-devise.html](http://random-rails.blogspot.com/2010/07/ldap-authentication-with-devise.html)
9
-
10
- **_Please Note_**
11
-
12
- If you are using rails 2.x then use 0.1.x series of gem, and see the rails2 branch README for instructions.
13
-
14
- Requirements
15
- ------------
16
-
17
- - An LDAP server (tested on OpenLDAP)
18
- - Rails 3.0.0
19
-
20
- These gems are dependencies of the gem:
21
-
22
- - Devise ~> 2.0.0
23
- - net-ldap ~> 0.2.2
7
+ Devise LDAP Authenticatable works in replacement of Database Authenticatable. This devise plugin has not been tested with DatabaseAuthenticatable enabled at the same time. This is meant as a drop in replacement for DatabaseAuthenticatable allowing for a semi single sign on approach.
24
8
 
25
- Installation
26
- ------------
27
-
28
- **_Please Note_**
9
+ For a screencast with an example application, please visit: [http://random-rails.blogspot.com/2010/07/ldap-authentication-with-devise.html](http://random-rails.blogspot.com/2010/07/ldap-authentication-with-devise.html)
29
10
 
30
- This will *only* work for Rails 3 applications.
11
+ Prerequisites
12
+ -------------
13
+ * devise ~> 2.0.0 (which requires rails ~> 3.1)
14
+ * net-ldap ~> 0.2.2
31
15
 
16
+ Usage
17
+ -----
32
18
  In the Gemfile for your application:
33
19
 
34
- gem "devise", "~> 2.0"
35
20
  gem "devise_ldap_authenticatable"
36
-
21
+
37
22
  To get the latest version, pull directly from github instead of the gem:
38
23
 
39
24
  gem "devise_ldap_authenticatable", :git => "git://github.com/cschiewek/devise_ldap_authenticatable.git"
@@ -41,13 +26,12 @@ To get the latest version, pull directly from github instead of the gem:
41
26
 
42
27
  Setup
43
28
  -----
44
-
45
29
  Run the rails generators for devise (please check the [devise](http://github.com/plataformatec/devise) documents for further instructions)
46
30
 
47
31
  rails generate devise:install
48
32
  rails generate devise MODEL_NAME
49
33
 
50
- Run the rails generator for devise_ldap_authenticatable
34
+ Run the rails generator for `devise_ldap_authenticatable`
51
35
 
52
36
  rails generate devise_ldap_authenticatable:install [options]
53
37
 
@@ -59,120 +43,89 @@ Options:
59
43
  # Default: user
60
44
  [--update-model] # Update model to change from database_authenticatable to ldap_authenticatable
61
45
  # Default: true
62
- [--add-rescue] # Update Application Controller with resuce_from for DeviseLdapAuthenticatable::LdapException
46
+ [--add-rescue] # Update Application Controller with rescue_from for DeviseLdapAuthenticatable::LdapException
63
47
  # Default: true
64
48
  [--advanced] # Add advanced config options to the devise initializer
65
49
 
66
-
67
- Usage
68
- -----
69
-
70
- Devise LDAP Authenticatable works in replacement of Database Authenticatable
71
-
72
- **_Please Note_**
73
-
74
- This devise plugin has not been tested with DatabaseAuthenticatable enabled at the same time. This is meant as a drop in replacement for DatabaseAuthenticatable allowing for a semi single sign on approach.
75
-
76
- The field that is used for logins is the first key that's configured in the `config/devise.rb` file under `config.authentication_keys`, which by default is email. For help changing this, please see the [Railscast](http://railscasts.com/episodes/210-customizing-devise) that goes through how to customize Devise.
77
-
78
-
79
50
  Querying LDAP
80
- ----------------
81
-
82
- Given that ldap\_create\_user is set to true and you are authenticating with username, you can query an LDAP server for other attributes.
51
+ -------------
52
+ Given that `ldap_create_user` is set to true and you are authenticating with username, you can query an LDAP server for other attributes.
83
53
 
84
54
  in your user model:
85
55
 
86
- before_save :get_ldap_email
87
-
88
- def get_ldap_email
89
- self.email = Devise::LdapAdapter.get_ldap_param(self.username,"mail")
90
- end
56
+ before_save :get_ldap_email
91
57
 
58
+ def get_ldap_email
59
+ self.email = Devise::LdapAdapter.get_ldap_param(self.username,"mail")
60
+ end
92
61
 
93
62
  Configuration
94
63
  -------------
95
-
96
64
  In initializer `config/initializers/devise.rb` :
97
65
 
98
- * ldap\_logger _(default: true)_
66
+ * `ldap_logger` _(default: true)_
99
67
  * If set to true, will log LDAP queries to the Rails logger.
100
68
 
101
- * ldap\_create\_user _(default: false)_
102
- * If set to true, all valid LDAP users will be allowed to login and an appropriate user record will be created.
69
+ * `ldap_create_user` _(default: false)_
70
+ * If set to true, all valid LDAP users will be allowed to login and an appropriate user record will be created.
103
71
  If set to false, you will have to create the user record before they will be allowed to login.
104
72
 
105
- * ldap\_config _(default: #{Rails.root}/config/ldap.yml)_
73
+ * `ldap_config` _(default: #{Rails.root}/config/ldap.yml)_
106
74
  * Where to find the LDAP config file. Commented out to use the default, change if needed.
107
75
 
108
- * ldap\_update\_password _(default: true)_
76
+ * `ldap_update_password` _(default: true)_
109
77
  * When doing password resets, if true will update the LDAP server. Requires admin password in the ldap.yml
110
78
 
111
- * ldap\_check\_group_membership _(default: false)_
79
+ * `ldap_check_group_membership` _(default: false)_
112
80
  * When set to true, the user trying to login will be checked to make sure they are in all of groups specified in the ldap.yml file.
113
81
 
114
- * ldap\_check\_attributes _(default: false)_
82
+ * `ldap_check_attributes` _(default: false)_
115
83
  * When set to true, the user trying to login will be checked to make sure they have all of the attributes in the ldap.yml file.
116
84
 
117
- * ldap\_use\_admin\_to\_bind _(default: false)_
85
+ * `ldap_use_admin_to_bind` _(default: false)_
118
86
  * When set to true, the admin user will be used to bind to the LDAP server during authentication.
119
87
 
120
-
121
88
  Advanced Configuration
122
89
  ----------------------
123
-
124
90
  These parameters will be added to `config/initializers/devise.rb` when you pass the `--advanced` switch to the generator:
125
91
 
126
- * ldap\_auth\_username\_builder _(default: `Proc.new() {|attribute, login, ldap| "#{attribute}=#{login},#{ldap.base}" }`)_
92
+ * `ldap_auth_username_builder` _(default: `Proc.new() {|attribute, login, ldap| "#{attribute}=#{login},#{ldap.base}" }`)_
127
93
  * You can pass a proc to the username option to explicitly specify the format that you search for a users' DN on your LDAP server.
128
94
 
129
- Testing
130
- -------
131
-
132
- This has been tested using the following setup:
95
+ Troubleshooting
96
+ --------------
97
+ **Using a "username" instead of an "email":** The field that is used for logins is the first key that's configured in the `config/devise.rb` file under `config.authentication_keys`, which by default is email. For help changing this, please see the [Railscast](http://railscasts.com/episodes/210-customizing-devise) that goes through how to customize Devise.
133
98
 
134
- * Mac OSX 10.6
135
- * OpenLDAP 2.4.11
136
- * REE 1.8.7 (2010.02)
99
+ **SSL certificate invalid:** If you're using a test LDAP server running a self-signed SSL certificate, make sure the appropriate root certificate is installed on your system. Alternately, you may temporarily disable certificate checking for SSL by modifying your system LDAP configuration (e.g., `/etc/openldap/ldap.conf` or `/etc/ldap/ldap.conf`) to read `TLS_REQCERT never`.
137
100
 
138
- All unit and functional tests are part of a sample rails application under test/rails_app and requires a working LDAP sever.
101
+ Development guide
102
+ ------------
103
+ To contribute to `devise_ldap_authentication`, you should be able to run a test OpenLDAP server. Specifically, you need the `slapd`, `ldapadd`, and `ldapmodify` binaries.
139
104
 
140
- Build / Start Instructions for Test LDAP Server
141
- -----------------------------------------------
105
+ This seems to come out of the box with Mac OS X 10.6.
142
106
 
143
- These instructions require the current directory context to be the `test/ldap` directory relative to the project root.
107
+ On Ubuntu (tested on 12.04 and 12.10), you can run `sudo apt-get install slapd ldap-utils`. You will also likely have to add the `spec/ldap` directory of your local git clone to the slapd [apparmor](https://wiki.ubuntu.com/DebuggingApparmor) profile `/etc/apparmor.d/usr.sbin.slapd` if you get permissions errors. Something like this should do:
144
108
 
145
- 1. To start the server, run `./run-server.sh`
146
- 2. Add the basic structure: `ldapadd -x -h localhost -p 3389 -x -D "cn=admin,dc=test,dc=com" -w secret -f base.ldif`
147
- * this creates the users / passwords:
148
- * cn=admin,dc=test,com / secret
149
- * cn=example.user@test.com,ou=people,dc=test,dc=com / secret
150
- 3. You should now be able to run the tests in test/rails_app by running: `rake`
151
-
152
- _For a LDAP server running SSL_
153
-
154
- 1. To start the server, run: `./run-server.sh --ssl`
155
- 2. Add the basic structure: `ldapadd -x -H ldaps://localhost:3389 -x -D "cn=admin,dc=test,dc=com" -w secret -f base.ldif`
156
- * this creates the users / passwords:
157
- * cn=admin,dc=test,com / secret
158
- * cn=example.user@test.com,ou=people,dc=test,dc=com / secret
159
- 3. You should now be able to run the tests in test/rails_app by running: `LDAP_SSL=true rake`
109
+ /path/to/devise_ldap_authenticatable/spec/ldap/** rw,$
160
110
 
161
- **_Please Note_**
111
+ To start hacking on `devise_ldap_authentication`, clone the github repository, start the test LDAP server, and run the rake test task:
162
112
 
163
- In your system LDAP config file (on OSX it's /etc/openldap/ldap.conf) make sure you have the following setting:
113
+ git clone https://github.com/cschiewek/devise_ldap_authenticatable.git
114
+ cd devise_ldap_authenticatable
115
+ bundle install
164
116
 
165
- TLS_REQCERT never
117
+ # in a separate console or backgrounded
118
+ ./spec/ldap/run-server
166
119
 
167
- This will allow requests to go to the test LDAP server without being signed by a trusted root (it uses a self-signed cert)
120
+ bundle exec rake db:migrate # first time only
121
+ bundle exec rake spec
168
122
 
169
123
  References
170
124
  ----------
171
-
172
125
  * [OpenLDAP](http://www.openldap.org/)
173
126
  * [Devise](http://github.com/plataformatec/devise)
174
127
  * [Warden](http://github.com/hassox/warden)
175
128
 
176
129
  Released under the MIT license
177
130
 
178
- Copyright (c) 2010 Curtis Schiewek, Daniel McNevin, Steven Xu
131
+ Copyright (c) 2012 [Curtis Schiewek](https://github.com/cschiewek), [Daniel McNevin](https://github.com/dpmcnevin), [Steven Xu](https://github.com/cairo140)
data/Rakefile CHANGED
@@ -1,15 +1,16 @@
1
- require 'rake'
2
- require 'rake/testtask'
3
- require 'rake/rdoctask'
1
+ require File.expand_path('spec/rails_app/config/environment', File.dirname(__FILE__))
2
+ require 'rdoc/task'
4
3
 
5
- desc 'Default: run unit tests.'
6
- task :default => :test
4
+ desc 'Default: run test suite.'
5
+ task :default => :spec
7
6
 
8
7
  desc 'Generate documentation for the devise_ldap_authenticatable plugin.'
9
8
  Rake::RDocTask.new(:rdoc) do |rdoc|
10
9
  rdoc.rdoc_dir = 'rdoc'
11
10
  rdoc.title = 'DeviseLDAPAuthenticatable'
12
11
  rdoc.options << '--line-numbers' << '--inline-source'
13
- rdoc.rdoc_files.include('README')
12
+ rdoc.rdoc_files.include('README.md')
14
13
  rdoc.rdoc_files.include('lib/**/*.rb')
15
- end
14
+ end
15
+
16
+ RailsApp::Application.load_tasks
@@ -17,6 +17,18 @@ Gem::Specification.new do |s|
17
17
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
18
  s.require_paths = ["lib"]
19
19
 
20
- s.add_dependency('devise', '>= 2.0.0')
21
- s.add_dependency('net-ldap', '~> 0.2.2')
22
- end
20
+ s.add_dependency('devise', '~> 2.0')
21
+ s.add_dependency('net-ldap', '~> 0.3.1')
22
+
23
+ s.add_development_dependency('rake', '>= 0.9')
24
+ s.add_development_dependency('rdoc', '>= 3')
25
+ s.add_development_dependency('rails', '>= 3.2')
26
+ s.add_development_dependency('sqlite3')
27
+ s.add_development_dependency('factory_girl_rails', '~> 1.0')
28
+ s.add_development_dependency('factory_girl', '~> 2.0')
29
+ s.add_development_dependency('rspec-rails')
30
+
31
+ %w{database_cleaner capybara launchy}.each do |dep|
32
+ s.add_development_dependency(dep)
33
+ end
34
+ end
@@ -1,8 +1,8 @@
1
1
  require "net/ldap"
2
2
 
3
3
  module Devise
4
-
5
4
  module LdapAdapter
5
+ DEFAULT_GROUP_UNIQUE_MEMBER_LIST_KEY = 'uniqueMember'
6
6
 
7
7
  def self.valid_credentials?(login, password_plaintext)
8
8
  options = {:login => login,
@@ -25,7 +25,7 @@ module Devise
25
25
  end
26
26
 
27
27
  def self.update_own_password(login, new_password, current_password)
28
- set_ldap_param(login, :userpassword, new_password, current_password)
28
+ set_ldap_param(login, :userpassword, Net::LDAP::Password.generate(:sha, new_password), current_password)
29
29
  end
30
30
 
31
31
  def self.ldap_connect(login)
@@ -44,6 +44,10 @@ module Devise
44
44
  self.ldap_connect(login).user_groups
45
45
  end
46
46
 
47
+ def self.in_ldap_group?(login, group_name, group_attribute = nil)
48
+ self.ldap_connect(login).in_group?(group_name, group_attribute)
49
+ end
50
+
47
51
  def self.get_dn(login)
48
52
  self.ldap_connect(login).dn
49
53
  end
@@ -154,7 +158,18 @@ module Devise
154
158
 
155
159
  def authorized?
156
160
  DeviseLdapAuthenticatable::Logger.send("Authorizing user #{dn}")
157
- authenticated? && in_required_groups? && has_required_attribute?
161
+ if !authenticated?
162
+ DeviseLdapAuthenticatable::Logger.send("Not authorized because not authenticated.")
163
+ return false
164
+ elsif !in_required_groups?
165
+ DeviseLdapAuthenticatable::Logger.send("Not authorized because not in required groups.")
166
+ return false
167
+ elsif !has_required_attribute?
168
+ DeviseLdapAuthenticatable::Logger.send("Not authorized because does not have required attribute.")
169
+ return false
170
+ else
171
+ return true
172
+ end
158
173
  end
159
174
 
160
175
  def change_password!
@@ -167,37 +182,44 @@ module Devise
167
182
  ## FIXME set errors here, the ldap.yml isn't set properly.
168
183
  return false if @required_groups.nil?
169
184
 
170
- admin_ldap = LdapConnect.admin
171
-
172
185
  for group in @required_groups
173
186
  if group.is_a?(Array)
174
- group_attribute, group_name = group
187
+ return false unless in_group?(group[1], group[0])
175
188
  else
176
- group_attribute = "uniqueMember"
177
- group_name = group
189
+ return false unless in_group?(group)
178
190
  end
179
- unless ::Devise.ldap_ad_group_check
180
- admin_ldap.search(:base => group_name, :scope => Net::LDAP::SearchScope_BaseObject) do |entry|
181
- unless entry[group_attribute].include? dn
182
- DeviseLdapAuthenticatable::Logger.send("User #{dn} is not in group: #{group_name }")
183
- return false
184
- end
185
- end
186
- else
187
- # AD optimization - extension will recursively check sub-groups with one query
188
- # "(memberof:1.2.840.113556.1.4.1941:=group_name)"
189
- search_result = admin_ldap.search(:base => dn,
190
- :filter => Net::LDAP::Filter.ex("memberof:1.2.840.113556.1.4.1941", group_name),
191
- :scope => Net::LDAP::SearchScope_BaseObject)
192
- # Will return the user entry if belongs to group otherwise nothing
193
- unless search_result.length == 1 && search_result[0].dn.eql?(dn)
194
- DeviseLdapAuthenticatable::Logger.send("User #{dn} is not in group: #{group_name }")
195
- return false
191
+ end
192
+ return true
193
+ end
194
+
195
+ def in_group?(group_name, group_attribute = DEFAULT_GROUP_UNIQUE_MEMBER_LIST_KEY)
196
+ in_group = false
197
+
198
+ admin_ldap = LdapConnect.admin
199
+
200
+ unless ::Devise.ldap_ad_group_check
201
+ admin_ldap.search(:base => group_name, :scope => Net::LDAP::SearchScope_BaseObject) do |entry|
202
+ if entry[group_attribute].include? dn
203
+ in_group = true
196
204
  end
197
205
  end
206
+ else
207
+ # AD optimization - extension will recursively check sub-groups with one query
208
+ # "(memberof:1.2.840.113556.1.4.1941:=group_name)"
209
+ search_result = admin_ldap.search(:base => dn,
210
+ :filter => Net::LDAP::Filter.ex("memberof:1.2.840.113556.1.4.1941", group_name),
211
+ :scope => Net::LDAP::SearchScope_BaseObject)
212
+ # Will return the user entry if belongs to group otherwise nothing
213
+ if search_result.length == 1 && search_result[0].dn.eql?(dn)
214
+ in_group = true
215
+ end
198
216
  end
199
217
 
200
- return true
218
+ unless in_group
219
+ DeviseLdapAuthenticatable::Logger.send("User #{dn} is not in group: #{group_name}")
220
+ end
221
+
222
+ return in_group
201
223
  end
202
224
 
203
225
  def has_required_attribute?
@@ -236,7 +258,9 @@ module Devise
236
258
  DeviseLdapAuthenticatable::Logger.send("LDAP search for login: #{@attribute}=#{@login}")
237
259
  filter = Net::LDAP::Filter.eq(@attribute.to_s, @login.to_s)
238
260
  ldap_entry = nil
239
- @ldap.search(:filter => filter) {|entry| ldap_entry = entry}
261
+ match_count = 0
262
+ @ldap.search(:filter => filter) {|entry| ldap_entry = entry; match_count+=1}
263
+ DeviseLdapAuthenticatable::Logger.send("LDAP search yielded #{match_count} matches")
240
264
  ldap_entry
241
265
  end
242
266
 
@@ -1,4 +1,4 @@
1
- require 'devise_ldap_authenticatable/strategy'
1
+ require 'devise_ldap_authenticatable/strategy'
2
2
 
3
3
  module Devise
4
4
  module Models
@@ -53,6 +53,10 @@ module Devise
53
53
  Devise::LdapAdapter.get_groups(login_with)
54
54
  end
55
55
 
56
+ def in_ldap_group?(group_name, group_attribute = LdapAdapter::DEFAULT_GROUP_UNIQUE_MEMBER_LIST_KEY)
57
+ Devise::LdapAdapter.in_ldap_group?(login_with, group_name, group_attribute)
58
+ end
59
+
56
60
  def ldap_dn
57
61
  Devise::LdapAdapter.get_dn(login_with)
58
62
  end