Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorValery Sizov <vsv2711@gmail.com>2012-01-21 22:36:14 +0400
committervsizov <vsv2711@gmail.com>2012-01-28 14:11:21 +0400
commitc083df0570d6fa83e6e302feff67ab65db15fe04 (patch)
treee9a7856a5a89c3c430589adf42cf492de790751c
parentb4cc04d7e17eeefe6d89bbb72661a43d7d9e3e2e (diff)
LDAP BASE IMPLEMENRTATION
-rw-r--r--Gemfile1
-rw-r--r--Gemfile.lock13
-rw-r--r--app/assets/javascripts/users/omniauth_callbacks.js.coffee3
-rw-r--r--app/assets/stylesheets/users/omniauth_callbacks.css.scss3
-rw-r--r--app/controllers/users/omniauth_callbacks_controller.rb22
-rw-r--r--app/helpers/users/omniauth_callbacks_helper.rb2
-rw-r--r--app/models/user.rb6
-rw-r--r--app/views/devise/sessions/new.html.erb1
-rw-r--r--config/initializers/devise.rb9
-rw-r--r--config/routes.rb2
-rw-r--r--spec/controllers/users/omniauth_callbacks_controller_spec.rb5
-rw-r--r--spec/helpers/users/omniauth_callbacks_helper_spec.rb15
12 files changed, 80 insertions, 2 deletions
diff --git a/Gemfile b/Gemfile
index b80dca11534..801b0aeecbb 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 ec171d2c84d..89d92ae4324 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 00000000000..761567942fc
--- /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 00000000000..155643abb7d
--- /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 00000000000..19c3879c896
--- /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 00000000000..7c6e5e0e9b1
--- /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 a13a6f77870..1123c00e69f 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 c17ff3f9914..6ed3edd3b71 100644
--- a/app/views/devise/sessions/new.html.erb
+++ b/app/views/devise/sessions/new.html.erb
@@ -9,4 +9,5 @@
<br/>
<%= f.submit "Sign in", :class => "grey-button" %>
<div class="right"> <%= render :partial => "devise/shared/links" %></div>
+ <%= user_omniauth_authorize_path(:ldap)%>
<% end %>
diff --git a/config/initializers/devise.rb b/config/initializers/devise.rb
index 68bf5a0ea39..ae378b73140 100644
--- a/config/initializers/devise.rb
+++ b/config/initializers/devise.rb
@@ -199,6 +199,15 @@ Devise.setup do |config|
# up on your models and hooks.
# config.omniauth :github, 'APP_ID', 'APP_SECRET', :scope => 'user,public_repo'
+ #config.omniauth :ldap,
+ # :host => 'YOUR_LDAP_SERVER',
+ # :base => 'THE_BASE_WHERE_YOU_SEARCH_FOR_USERS',
+ # :uid => 'sAMAccountName',
+ # :port => 389,
+ # :method => :plain,
+ # :bind_dn => 'THE_FULL_DN_OF_THE_USER_YOU_WILL_BIND_WITH',
+ # :password => 'THE_PASSWORD_OF_THE_BIND_USER'
+
# ==> Warden configuration
# If you want to use other strategies, that are not supported by Devise, or
# change the failure app, you can configure them inside the config.warden block.
diff --git a/config/routes.rb b/config/routes.rb
index bb575356d94..d6951c0eb9e 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -39,7 +39,7 @@ Gitlab::Application.routes.draw do
resources :projects, :constraints => { :id => /[^\/]+/ }, :only => [:new, :create, :index]
resources :keys
- devise_for :users
+ devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }
resources :projects, :constraints => { :id => /[^\/]+/ }, :except => [:new, :create, :index], :path => "/" do
member do
diff --git a/spec/controllers/users/omniauth_callbacks_controller_spec.rb b/spec/controllers/users/omniauth_callbacks_controller_spec.rb
new file mode 100644
index 00000000000..c393cd3d7b8
--- /dev/null
+++ b/spec/controllers/users/omniauth_callbacks_controller_spec.rb
@@ -0,0 +1,5 @@
+require 'spec_helper'
+
+describe Users::OmniauthCallbacksController do
+
+end
diff --git a/spec/helpers/users/omniauth_callbacks_helper_spec.rb b/spec/helpers/users/omniauth_callbacks_helper_spec.rb
new file mode 100644
index 00000000000..589a257097a
--- /dev/null
+++ b/spec/helpers/users/omniauth_callbacks_helper_spec.rb
@@ -0,0 +1,15 @@
+require 'spec_helper'
+
+# Specs in this file have access to a helper object that includes
+# the Users::OmniauthCallbacksHelper. For example:
+#
+# describe Users::OmniauthCallbacksHelper do
+# describe "string concat" do
+# it "concats two strings with spaces" do
+# helper.concat_strings("this","that").should == "this that"
+# end
+# end
+# end
+describe Users::OmniauthCallbacksHelper do
+ pending "add some examples to (or delete) #{__FILE__}"
+end