gitlab-org--gitlab-foss/lib/gitlab/o_auth/user.rb

161 lines
4.4 KiB
Ruby
Raw Normal View History

# OAuth extension for User model
#
# * Find GitLab user based on omniauth uid and provider
# * Create new user from omniauth data
#
module Gitlab
module OAuth
2015-05-12 06:50:06 -04:00
class SignupDisabledError < StandardError; end
class User
2014-10-10 06:03:32 -04:00
attr_accessor :auth_hash, :gl_user
2014-10-10 06:03:32 -04:00
def initialize(auth_hash)
self.auth_hash = auth_hash
end
2014-10-10 06:03:32 -04:00
def persisted?
gl_user.try(:persisted?)
2014-10-10 06:03:32 -04:00
end
2014-10-10 06:03:32 -04:00
def new?
!persisted?
2014-10-10 06:03:32 -04:00
end
2014-10-10 06:03:32 -04:00
def valid?
gl_user.try(:valid?)
end
2014-10-10 06:03:32 -04:00
def save
unauthorized_to_create unless gl_user
if needs_blocking?
gl_user.save!
gl_user.block
else
gl_user.save!
end
log.info "(OAuth) saving user #{auth_hash.email} from login with extern_uid => #{auth_hash.uid}"
2014-10-10 06:03:32 -04:00
gl_user
rescue ActiveRecord::RecordInvalid => e
log.info "(OAuth) Error saving user: #{gl_user.errors.full_messages}"
return self, e.record.errors
end
def gl_user
@user ||= find_by_uid_and_provider
if auto_link_ldap_user?
@user ||= find_or_create_ldap_user
end
if signup_enabled?
@user ||= build_new_user
end
@user
end
2014-10-10 06:03:32 -04:00
protected
def find_or_create_ldap_user
return unless ldap_person
#If a corresponding person exists with same uid in a LDAP server,
#set up a Gitlab user with dual LDAP and Omniauth identities.
if user = Gitlab::LDAP::User.find_by_uid_and_provider(ldap_person.dn.downcase, ldap_person.provider)
#case when a LDAP user already exists in Gitlab. Add the Omniauth identity to existing account.
user.identities.build(extern_uid: auth_hash.uid, provider: auth_hash.provider)
else
#no account in Gitlab yet: create it and add the LDAP identity
user = build_new_user
user.identities.new(provider: ldap_person.provider, extern_uid: ldap_person.dn)
end
user
end
def auto_link_ldap_user?
Gitlab.config.omniauth.auto_link_ldap_user
end
def creating_linked_ldap_user?
auto_link_ldap_user? && ldap_person
end
def ldap_person
return @ldap_person if defined?(@ldap_person)
#looks for a corresponding person with same uid in any of the configured LDAP providers
Gitlab::LDAP::Config.providers.each do |provider|
adapter = Gitlab::LDAP::Adapter.new(provider)
@ldap_person = Gitlab::LDAP::Person.find_by_uid(auth_hash.uid, adapter)
break if @ldap_person #exit on first person found
end
@ldap_person #may be nil if we could not find a match
end
def ldap_config
ldap_person && Gitlab::LDAP::Config.new(ldap_person.provider)
end
def needs_blocking?
return false unless new?
if creating_linked_ldap_user?
ldap_config.block_auto_created_users
else
block_after_signup?
end
end
def signup_enabled?
Gitlab.config.omniauth.allow_single_sign_on
end
def block_after_signup?
Gitlab.config.omniauth.block_auto_created_users
end
2014-09-04 06:55:10 -04:00
def auth_hash=(auth_hash)
@auth_hash = AuthHash.new(auth_hash)
end
2014-10-10 06:03:32 -04:00
def find_by_uid_and_provider
identity = Identity.find_by(provider: auth_hash.provider, extern_uid: auth_hash.uid)
identity && identity.user
2014-10-10 06:03:32 -04:00
end
2014-10-10 06:03:32 -04:00
def build_new_user
user = ::User.new(user_attributes)
user.skip_confirmation!
user.identities.new(extern_uid: auth_hash.uid, provider: auth_hash.provider)
user
end
def user_attributes
# Give preference to LDAP for sensitive information when creating a linked account
username, email = if creating_linked_ldap_user?
[ ldap_person.username, ldap_person.email.first ]
else
[ auth_hash.username, auth_hash.email ]
end
return {
name: auth_hash.name,
username: ::Namespace.clean_path(username),
email: email,
password: auth_hash.password,
password_confirmation: auth_hash.password,
password_automatically_set: true
}
end
def log
Gitlab::AppLogger
end
def unauthorized_to_create
2015-05-12 06:50:06 -04:00
raise SignupDisabledError
end
end
end
end