diff --git a/Gemfile b/Gemfile
index b80dca1..801b0ae 100644
--- a/Gemfile
+++ b/Gemfile
@@ -28,6 +28,7 @@ gem "resque"
gem "httparty"
gem "charlock_holmes"
gem "foreman"
+gem "omniauth-ldap"
group :assets do
gem "sass-rails", "~> 3.1.0"
diff --git a/Gemfile.lock b/Gemfile.lock
index ec171d2..89d92ae 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -115,6 +115,7 @@ GEM
haml (~> 3.0)
railties (~> 3.0)
hashery (1.4.0)
+ hashie (1.2.0)
hike (1.2.1)
httparty (0.8.1)
multi_json
@@ -141,12 +142,22 @@ GEM
mime-types (1.17.2)
multi_json (1.0.3)
multi_xml (0.4.1)
+ net-ldap (0.2.2)
nokogiri (1.5.0)
+ omniauth (1.0.2)
+ hashie (~> 1.2)
+ rack
+ omniauth-ldap (1.0.2)
+ net-ldap (~> 0.2.2)
+ omniauth (~> 1.0)
+ pyu-ruby-sasl (~> 0.0.3.1)
+ rubyntlm (~> 0.1.1)
orm_adapter (0.0.5)
polyglot (0.3.3)
posix-spawn (0.3.6)
pygments.rb (0.2.4)
rubypython (~> 0.5.3)
+ pyu-ruby-sasl (0.0.3.3)
rack (1.3.5)
rack-cache (1.1)
rack (>= 0.4)
@@ -210,6 +221,7 @@ GEM
ruby-debug-base19 (>= 0.11.19)
ruby_core_source (0.1.5)
archive-tar-minitar (>= 0.5.2)
+ rubyntlm (0.1.1)
rubypython (0.5.3)
blankslate (>= 2.1.2.3)
ffi (~> 1.0.7)
@@ -306,6 +318,7 @@ DEPENDENCIES
kaminari
launchy
letter_opener
+ omniauth-ldap
pygments.rb (= 0.2.4)
rails (= 3.1.1)
rails-footnotes (~> 3.7.5)
diff --git a/app/assets/javascripts/users/omniauth_callbacks.js.coffee b/app/assets/javascripts/users/omniauth_callbacks.js.coffee
new file mode 100644
index 0000000..7615679
--- /dev/null
+++ b/app/assets/javascripts/users/omniauth_callbacks.js.coffee
@@ -0,0 +1,3 @@
+# Place all the behaviors and hooks related to the matching controller here.
+# All this logic will automatically be available in application.js.
+# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
diff --git a/app/assets/stylesheets/users/omniauth_callbacks.css.scss b/app/assets/stylesheets/users/omniauth_callbacks.css.scss
new file mode 100644
index 0000000..155643a
--- /dev/null
+++ b/app/assets/stylesheets/users/omniauth_callbacks.css.scss
@@ -0,0 +1,3 @@
+// Place all the styles related to the Users::OmniauthCallbacks controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
diff --git a/app/controllers/users/omniauth_callbacks_controller.rb b/app/controllers/users/omniauth_callbacks_controller.rb
new file mode 100644
index 0000000..19c3879
--- /dev/null
+++ b/app/controllers/users/omniauth_callbacks_controller.rb
@@ -0,0 +1,22 @@
+class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
+
+ def ldap
+ # We only find ourselves here if the authentication to LDAP was successful.
+ ldap = request.env["omniauth.auth"]["extra"]["raw_info"]
+ username = ldap.sAMAccountName[0].to_s
+ email = ldap.proxyaddresses[0][5..-1].to_s
+
+ if @user = User.find_by_email(email)
+ sign_in_and_redirect root_path
+ else
+ password = User.generate_random_password
+ @user = User.create(:name => username,
+ :email => email,
+ :password => password,
+ :password_confirmation => password
+ )
+ sign_in_and_redirect @user
+ end
+ end
+
+end
diff --git a/app/helpers/users/omniauth_callbacks_helper.rb b/app/helpers/users/omniauth_callbacks_helper.rb
new file mode 100644
index 0000000..7c6e5e0
--- /dev/null
+++ b/app/helpers/users/omniauth_callbacks_helper.rb
@@ -0,0 +1,2 @@
+module Users::OmniauthCallbacksHelper
+end
diff --git a/app/models/user.rb b/app/models/user.rb
index a13a6f7..1123c00 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -2,7 +2,7 @@ class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :token_authenticatable,
- :recoverable, :rememberable, :trackable, :validatable
+ :recoverable, :rememberable, :trackable, :validatable, :omniauthable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me,
@@ -62,6 +62,10 @@ class User < ActiveRecord::Base
def last_activity_project
projects.first
end
+
+ def self.generate_random_password
+ (0...8).map{ ('a'..'z').to_a[rand(26)] }.join
+ end
end
# == Schema Information
#
diff --git a/app/views/devise/sessions/new.html.erb b/app/views/devise/sessions/new.html.erb
index c17ff3f..6ed3edd 100644
--- a/app/views/devise/sessions/new.html.erb
+++ b/app/views/devise/sessions/new.html.erb
@@ -9,4 +9,5 @@
<%= f.submit "Sign in", :class => "grey-button" %>