2018-10-11 20:12:21 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-02-23 12:10:39 +00:00
|
|
|
# LDAP extension for User model
|
|
|
|
#
|
|
|
|
# * Find or create user from omniauth.auth data
|
|
|
|
# * Links LDAP account with existing user
|
|
|
|
# * Auth LDAP user with login and password
|
|
|
|
#
|
|
|
|
module Gitlab
|
|
|
|
module Auth
|
2020-03-12 15:09:39 +00:00
|
|
|
module Ldap
|
2018-02-23 12:10:39 +00:00
|
|
|
class User < Gitlab::Auth::OAuth::User
|
2018-04-18 14:03:27 +00:00
|
|
|
extend ::Gitlab::Utils::Override
|
2018-02-23 12:10:39 +00:00
|
|
|
|
|
|
|
# instance methods
|
|
|
|
def find_user
|
|
|
|
find_by_uid_and_provider || find_by_email || build_new_user
|
|
|
|
end
|
|
|
|
|
2018-04-23 15:20:33 +00:00
|
|
|
override :should_save?
|
|
|
|
def should_save?
|
2018-02-23 12:10:39 +00:00
|
|
|
gl_user.changed? || gl_user.identities.any?(&:changed?)
|
|
|
|
end
|
|
|
|
|
|
|
|
def block_after_signup?
|
|
|
|
ldap_config.block_auto_created_users
|
|
|
|
end
|
|
|
|
|
|
|
|
def allowed?
|
2020-03-12 15:09:39 +00:00
|
|
|
Gitlab::Auth::Ldap::Access.allowed?(gl_user)
|
2018-02-23 12:10:39 +00:00
|
|
|
end
|
|
|
|
|
2018-04-18 14:03:27 +00:00
|
|
|
def valid_sign_in?
|
2022-07-14 03:08:51 +00:00
|
|
|
# The order is important here: we need to ensure the
|
|
|
|
# associated GitLab user entry is valid and persisted in the
|
|
|
|
# database. Otherwise, the LDAP access check will fail since
|
|
|
|
# the user doesn't have an associated LDAP identity.
|
|
|
|
super && allowed?
|
2018-04-18 14:03:27 +00:00
|
|
|
end
|
|
|
|
|
2018-02-23 12:10:39 +00:00
|
|
|
def ldap_config
|
2020-03-12 15:09:39 +00:00
|
|
|
Gitlab::Auth::Ldap::Config.new(auth_hash.provider)
|
2018-02-23 12:10:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def auth_hash=(auth_hash)
|
2020-03-12 15:09:39 +00:00
|
|
|
@auth_hash = Gitlab::Auth::Ldap::AuthHash.new(auth_hash)
|
2018-02-23 12:10:39 +00:00
|
|
|
end
|
2022-03-08 15:15:55 +00:00
|
|
|
|
|
|
|
def protocol_name
|
|
|
|
'LDAP'
|
|
|
|
end
|
2018-02-23 12:10:39 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-04-28 12:09:44 +00:00
|
|
|
|
2021-05-11 21:10:21 +00:00
|
|
|
Gitlab::Auth::Ldap::User.prepend_mod_with('Gitlab::Auth::Ldap::User')
|