diff options
author | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2013-09-22 23:58:24 -0700 |
---|---|---|
committer | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2013-09-22 23:58:24 -0700 |
commit | 0630be3828998af1261b87ae85b42c0ef9a439ed (patch) | |
tree | 312a70ff94c1abb585b798b9f41698026e388a77 | |
parent | 089f0000cae2d49aa7f031e628a6d9e66db69fec (diff) | |
parent | 8a8123a3d4d3a5f991ae599e454b99fd548d47f2 (diff) | |
download | gitlab-ce-0630be3828998af1261b87ae85b42c0ef9a439ed.tar.gz |
Merge pull request #5063 from karlhungus/feature-allow-ldap-update-with-username
Allows username only updates to ldap properties
-rw-r--r-- | lib/gitlab/ldap/user.rb | 15 | ||||
-rw-r--r-- | spec/lib/gitlab/ldap/ldap_user_auth_spec.rb | 57 | ||||
-rw-r--r-- | spec/models/user_spec.rb | 2 |
3 files changed, 72 insertions, 2 deletions
diff --git a/lib/gitlab/ldap/user.rb b/lib/gitlab/ldap/user.rb index c8f3a69376a..260bacfeeb0 100644 --- a/lib/gitlab/ldap/user.rb +++ b/lib/gitlab/ldap/user.rb @@ -26,7 +26,7 @@ module Gitlab # * When user already has account and need to link his LDAP account. # * LDAP uid changed for user with same email and we need to update his uid # - user = model.find_by_email(email) + user = find_user(email) if user user.update_attributes(extern_uid: uid, provider: provider) @@ -43,6 +43,19 @@ module Gitlab user end + def find_user(email) + user = model.find_by_email(email) + + # If no user found and allow_username_or_email_login is true + # we look for user by extracting part of his email + if !user && email && ldap_conf['allow_username_or_email_login'] + uname = email.partition('@').first + user = model.find_by_username(uname) + end + + user + end + def authenticate(login, password) # Check user against LDAP backend if user is not authenticated # Only check with valid login and password to prevent anonymous bind results diff --git a/spec/lib/gitlab/ldap/ldap_user_auth_spec.rb b/spec/lib/gitlab/ldap/ldap_user_auth_spec.rb new file mode 100644 index 00000000000..b1c583c0476 --- /dev/null +++ b/spec/lib/gitlab/ldap/ldap_user_auth_spec.rb @@ -0,0 +1,57 @@ +require 'spec_helper' + +describe Gitlab::LDAP do + let(:gl_auth) { Gitlab::LDAP::User } + + before do + Gitlab.config.stub(omniauth: {}) + + @info = mock( + uid: '12djsak321', + name: 'John', + email: 'john@mail.com' + ) + end + + describe :find_for_ldap_auth do + before do + @auth = mock( + uid: '12djsak321', + info: @info, + provider: 'ldap' + ) + end + + it "should update credentials by email if missing uid" do + user = double('User') + User.stub find_by_extern_uid_and_provider: nil + User.stub find_by_email: user + user.should_receive :update_attributes + gl_auth.find_or_create(@auth) + end + + it "should update credentials by username if missing uid and Gitlab.config.ldap.allow_username_or_email_login is true" do + user = double('User') + value = Gitlab.config.ldap.allow_username_or_email_login + Gitlab.config.ldap['allow_username_or_email_login'] = true + User.stub find_by_extern_uid_and_provider: nil + User.stub find_by_email: nil + User.stub find_by_username: user + user.should_receive :update_attributes + gl_auth.find_or_create(@auth) + Gitlab.config.ldap['allow_username_or_email_login'] = value + end + + it "should not update credentials by username if missing uid and Gitlab.config.ldap.allow_username_or_email_login is false" do + user = double('User') + value = Gitlab.config.ldap.allow_username_or_email_login + Gitlab.config.ldap['allow_username_or_email_login'] = false + User.stub find_by_extern_uid_and_provider: nil + User.stub find_by_email: nil + User.stub find_by_username: user + user.should_not_receive :update_attributes + gl_auth.find_or_create(@auth) + Gitlab.config.ldap['allow_username_or_email_login'] = value + end + end +end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 2b42226ecaf..c879900f8fd 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -233,7 +233,7 @@ describe User do it "should apply defaults to user" do Gitlab.config.gitlab.default_projects_limit.should_not == 123 Gitlab.config.gitlab.default_can_create_group.should_not be_true - Gitlab.config.gitlab.default_theme.should_not == Gitlab::Theme::MARS + Gitlab.config.gitlab.default_theme.should_not == Gitlab::Theme::BASIC user.projects_limit.should == 123 user.can_create_group.should be_true user.theme_id.should == Gitlab::Theme::BASIC |