Commit 366c0065c4302eacbb26ba7292c4c0528dbd33a6
Exists in
master
and in
4 other branches
Merge branch 'better_ldap' of https://github.com/jirutka/gitlabhq into jirutka-better_ldap
Conflicts: app/models/user.rb
Showing
4 changed files
with
36 additions
and
14 deletions
Show diff stats
app/controllers/omniauth_callbacks_controller.rb
... | ... | @@ -12,8 +12,7 @@ class OmniauthCallbacksController < Devise::OmniauthCallbacksController |
12 | 12 | |
13 | 13 | def ldap |
14 | 14 | # We only find ourselves here if the authentication to LDAP was successful. |
15 | - info = request.env["omniauth.auth"]["info"] | |
16 | - @user = User.find_for_ldap_auth(info) | |
15 | + @user = User.find_for_ldap_auth(request.env["omniauth.auth"], current_user) | |
17 | 16 | if @user.persisted? |
18 | 17 | @user.remember_me = true |
19 | 18 | end | ... | ... |
app/models/user.rb
... | ... | @@ -7,7 +7,7 @@ class User < ActiveRecord::Base |
7 | 7 | |
8 | 8 | attr_accessible :email, :password, :password_confirmation, :remember_me, :bio, |
9 | 9 | :name, :projects_limit, :skype, :linkedin, :twitter, :dark_scheme, |
10 | - :theme_id, :force_random_password | |
10 | + :theme_id, :force_random_password, :extern_uid, :provider | |
11 | 11 | |
12 | 12 | attr_accessor :force_random_password |
13 | 13 | |
... | ... | @@ -54,6 +54,8 @@ class User < ActiveRecord::Base |
54 | 54 | |
55 | 55 | validates :bio, length: { within: 0..255 } |
56 | 56 | |
57 | + validates :extern_uid, :allow_blank => true, :uniqueness => {:scope => :provider} | |
58 | + | |
57 | 59 | before_save :ensure_authentication_token |
58 | 60 | alias_attribute :private_token, :authentication_token |
59 | 61 | |
... | ... | @@ -84,21 +86,31 @@ class User < ActiveRecord::Base |
84 | 86 | where('id NOT IN (SELECT DISTINCT(user_id) FROM users_projects)') |
85 | 87 | end |
86 | 88 | |
87 | - def self.find_for_ldap_auth(omniauth_info) | |
88 | - name = omniauth_info.name.force_encoding("utf-8") | |
89 | - email = omniauth_info.email.downcase unless omniauth_info.email.nil? | |
90 | - raise OmniAuth::Error, "LDAP accounts must provide an email address" if email.nil? | |
89 | + def self.find_for_ldap_auth(auth, signed_in_resource=nil) | |
90 | + uid = auth.info.uid | |
91 | + provider = auth.provider | |
92 | + name = auth.info.name.force_encoding("utf-8") | |
93 | + email = auth.info.email.downcase unless auth.info.email.nil? | |
94 | + raise OmniAuth::Error, "LDAP accounts must provide an uid and email address" if uid.nil? or email.nil? | |
91 | 95 | |
92 | - if @user = User.find_by_email(email) | |
96 | + if @user = User.find_by_extern_uid_and_provider(uid, provider) | |
97 | + @user | |
98 | + # workaround for backward compatibility | |
99 | + elsif @user = User.find_by_email(email) | |
100 | + logger.info "Updating legacy LDAP user #{email} with extern_uid => #{uid}" | |
101 | + @user.update_attributes(:extern_uid => uid, :provider => provider) | |
93 | 102 | @user |
94 | 103 | else |
104 | + logger.info "Creating user from LDAP login {uid => #{uid}, name => #{name}, email => #{email}}" | |
95 | 105 | password = Devise.friendly_token[0, 8].downcase |
96 | 106 | @user = User.create( |
97 | - name: name, | |
98 | - email: email, | |
99 | - password: password, | |
100 | - password_confirmation: password, | |
101 | - projects_limit: Gitlab.config.default_projects_limit | |
107 | + :extern_uid => uid, | |
108 | + :provider => provider, | |
109 | + :name => name, | |
110 | + :email => email, | |
111 | + :password => password, | |
112 | + :password_confirmation => password, | |
113 | + :projects_limit => Gitlab.config.default_projects_limit | |
102 | 114 | ) |
103 | 115 | end |
104 | 116 | end | ... | ... |
db/migrate/20120729131232_add_extern_auth_provider_to_users.rb
0 → 100644
db/schema.rb
... | ... | @@ -11,7 +11,7 @@ |
11 | 11 | # |
12 | 12 | # It's strongly recommended to check this file into your version control system. |
13 | 13 | |
14 | -ActiveRecord::Schema.define(:version => 20120712080407) do | |
14 | +ActiveRecord::Schema.define(:version => 20120729131232) do | |
15 | 15 | |
16 | 16 | create_table "events", :force => true do |t| |
17 | 17 | t.string "target_type" |
... | ... | @@ -171,9 +171,12 @@ ActiveRecord::Schema.define(:version => 20120712080407) do |
171 | 171 | t.boolean "blocked", :default => false, :null => false |
172 | 172 | t.integer "failed_attempts", :default => 0 |
173 | 173 | t.datetime "locked_at" |
174 | + t.string "extern_uid" | |
175 | + t.string "provider" | |
174 | 176 | end |
175 | 177 | |
176 | 178 | add_index "users", ["email"], :name => "index_users_on_email", :unique => true |
179 | + add_index "users", ["extern_uid", "provider"], :name => "index_users_on_extern_uid_and_provider", :unique => true | |
177 | 180 | add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true |
178 | 181 | |
179 | 182 | create_table "users_projects", :force => true do |t| | ... | ... |