summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDouwe Maan <douwe@gitlab.com>2015-09-08 10:58:48 +0100
committerDouwe Maan <douwe@gitlab.com>2015-09-08 16:18:14 +0100
commitc915e2c8237ddcae57ec48e700badd9d5bfd8c8c (patch)
tree045b66c46a9af2022734ccf390ccd979f2ddb07a /lib
parente0da2c352325c1cb2ede88a73434ed7afc037481 (diff)
downloadgitlab-ce-c915e2c8237ddcae57ec48e700badd9d5bfd8c8c.tar.gz
Allow configuration of LDAP attributes GitLab will use for the new user account.
Diffstat (limited to 'lib')
-rw-r--r--lib/gitlab/ldap/auth_hash.rb40
-rw-r--r--lib/gitlab/ldap/user.rb4
-rw-r--r--lib/gitlab/o_auth/auth_hash.rb24
3 files changed, 57 insertions, 11 deletions
diff --git a/lib/gitlab/ldap/auth_hash.rb b/lib/gitlab/ldap/auth_hash.rb
new file mode 100644
index 00000000000..caca7bb3b57
--- /dev/null
+++ b/lib/gitlab/ldap/auth_hash.rb
@@ -0,0 +1,40 @@
+# Class to parse and transform the info provided by omniauth
+#
+module Gitlab
+ module LDAP
+ class AuthHash < Gitlab::OAuth::AuthHash
+ attr_accessor :config
+
+ def initialize(auth_hash, config)
+ super(auth_hash)
+ @config = config
+ end
+
+ private
+
+ def get_info(key)
+ raw_key = config.attributes[key]
+ return super unless raw_key
+
+ value =
+ case raw_key
+ when String
+ get_raw(raw_key)
+ when Array
+ raw_key.inject(nil) { |value, key| value || get_raw(key).presence }
+ else
+ nil
+ end
+
+ return super unless value
+
+ Gitlab::Utils.force_utf8(value)
+ value
+ end
+
+ def get_raw(key)
+ auth_hash.extra[:raw_info][key]
+ end
+ end
+ end
+end
diff --git a/lib/gitlab/ldap/user.rb b/lib/gitlab/ldap/user.rb
index 04a22237478..e568b0e3b31 100644
--- a/lib/gitlab/ldap/user.rb
+++ b/lib/gitlab/ldap/user.rb
@@ -71,6 +71,10 @@ module Gitlab
def ldap_config
Gitlab::LDAP::Config.new(auth_hash.provider)
end
+
+ def auth_hash=(auth_hash)
+ @auth_hash = Gitlab::LDAP::AuthHash.new(auth_hash, ldap_config)
+ end
end
end
end
diff --git a/lib/gitlab/o_auth/auth_hash.rb b/lib/gitlab/o_auth/auth_hash.rb
index 9b8e783d16c..76fbe698c74 100644
--- a/lib/gitlab/o_auth/auth_hash.rb
+++ b/lib/gitlab/o_auth/auth_hash.rb
@@ -16,16 +16,6 @@ module Gitlab
@provider ||= Gitlab::Utils.force_utf8(auth_hash.provider.to_s)
end
- def info
- auth_hash.info
- end
-
- def get_info(key)
- value = info.try(key)
- Gitlab::Utils.force_utf8(value) if value
- value
- end
-
def name
@name ||= get_info(:name) || "#{get_info(:first_name)} #{get_info(:last_name)}"
end
@@ -44,9 +34,21 @@ module Gitlab
private
+ def info
+ auth_hash.info
+ end
+
+ def get_info(key)
+ key = :nickname if key == :username
+
+ value = info[key]
+ Gitlab::Utils.force_utf8(value) if value
+ value
+ end
+
def username_and_email
@username_and_email ||= begin
- username = get_info(:nickname) || get_info(:username)
+ username = get_info(:username)
email = get_info(:email)
username ||= generate_username(email) if email