From 75d43f55ede701d10e15f579a28aebcb1dac72f1 Mon Sep 17 00:00:00 2001 From: Brock Trappitt <brock.trappitt@gmail.com> Date: Sat, 20 Apr 2013 13:27:47 +0800 Subject: [PATCH 1/4] Refactored User parameters out a bit when using LDAP --- lib/gitlab/auth.rb | 88 +++++++++++++++++++++++++++++++++------------- 1 file changed, 64 insertions(+), 24 deletions(-) diff --git a/lib/gitlab/auth.rb b/lib/gitlab/auth.rb index 0fee33dbeb0..d754fb61a0d 100644 --- a/lib/gitlab/auth.rb +++ b/lib/gitlab/auth.rb @@ -18,30 +18,9 @@ module Gitlab end def create_from_omniauth(auth, ldap = false) - provider = auth.provider - uid = auth.info.uid || auth.uid - uid = uid.to_s.force_encoding("utf-8") - name = auth.info.name.to_s.force_encoding("utf-8") - email = auth.info.email.to_s.downcase unless auth.info.email.nil? - - ldap_prefix = ldap ? '(LDAP) ' : '' - raise OmniAuth::Error, "#{ldap_prefix}#{provider} does not provide an email"\ - " address" if auth.info.email.blank? - - log.info "#{ldap_prefix}Creating user from #{provider} login"\ - " {uid => #{uid}, name => #{name}, email => #{email}}" - password = Devise.friendly_token[0, 8].downcase - @user = User.new({ - extern_uid: uid, - provider: provider, - name: name, - username: email.match(/^[^@]*/)[0], - email: email, - password: password, - password_confirmation: password, - projects_limit: Gitlab.config.gitlab.default_projects_limit, - }, as: :admin) - @user.save! + creation_helper = UserCreationHelper.new(auth, ldap) + log.info creation_helper.creation_message + @user = User.create!(creation_helper.parameters, as: :admin) if Gitlab.config.omniauth['block_auto_created_users'] && !ldap @user.block @@ -70,5 +49,66 @@ module Gitlab def log Gitlab::AppLogger end + + class UserCreationHelper + def initialize(auth, ldap = false) + @auth = auth + @ldap = ldap + end + + def parameters + { + extern_uid: uid, + provider: provider, + name: name, + username: username, + email: email, + password: password, + password_confirmation: password, + projects_limit: Gitlab.config.gitlab.default_projects_limit + } + end + + def uid + (@auth.info.uid || @auth.uid).to_s.force_encoding("utf-8") + end + + def provider + @auth.info.provider + end + + def name + @auth.info.name.to_s.force_encoding("utf-8") + end + + def username + email.match(/^[^@]*/)[0] + end + + def email + auth.info.email.nil? ? auth.info.email.to_s.downcase : email_error + end + + def password + @password ||= Devise.friendly_token[0, 8].downcase + end + + def creation_message + "#{ldap_prefix}Creating user from #{provider} login"\ + " {uid => #{uid}, name => #{name}, email => #{email}}" + end + + private + + def ldap_prefix + ldap ? '(LDAP) ' : '' + end + + def email_error + raise OmniAuth::Error, "#{ldap_prefix}#{parameters.provider} does not"\ + " provide an email address" unless parameters.email + end + end + end end -- GitLab From df3412db3c1d3d721cf53c2379055a7df47013c8 Mon Sep 17 00:00:00 2001 From: Brock Trappitt <brock.trappitt@gmail.com> Date: Sat, 20 Apr 2013 13:33:58 +0800 Subject: [PATCH 2/4] Got email logic backwards --- lib/gitlab/auth.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/gitlab/auth.rb b/lib/gitlab/auth.rb index d754fb61a0d..a9b85936299 100644 --- a/lib/gitlab/auth.rb +++ b/lib/gitlab/auth.rb @@ -86,7 +86,7 @@ module Gitlab end def email - auth.info.email.nil? ? auth.info.email.to_s.downcase : email_error + auth.info.email.nil? ? email_error : auth.info.email.to_s.downcase end def password -- GitLab From c6beec1a2df4fd65fd1aac2c113d71bcdb3b80e0 Mon Sep 17 00:00:00 2001 From: Brock Trappitt <brock.trappitt@gmail.com> Date: Sat, 20 Apr 2013 14:04:24 +0800 Subject: [PATCH 3/4] Starting work on the procs --- config/gitlab.yml.example | 5 +++++ lib/gitlab/auth.rb | 14 +++++++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/config/gitlab.yml.example b/config/gitlab.yml.example index 10fe1245115..8de5e196da6 100644 --- a/config/gitlab.yml.example +++ b/config/gitlab.yml.example @@ -110,6 +110,11 @@ production: &base # - { name: 'github', app_id: 'YOUR APP ID', # app_secret: 'YOUR APP SECRET' } + ## User mapping settings + user_mapping: + name_proc: ->(uid, name, email) { name } + username_proc: ->(uid, name, email) { email.match(/^[^@]*/)[0] } + email_proc: ->(uid, name, email) { email } # diff --git a/lib/gitlab/auth.rb b/lib/gitlab/auth.rb index a9b85936299..83beea5964e 100644 --- a/lib/gitlab/auth.rb +++ b/lib/gitlab/auth.rb @@ -78,15 +78,15 @@ module Gitlab end def name - @auth.info.name.to_s.force_encoding("utf-8") + Gitlab.config.user_mapping.name_proc.call(uid, raw_name, raw_email) end def username - email.match(/^[^@]*/)[0] + Gitlab.config.user_mapping.username_proc.call(uid, raw_name, raw_email) end def email - auth.info.email.nil? ? email_error : auth.info.email.to_s.downcase + Gitlab.config.user_mapping.email_proc.call(uid, raw_name, raw_email) end def password @@ -100,6 +100,14 @@ module Gitlab private + def raw_name + @auth.info.name.to_s.force_encoding("utf-8") + end + + def raw_email + auth.info.email.nil? ? email_error : auth.info.email.to_s.downcase + end + def ldap_prefix ldap ? '(LDAP) ' : '' end -- GitLab From 7ccba13f7ddbad9a46e1892cfa12a73fb37789cd Mon Sep 17 00:00:00 2001 From: Brock Trappitt <brock.trappitt@gmail.com> Date: Sat, 20 Apr 2013 15:45:33 +0800 Subject: [PATCH 4/4] Added default procs to map attributes --- config/gitlab.yml.example | 6 +++--- lib/gitlab/auth.rb | 11 ++++++++--- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/config/gitlab.yml.example b/config/gitlab.yml.example index 8de5e196da6..684f4be0d10 100644 --- a/config/gitlab.yml.example +++ b/config/gitlab.yml.example @@ -112,9 +112,9 @@ production: &base ## User mapping settings user_mapping: - name_proc: ->(uid, name, email) { name } - username_proc: ->(uid, name, email) { email.match(/^[^@]*/)[0] } - email_proc: ->(uid, name, email) { email } + name: ->(uid, name, email) { name } + username: ->(uid, name, email) { email.match(/^[^@]*/)[0] } + email: ->(uid, name, email) { email } # diff --git a/lib/gitlab/auth.rb b/lib/gitlab/auth.rb index 83beea5964e..99288497218 100644 --- a/lib/gitlab/auth.rb +++ b/lib/gitlab/auth.rb @@ -54,6 +54,11 @@ module Gitlab def initialize(auth, ldap = false) @auth = auth @ldap = ldap + @procs = { + name_proc: ->(uid, name, email) { name } + username_proc: ->(uid, name, email) { email.match(/^[^@]*/)[0] } + email_proc: ->(uid, name, email) { email } + }.merge(Gitlab.config.user_mapping) end def parameters @@ -78,15 +83,15 @@ module Gitlab end def name - Gitlab.config.user_mapping.name_proc.call(uid, raw_name, raw_email) + @procs[:name].call(uid, raw_name, raw_email) end def username - Gitlab.config.user_mapping.username_proc.call(uid, raw_name, raw_email) + @procs[:username].call(uid, raw_name, raw_email) end def email - Gitlab.config.user_mapping.email_proc.call(uid, raw_name, raw_email) + @procs[:email].call(uid, raw_name, raw_email) end def password -- GitLab