2012-09-12 02:23:16 -04:00
|
|
|
module Gitlab
|
|
|
|
class Auth
|
|
|
|
def find_for_ldap_auth(auth, signed_in_resource = nil)
|
|
|
|
uid = auth.info.uid
|
|
|
|
provider = auth.provider
|
|
|
|
email = auth.info.email.downcase unless auth.info.email.nil?
|
|
|
|
raise OmniAuth::Error, "LDAP accounts must provide an uid and email address" if uid.nil? or email.nil?
|
|
|
|
|
|
|
|
if @user = User.find_by_extern_uid_and_provider(uid, provider)
|
|
|
|
@user
|
|
|
|
elsif @user = User.find_by_email(email)
|
|
|
|
log.info "Updating legacy LDAP user #{email} with extern_uid => #{uid}"
|
|
|
|
@user.update_attributes(:extern_uid => uid, :provider => provider)
|
|
|
|
@user
|
|
|
|
else
|
|
|
|
create_from_omniauth(auth, true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-09-12 18:11:59 -04:00
|
|
|
def create_from_omniauth(auth, ldap = false)
|
2012-09-12 02:23:16 -04:00
|
|
|
provider = auth.provider
|
|
|
|
uid = auth.info.uid || auth.uid
|
2012-09-28 10:00:04 -04:00
|
|
|
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?
|
2012-09-12 02:23:16 -04:00
|
|
|
|
|
|
|
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
|
2012-09-26 14:06:31 -04:00
|
|
|
@user = User.new({
|
2012-09-12 02:23:16 -04:00
|
|
|
extern_uid: uid,
|
|
|
|
provider: provider,
|
|
|
|
name: name,
|
2012-11-23 13:11:09 -05:00
|
|
|
username: email.match(/^[^@]*/)[0],
|
2012-09-12 02:23:16 -04:00
|
|
|
email: email,
|
|
|
|
password: password,
|
|
|
|
password_confirmation: password,
|
2012-12-14 19:16:25 -05:00
|
|
|
projects_limit: Gitlab.config.gitlab.default_projects_limit,
|
2012-09-26 14:06:31 -04:00
|
|
|
}, as: :admin)
|
2012-09-12 18:11:59 -04:00
|
|
|
if Gitlab.config.omniauth['block_auto_created_users'] && !ldap
|
2012-09-12 02:23:16 -04:00
|
|
|
@user.blocked = true
|
|
|
|
end
|
|
|
|
@user.save!
|
|
|
|
@user
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_or_new_for_omniauth(auth)
|
|
|
|
provider, uid = auth.provider, auth.uid
|
2012-10-19 17:38:07 -04:00
|
|
|
email = auth.info.email.downcase unless auth.info.email.nil?
|
2012-09-12 02:23:16 -04:00
|
|
|
|
|
|
|
if @user = User.find_by_provider_and_extern_uid(provider, uid)
|
|
|
|
@user
|
2012-10-19 17:38:07 -04:00
|
|
|
elsif @user = User.find_by_email(email)
|
|
|
|
@user.update_attributes(:extern_uid => uid, :provider => provider)
|
|
|
|
@user
|
2012-09-12 02:23:16 -04:00
|
|
|
else
|
2012-09-12 18:11:59 -04:00
|
|
|
if Gitlab.config.omniauth['allow_single_sign_on']
|
2012-09-12 02:23:16 -04:00
|
|
|
@user = create_from_omniauth(auth)
|
|
|
|
@user
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def log
|
|
|
|
Gitlab::AppLogger
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|