Commit d6a0b8f42881964b9e57ad1dde4918e74aea0ed0
1 parent
d885f24f
Exists in
master
and in
4 other branches
LDAP done
Showing
6 changed files
with
37 additions
and
24 deletions
Show diff stats
| @@ -0,0 +1,13 @@ | @@ -0,0 +1,13 @@ | ||
| 1 | +class OmniauthCallbacksController < Devise::OmniauthCallbacksController | ||
| 2 | + | ||
| 3 | + def ldap | ||
| 4 | + # We only find ourselves here if the authentication to LDAP was successful. | ||
| 5 | + omniauth = request.env["omniauth.auth"]["extra"]["raw_info"] | ||
| 6 | + @user = User.find_for_ldap_auth(omniauth) | ||
| 7 | + if @user.persisted? | ||
| 8 | + @user.remember_me = true | ||
| 9 | + end | ||
| 10 | + sign_in_and_redirect @user | ||
| 11 | + end | ||
| 12 | + | ||
| 13 | +end |
app/controllers/users/omniauth_callbacks_controller.rb
| @@ -1,22 +0,0 @@ | @@ -1,22 +0,0 @@ | ||
| 1 | -class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController | ||
| 2 | - | ||
| 3 | - def ldap | ||
| 4 | - # We only find ourselves here if the authentication to LDAP was successful. | ||
| 5 | - ldap = request.env["omniauth.auth"]["extra"]["raw_info"] | ||
| 6 | - username = ldap.sAMAccountName[0].to_s | ||
| 7 | - email = ldap.proxyaddresses[0][5..-1].to_s | ||
| 8 | - | ||
| 9 | - if @user = User.find_by_email(email) | ||
| 10 | - sign_in_and_redirect root_path | ||
| 11 | - else | ||
| 12 | - password = User.generate_random_password | ||
| 13 | - @user = User.create(:name => username, | ||
| 14 | - :email => email, | ||
| 15 | - :password => password, | ||
| 16 | - :password_confirmation => password | ||
| 17 | - ) | ||
| 18 | - sign_in_and_redirect @user | ||
| 19 | - end | ||
| 20 | - end | ||
| 21 | - | ||
| 22 | -end |
app/helpers/application_helper.rb
| @@ -93,4 +93,8 @@ module ApplicationHelper | @@ -93,4 +93,8 @@ module ApplicationHelper | ||
| 93 | def help_layout | 93 | def help_layout |
| 94 | controller.controller_name == "help" | 94 | controller.controller_name == "help" |
| 95 | end | 95 | end |
| 96 | + | ||
| 97 | + def ldap_enable? | ||
| 98 | + Devise.omniauth_providers.include?(:ldap) | ||
| 99 | + end | ||
| 96 | end | 100 | end |
app/models/user.rb
| @@ -66,6 +66,22 @@ class User < ActiveRecord::Base | @@ -66,6 +66,22 @@ class User < ActiveRecord::Base | ||
| 66 | def self.generate_random_password | 66 | def self.generate_random_password |
| 67 | (0...8).map{ ('a'..'z').to_a[rand(26)] }.join | 67 | (0...8).map{ ('a'..'z').to_a[rand(26)] }.join |
| 68 | end | 68 | end |
| 69 | + | ||
| 70 | + def self.find_for_ldap_auth(omniauth) | ||
| 71 | + username = omniauth.sAMAccountName[0] | ||
| 72 | + email = omniauth.userprincipalname[0] | ||
| 73 | + | ||
| 74 | + if @user = User.find_by_email(email) | ||
| 75 | + @user | ||
| 76 | + else | ||
| 77 | + password = generate_random_password | ||
| 78 | + @user = User.create(:name => username, | ||
| 79 | + :email => email, | ||
| 80 | + :password => password, | ||
| 81 | + :password_confirmation => password | ||
| 82 | + ) | ||
| 83 | + end | ||
| 84 | + end | ||
| 69 | end | 85 | end |
| 70 | # == Schema Information | 86 | # == Schema Information |
| 71 | # | 87 | # |
app/views/devise/sessions/new.html.erb
| @@ -9,5 +9,7 @@ | @@ -9,5 +9,7 @@ | ||
| 9 | <br/> | 9 | <br/> |
| 10 | <%= f.submit "Sign in", :class => "grey-button" %> | 10 | <%= f.submit "Sign in", :class => "grey-button" %> |
| 11 | <div class="right"> <%= render :partial => "devise/shared/links" %></div> | 11 | <div class="right"> <%= render :partial => "devise/shared/links" %></div> |
| 12 | - <%= user_omniauth_authorize_path(:ldap)%> | 12 | + <% if ldap_enable? -%> |
| 13 | + <p><%= link_to "via LDAP", user_omniauth_authorize_path(:ldap)%></p> | ||
| 14 | + <% end -%> | ||
| 13 | <% end %> | 15 | <% end %> |
config/routes.rb
| @@ -39,7 +39,7 @@ Gitlab::Application.routes.draw do | @@ -39,7 +39,7 @@ Gitlab::Application.routes.draw do | ||
| 39 | resources :projects, :constraints => { :id => /[^\/]+/ }, :only => [:new, :create, :index] | 39 | resources :projects, :constraints => { :id => /[^\/]+/ }, :only => [:new, :create, :index] |
| 40 | resources :keys | 40 | resources :keys |
| 41 | 41 | ||
| 42 | - devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" } | 42 | + devise_for :users, :controllers => { :omniauth_callbacks => :omniauth_callbacks } |
| 43 | 43 | ||
| 44 | resources :projects, :constraints => { :id => /[^\/]+/ }, :except => [:new, :create, :index], :path => "/" do | 44 | resources :projects, :constraints => { :id => /[^\/]+/ }, :except => [:new, :create, :index], :path => "/" do |
| 45 | member do | 45 | member do |