Commit 89ac11df authored by Jan-Willem van der Meer's avatar Jan-Willem van der Meer

Merge branch 'feature-multi-ldap-servers' into feature-multiple-ldap-push-authentication

parents 19d811e6 f06a60e2
...@@ -4,14 +4,14 @@ ...@@ -4,14 +4,14 @@
.login-body .login-body
- if ldap_enabled? && gitlab_config.signin_enabled - if ldap_enabled? && gitlab_config.signin_enabled
%ul.nav.nav-tabs %ul.nav.nav-tabs
- @ldap_servers.each do |server| - @ldap_servers.each_with_index do |server, i|
%li{class: (:active if server['primary'])} %li{class: (:active if i==0)}
= link_to server['label'], "#tab-#{server.provider_name}", 'data-toggle' => 'tab' = link_to server['label'], "#tab-#{server.provider_name}", 'data-toggle' => 'tab'
%li %li
= link_to 'Standard', '#tab-signin', 'data-toggle' => 'tab' = link_to 'Standard', '#tab-signin', 'data-toggle' => 'tab'
.tab-content .tab-content
- @ldap_servers.each do |server| - @ldap_servers.each_with_index do |server,i|
%div.tab-pane{id: "tab-#{server.provider_name}", class: (:active if server['primary'])} %div.tab-pane{id: "tab-#{server.provider_name}", class: (:active if i==0)}
= render 'devise/sessions/new_ldap', provider: server.provider_name = render 'devise/sessions/new_ldap', provider: server.provider_name
%div#tab-signin.tab-pane %div#tab-signin.tab-pane
= render 'devise/sessions/new_base' = render 'devise/sessions/new_base'
......
...@@ -136,6 +136,27 @@ production: &base ...@@ -136,6 +136,27 @@ production: &base
enabled: false enabled: false
servers: servers:
- -
## provider_id
#
# This identifier is used by GitLab to keep track of which LDAP server each
# GitLab user belongs to. Each LDAP server known to GitLab should have a unique
# provider_id. This identifier cannot be changed once users from the LDAP server
# have started logging in to GitLab.
#
# Format: one word, using a-z (lower case) and 0-9
# Example: 'paris' or 'uswest2'
provider_id: main
## label
#
# A human-friendly name for your LDAP server. It is OK to change the label later,
# for instance if you find out it is too large to fit on the web page.
#
# Example: 'Paris' or 'Acme, Ltd.'
label: 'LDAP'
host: '_your_ldap_server' host: '_your_ldap_server'
port: 636 port: 636
uid: 'sAMAccountName' uid: 'sAMAccountName'
...@@ -143,13 +164,6 @@ production: &base ...@@ -143,13 +164,6 @@ production: &base
bind_dn: '_the_full_dn_of_the_user_you_will_bind_with' bind_dn: '_the_full_dn_of_the_user_you_will_bind_with'
password: '_the_password_of_the_bind_user' password: '_the_password_of_the_bind_user'
# When authenticating against an ldap server, this will provide a unique identifier
# Use one uniq word, no non-word charcters are allowed
provider_id: main
# The UI use this to make a distinction between your LDAP servers
label: 'LDAP'
# This setting controls the amount of time between LDAP permission checks for each user. # This setting controls the amount of time between LDAP permission checks for each user.
# After this time has expired for a given user, their next interaction with GitLab (a click in the web UI, a git pull etc.) will be slower because the LDAP permission check is being performed. # After this time has expired for a given user, their next interaction with GitLab (a click in the web UI, a git pull etc.) will be slower because the LDAP permission check is being performed.
# How much slower depends on your LDAP setup, but it is not uncommon for this check to add seconds of waiting time. # How much slower depends on your LDAP setup, but it is not uncommon for this check to add seconds of waiting time.
......
...@@ -55,22 +55,20 @@ end ...@@ -55,22 +55,20 @@ end
# Default settings # Default settings
Settings['ldap'] ||= Settingslogic.new({}) Settings['ldap'] ||= Settingslogic.new({})
Settings.ldap['enabled'] = false if Settings.ldap['enabled'].nil? Settings.ldap['enabled'] = false if Settings.ldap['enabled'].nil?
Settings.ldap['allow_username_or_email_login'] = false if Settings.ldap['allow_username_or_email_login'].nil?
Settings.ldap['sync_time'] = 3600 if Settings.ldap['sync_time'].nil? Settings.ldap['sync_time'] = 3600 if Settings.ldap['sync_time'].nil?
Settings.ldap['active_directory'] = true if Settings.ldap['active_directory'].nil?
# backwards compatibility, we only have one host # backwards compatibility, we only have one host
if Settings.ldap['enabled'] || Rails.env.test? if Settings.ldap['enabled'] || Rails.env.test?
if Settings.ldap['host'].present? if Settings.ldap['host'].present?
excluded_per_server_settings = %w(sync_time allow_username_or_email_login) server = Settings.ldap.except('sync_time')
server = Settings.ldap.except(excluded_per_server_settings)
server['primary'] = true
server['label'] = 'LDAP' server['label'] = 'LDAP'
server['provider_id'] = '' #providername will be ldap server['provider_id'] = 'main'
Settings.ldap['servers'] = [server] Settings.ldap['servers'] = [server]
end end
Settings.ldap['servers'].each do |server| Settings.ldap['servers'].each do |server|
server['allow_username_or_email_login'] = false if server['allow_username_or_email_login'].nil?
server['active_directory'] = true if server['active_directory'].nil?
server['provider_name'] = "ldap#{server['provider_id']}".downcase server['provider_name'] = "ldap#{server['provider_id']}".downcase
server['provider_class'] = OmniAuth::Utils.camelize(server['provider_name']) server['provider_class'] = OmniAuth::Utils.camelize(server['provider_name'])
end end
......
...@@ -205,13 +205,13 @@ Devise.setup do |config| ...@@ -205,13 +205,13 @@ Devise.setup do |config|
# end # end
if Gitlab.config.ldap.enabled if Gitlab.config.ldap.enabled
if Gitlab.config.ldap.allow_username_or_email_login
email_stripping_proc = ->(name) {name.gsub(/@.*$/,'')}
else
email_stripping_proc = ->(name) {name}
end
Gitlab.config.ldap.servers.each do |server| Gitlab.config.ldap.servers.each do |server|
if server['allow_username_or_email_login']
email_stripping_proc = ->(name) {name.gsub(/@.*$/,'')}
else
email_stripping_proc = ->(name) {name}
end
config.omniauth server.provider_name, config.omniauth server.provider_name,
host: server['host'], host: server['host'],
base: server['base'], base: server['base'],
......
...@@ -36,9 +36,8 @@ module Gitlab ...@@ -36,9 +36,8 @@ module Gitlab
def allowed? def allowed?
if Gitlab::LDAP::Person.find_by_dn(user.extern_uid, adapter) if Gitlab::LDAP::Person.find_by_dn(user.extern_uid, adapter)
if Gitlab.config.ldap.active_directory return true unless ldap_config.active_directory
!Gitlab::LDAP::Person.disabled_via_active_directory?(user.extern_uid, adapter) !Gitlab::LDAP::Person.disabled_via_active_directory?(user.extern_uid, adapter)
end
else else
false false
end end
......
...@@ -18,6 +18,7 @@ module Gitlab ...@@ -18,6 +18,7 @@ module Gitlab
def initialize(provider) def initialize(provider)
@provider = provider @provider = provider
invalid_provider unless valid_provider?
@options = config_for(provider) @options = config_for(provider)
end end
...@@ -64,6 +65,10 @@ module Gitlab ...@@ -64,6 +65,10 @@ module Gitlab
options['admin_group'] options['admin_group']
end end
def active_directory
options['active_directory']
end
protected protected
def base_config def base_config
Gitlab.config.ldap Gitlab.config.ldap
...@@ -84,6 +89,14 @@ module Gitlab ...@@ -84,6 +89,14 @@ module Gitlab
end end
end end
def valid_provider?
self.class.providers.include?(provider)
end
def invalid_provider
raise "Unknown provider (#{provider}). Available providers: #{self.class.providers}"
end
def auth_options def auth_options
{ {
auth: { auth: {
......
...@@ -60,10 +60,6 @@ module Gitlab ...@@ -60,10 +60,6 @@ module Gitlab
@entry @entry
end end
# def adapter
# @adapter ||= Gitlab::LDAP::Adapter.new
# end
def config def config
@config ||= Gitlab::LDAP::Config.new(provider) @config ||= Gitlab::LDAP::Config.new(provider)
end end
......
...@@ -20,7 +20,7 @@ module Gitlab ...@@ -20,7 +20,7 @@ module Gitlab
def initialize(auth_hash) def initialize(auth_hash)
super super
update_attributes update_user_attributes
end end
# instance methods # instance methods
...@@ -39,7 +39,7 @@ module Gitlab ...@@ -39,7 +39,7 @@ module Gitlab
model.find_by(email: auth_hash.email) model.find_by(email: auth_hash.email)
end end
def update_attributes def update_user_attributes
gl_user.attributes = { gl_user.attributes = {
extern_uid: auth_hash.uid, extern_uid: auth_hash.uid,
provider: auth_hash.provider, provider: auth_hash.provider,
......
...@@ -31,7 +31,7 @@ module Gitlab ...@@ -31,7 +31,7 @@ module Gitlab
gl_user gl_user
rescue ActiveRecord::RecordInvalid => e rescue ActiveRecord::RecordInvalid => e
log.info "(OAuth) Email #{e.record.errors[:email]}. Username #{e.record.errors[:username]}" log.info "(OAuth) Error saving user: #{gl_user.errors.full_messages}"
return self, e.record.errors return self, e.record.errors
end end
......
...@@ -28,19 +28,13 @@ describe Gitlab::LDAP::Access do ...@@ -28,19 +28,13 @@ describe Gitlab::LDAP::Access do
it { should be_true } it { should be_true }
end end
context 'and has no disabled flag in active diretory' do context 'withoud ActiveDirectory enabled' do
before { before do
Gitlab::LDAP::Person.stub(disabled_via_active_directory?: false) Gitlab::LDAP::Config.stub(enabled?: true)
Gitlab.config.ldap['enabled'] = true Gitlab::LDAP::Config.any_instance.stub(active_directory: false)
Gitlab.config.ldap['active_directory'] = false end
}
after {
Gitlab.config.ldap['enabled'] = false
Gitlab.config.ldap['active_directory'] = true
}
it { should be_false } it { should be_true }
end end
end end
end end
......
...@@ -12,5 +12,9 @@ describe Gitlab::LDAP::Config do ...@@ -12,5 +12,9 @@ describe Gitlab::LDAP::Config do
it "works" do it "works" do
expect(config).to be_a described_class expect(config).to be_a described_class
end end
it "raises an error if a unknow provider is used" do
expect{ Gitlab::LDAP::Config.new 'unknown' }.to raise_error
end
end end
end end
\ No newline at end of file
...@@ -33,7 +33,7 @@ describe Gitlab::OAuth::AuthHash do ...@@ -33,7 +33,7 @@ describe Gitlab::OAuth::AuthHash do
context "email not provided" do context "email not provided" do
before { info_hash.delete(:email) } before { info_hash.delete(:email) }
it "generates a temp email" do it "generates a temp email" do
expect( auth_hash.email).to_not be_empty expect( auth_hash.email).to start_with('temp-email-for-oauth')
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment