2016-02-18 17:01:07 -05:00
|
|
|
# SAML extension for User model
|
|
|
|
#
|
|
|
|
# * Find GitLab user based on SAML uid and provider
|
|
|
|
# * Create new user from SAML data
|
|
|
|
#
|
|
|
|
module Gitlab
|
|
|
|
module Saml
|
|
|
|
class User < Gitlab::OAuth::User
|
|
|
|
|
|
|
|
def save
|
|
|
|
super('SAML')
|
|
|
|
end
|
|
|
|
|
|
|
|
def gl_user
|
|
|
|
@user ||= find_by_uid_and_provider
|
|
|
|
|
|
|
|
if auto_link_ldap_user?
|
|
|
|
@user ||= find_or_create_ldap_user
|
|
|
|
end
|
|
|
|
|
2016-04-04 20:09:12 -04:00
|
|
|
if auto_link_saml_user?
|
2016-02-18 17:01:07 -05:00
|
|
|
@user ||= find_by_email
|
|
|
|
end
|
|
|
|
|
|
|
|
if signup_enabled?
|
|
|
|
@user ||= build_new_user
|
|
|
|
end
|
|
|
|
|
2016-04-05 20:20:18 -04:00
|
|
|
if external_users_enabled?
|
|
|
|
# Check if there is overlap between the user's groups and the external groups
|
2016-04-06 19:12:25 -04:00
|
|
|
# setting then set user as external or internal.
|
2016-04-05 20:20:18 -04:00
|
|
|
if (auth_hash.groups & Gitlab::Saml::Config.external_groups).empty?
|
2016-04-06 19:12:25 -04:00
|
|
|
@user.external = false
|
2016-04-05 20:20:18 -04:00
|
|
|
else
|
2016-04-06 19:12:25 -04:00
|
|
|
@user.external = true
|
2016-04-05 20:20:18 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-18 17:01:07 -05:00
|
|
|
@user
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_by_email
|
|
|
|
if auth_hash.has_email?
|
|
|
|
user = ::User.find_by(email: auth_hash.email.downcase)
|
|
|
|
user.identities.new(extern_uid: auth_hash.uid, provider: auth_hash.provider) if user
|
|
|
|
user
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-04-04 20:09:12 -04:00
|
|
|
def changed?
|
|
|
|
gl_user.changed? || gl_user.identities.any?(&:changed?)
|
|
|
|
end
|
|
|
|
|
2016-02-18 17:01:07 -05:00
|
|
|
protected
|
|
|
|
|
2016-04-04 20:09:12 -04:00
|
|
|
def auto_link_saml_user?
|
2016-02-18 17:01:07 -05:00
|
|
|
Gitlab.config.omniauth.auto_link_saml_user
|
|
|
|
end
|
2016-04-04 20:09:12 -04:00
|
|
|
|
|
|
|
def external_users_enabled?
|
|
|
|
!Gitlab::Saml::Config.external_groups.nil?
|
|
|
|
end
|
|
|
|
|
|
|
|
def auth_hash=(auth_hash)
|
|
|
|
@auth_hash = Gitlab::Saml::AuthHash.new(auth_hash)
|
|
|
|
end
|
2016-02-18 17:01:07 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|