diff options
Diffstat (limited to 'app')
-rw-r--r-- | app/controllers/omniauth_callbacks_controller.rb | 13 | ||||
-rw-r--r-- | app/helpers/application_helper.rb | 4 | ||||
-rw-r--r-- | app/models/user.rb | 22 | ||||
-rw-r--r-- | app/views/devise/sessions/new.html.erb | 3 |
4 files changed, 41 insertions, 1 deletions
diff --git a/app/controllers/omniauth_callbacks_controller.rb b/app/controllers/omniauth_callbacks_controller.rb new file mode 100644 index 00000000000..b79abf2c693 --- /dev/null +++ b/app/controllers/omniauth_callbacks_controller.rb @@ -0,0 +1,13 @@ +class OmniauthCallbacksController < Devise::OmniauthCallbacksController + + def ldap + # We only find ourselves here if the authentication to LDAP was successful. + omniauth = request.env["omniauth.auth"]["extra"]["raw_info"] + @user = User.find_for_ldap_auth(omniauth) + if @user.persisted? + @user.remember_me = true + end + sign_in_and_redirect @user + end + +end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 9f0f1e68954..277f90367a6 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -93,4 +93,8 @@ module ApplicationHelper def help_layout controller.controller_name == "help" end + + def ldap_enable? + Devise.omniauth_providers.include?(:ldap) + end end diff --git a/app/models/user.rb b/app/models/user.rb index a13a6f77870..fbac05108e6 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,26 @@ 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 + + def self.find_for_ldap_auth(omniauth) + username = omniauth.sAMAccountName[0] + email = omniauth.userprincipalname[0] + + if @user = User.find_by_email(email) + @user + else + password = generate_random_password + @user = User.create(:name => username, + :email => email, + :password => password, + :password_confirmation => password + ) + end + end end # == Schema Information # diff --git a/app/views/devise/sessions/new.html.erb b/app/views/devise/sessions/new.html.erb index c17ff3f9914..f5bd9575705 100644 --- a/app/views/devise/sessions/new.html.erb +++ b/app/views/devise/sessions/new.html.erb @@ -9,4 +9,7 @@ <br/> <%= f.submit "Sign in", :class => "grey-button" %> <div class="right"> <%= render :partial => "devise/shared/links" %></div> + <% if ldap_enable? -%> + <p><%= link_to "via LDAP", user_omniauth_authorize_path(:ldap)%></p> + <% end -%> <% end %> |