2014-10-13 11:24:05 -04:00
|
|
|
# LDAP authorization model
|
|
|
|
#
|
|
|
|
# * Check if we are allowed access (not blocked)
|
|
|
|
#
|
2014-03-10 08:48:08 -04:00
|
|
|
module Gitlab
|
|
|
|
module LDAP
|
|
|
|
class Access
|
2015-12-31 14:22:51 -05:00
|
|
|
attr_reader :provider, :user
|
2014-03-14 03:55:50 -04:00
|
|
|
|
2014-10-13 11:24:05 -04:00
|
|
|
def self.open(user, &block)
|
2014-11-27 06:34:39 -05:00
|
|
|
Gitlab::LDAP::Adapter.open(user.ldap_identity.provider) do |adapter|
|
2014-10-13 11:24:05 -04:00
|
|
|
block.call(self.new(user, adapter))
|
2014-03-14 03:55:50 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-07-30 03:50:50 -04:00
|
|
|
def self.allowed?(user)
|
2014-10-13 11:24:05 -04:00
|
|
|
self.open(user) do |access|
|
|
|
|
if access.allowed?
|
2014-07-30 03:50:50 -04:00
|
|
|
user.last_credential_check_at = Time.now
|
|
|
|
user.save
|
|
|
|
true
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-05 22:03:01 -04:00
|
|
|
def initialize(user, adapter = nil)
|
2014-03-14 03:55:50 -04:00
|
|
|
@adapter = adapter
|
2014-10-13 11:24:05 -04:00
|
|
|
@user = user
|
2014-11-27 06:34:39 -05:00
|
|
|
@provider = user.ldap_identity.provider
|
2014-03-14 03:55:50 -04:00
|
|
|
end
|
|
|
|
|
2014-10-13 11:24:05 -04:00
|
|
|
def allowed?
|
2015-12-31 14:22:51 -05:00
|
|
|
if ldap_user
|
2016-04-05 15:33:37 -04:00
|
|
|
unless ldap_config.active_directory
|
2016-12-20 13:54:37 -05:00
|
|
|
unblock_user(user, 'is available again') if user.ldap_blocked?
|
2016-04-05 15:33:37 -04:00
|
|
|
return true
|
|
|
|
end
|
2015-03-12 14:53:21 -04:00
|
|
|
|
|
|
|
# Block user in GitLab if he/she was blocked in AD
|
|
|
|
if Gitlab::LDAP::Person.disabled_via_active_directory?(user.ldap_identity.extern_uid, adapter)
|
2016-12-12 09:13:23 -05:00
|
|
|
block_user(user, 'is disabled in Active Directory')
|
2015-03-12 14:53:21 -04:00
|
|
|
false
|
|
|
|
else
|
2016-12-12 09:13:23 -05:00
|
|
|
unblock_user(user, 'is not disabled anymore') if user.ldap_blocked?
|
2015-03-12 14:53:21 -04:00
|
|
|
true
|
|
|
|
end
|
2014-05-14 12:32:40 -04:00
|
|
|
else
|
2015-12-08 10:47:42 -05:00
|
|
|
# Block the user if they no longer exist in LDAP/AD
|
2016-12-12 09:13:23 -05:00
|
|
|
block_user(user, 'does not exist anymore')
|
2014-05-14 12:32:40 -04:00
|
|
|
false
|
|
|
|
end
|
2014-03-10 08:48:08 -04:00
|
|
|
end
|
2014-10-13 11:24:05 -04:00
|
|
|
|
|
|
|
def adapter
|
|
|
|
@adapter ||= Gitlab::LDAP::Adapter.new(provider)
|
|
|
|
end
|
2014-10-14 03:40:35 -04:00
|
|
|
|
|
|
|
def ldap_config
|
|
|
|
Gitlab::LDAP::Config.new(provider)
|
|
|
|
end
|
2015-12-31 14:22:51 -05:00
|
|
|
|
|
|
|
def ldap_user
|
|
|
|
@ldap_user ||= Gitlab::LDAP::Person.find_by_dn(user.ldap_identity.extern_uid, adapter)
|
|
|
|
end
|
2016-12-12 09:13:23 -05:00
|
|
|
|
|
|
|
def block_user(user, reason)
|
|
|
|
user.ldap_block
|
|
|
|
|
|
|
|
Gitlab::AppLogger.info(
|
2017-01-10 08:31:21 -05:00
|
|
|
"LDAP account \"#{user.ldap_identity.extern_uid}\" #{reason}, " \
|
2016-12-12 09:13:23 -05:00
|
|
|
"blocking Gitlab user \"#{user.name}\" (#{user.email})"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def unblock_user(user, reason)
|
|
|
|
user.activate
|
|
|
|
|
|
|
|
Gitlab::AppLogger.info(
|
2017-01-10 08:31:21 -05:00
|
|
|
"LDAP account \"#{user.ldap_identity.extern_uid}\" #{reason}, " \
|
2016-12-12 09:13:23 -05:00
|
|
|
"unblocking Gitlab user \"#{user.name}\" (#{user.email})"
|
|
|
|
)
|
|
|
|
end
|
2014-03-10 08:48:08 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|