2018-10-11 16:12:21 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-02-23 07:10:39 -05:00
|
|
|
# LDAP authorization model
|
|
|
|
#
|
|
|
|
# * Check if we are allowed access (not blocked)
|
|
|
|
#
|
|
|
|
module Gitlab
|
|
|
|
module Auth
|
2020-03-12 11:09:39 -04:00
|
|
|
module Ldap
|
2018-02-23 07:10:39 -05:00
|
|
|
class Access
|
2018-05-18 10:26:44 -04:00
|
|
|
attr_reader :provider, :user, :ldap_identity
|
2018-02-23 07:10:39 -05:00
|
|
|
|
|
|
|
def self.open(user, &block)
|
2020-03-12 11:09:39 -04:00
|
|
|
Gitlab::Auth::Ldap::Adapter.open(user.ldap_identity.provider) do |adapter|
|
2018-02-23 07:10:39 -05:00
|
|
|
block.call(self.new(user, adapter))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-18 10:26:44 -04:00
|
|
|
def self.allowed?(user, options = {})
|
2018-02-23 07:10:39 -05:00
|
|
|
self.open(user) do |access|
|
2018-05-18 10:26:44 -04:00
|
|
|
# Whether user is allowed, or not, we should update
|
|
|
|
# permissions to keep things clean
|
2018-02-23 07:10:39 -05:00
|
|
|
if access.allowed?
|
2021-08-02 05:10:09 -04:00
|
|
|
unless Gitlab::Database.read_only?
|
2018-08-23 09:46:45 -04:00
|
|
|
access.update_user
|
|
|
|
Users::UpdateService.new(user, user: user, last_credential_check_at: Time.now).execute
|
|
|
|
end
|
2018-02-23 07:10:39 -05:00
|
|
|
|
|
|
|
true
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(user, adapter = nil)
|
|
|
|
@adapter = adapter
|
|
|
|
@user = user
|
2018-05-18 10:26:44 -04:00
|
|
|
@ldap_identity = user.ldap_identity
|
|
|
|
@provider = adapter&.provider || ldap_identity&.provider
|
2018-02-23 07:10:39 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def allowed?
|
|
|
|
if ldap_user
|
|
|
|
unless ldap_config.active_directory
|
|
|
|
unblock_user(user, 'is available again') if user.ldap_blocked?
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2020-02-07 10:09:52 -05:00
|
|
|
# Block user in GitLab if they were blocked in AD
|
2020-03-12 11:09:39 -04:00
|
|
|
if Gitlab::Auth::Ldap::Person.disabled_via_active_directory?(ldap_identity.extern_uid, adapter)
|
2018-02-23 07:10:39 -05:00
|
|
|
block_user(user, 'is disabled in Active Directory')
|
|
|
|
false
|
|
|
|
else
|
|
|
|
unblock_user(user, 'is not disabled anymore') if user.ldap_blocked?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
else
|
|
|
|
# Block the user if they no longer exist in LDAP/AD
|
|
|
|
block_user(user, 'does not exist anymore')
|
|
|
|
false
|
|
|
|
end
|
2020-03-12 11:09:39 -04:00
|
|
|
rescue LdapConnectionError
|
2018-04-04 05:07:28 -04:00
|
|
|
false
|
2018-02-23 07:10:39 -05:00
|
|
|
end
|
|
|
|
|
2018-08-23 09:46:45 -04:00
|
|
|
def update_user
|
|
|
|
# no-op in CE
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-02-23 07:10:39 -05:00
|
|
|
def adapter
|
2020-03-12 11:09:39 -04:00
|
|
|
@adapter ||= Gitlab::Auth::Ldap::Adapter.new(provider)
|
2018-02-23 07:10:39 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def ldap_config
|
2020-03-12 11:09:39 -04:00
|
|
|
Gitlab::Auth::Ldap::Config.new(provider)
|
2018-02-23 07:10:39 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def ldap_user
|
2018-05-18 10:26:44 -04:00
|
|
|
return unless provider
|
|
|
|
|
|
|
|
@ldap_user ||= find_ldap_user
|
2018-02-23 07:10:39 -05:00
|
|
|
end
|
|
|
|
|
2018-08-23 09:46:45 -04:00
|
|
|
def find_ldap_user
|
2020-03-12 11:09:39 -04:00
|
|
|
Gitlab::Auth::Ldap::Person.find_by_dn(ldap_identity.extern_uid, adapter)
|
2018-08-23 09:46:45 -04:00
|
|
|
end
|
|
|
|
|
2018-02-23 07:10:39 -05:00
|
|
|
def block_user(user, reason)
|
|
|
|
user.ldap_block
|
|
|
|
|
2018-05-18 10:26:44 -04:00
|
|
|
if provider
|
|
|
|
Gitlab::AppLogger.info(
|
|
|
|
"LDAP account \"#{ldap_identity.extern_uid}\" #{reason}, " \
|
2018-09-21 08:05:37 -04:00
|
|
|
"blocking GitLab user \"#{user.name}\" (#{user.email})"
|
2018-05-18 10:26:44 -04:00
|
|
|
)
|
|
|
|
else
|
|
|
|
Gitlab::AppLogger.info(
|
|
|
|
"Account is not provided by LDAP, " \
|
2018-09-21 08:05:37 -04:00
|
|
|
"blocking GitLab user \"#{user.name}\" (#{user.email})"
|
2018-05-18 10:26:44 -04:00
|
|
|
)
|
|
|
|
end
|
2018-02-23 07:10:39 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def unblock_user(user, reason)
|
|
|
|
user.activate
|
|
|
|
|
|
|
|
Gitlab::AppLogger.info(
|
2018-05-18 10:26:44 -04:00
|
|
|
"LDAP account \"#{ldap_identity.extern_uid}\" #{reason}, " \
|
2018-09-21 08:05:37 -04:00
|
|
|
"unblocking GitLab user \"#{user.name}\" (#{user.email})"
|
2018-02-23 07:10:39 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-04-28 08:09:44 -04:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
Gitlab::Auth::Ldap::Access.prepend_mod_with('Gitlab::Auth::Ldap::Access')
|