gitlab-org--gitlab-foss/lib/gitlab/ldap/access.rb

72 lines
1.7 KiB
Ruby
Raw Normal View History

# LDAP authorization model
#
# * Check if we are allowed access (not blocked)
#
module Gitlab
module LDAP
class Access
2015-12-31 14:22:51 -05:00
attr_reader :provider, :user
def self.open(user, &block)
Gitlab::LDAP::Adapter.open(user.ldap_identity.provider) do |adapter|
block.call(self.new(user, adapter))
end
end
def self.allowed?(user)
self.open(user) do |access|
if access.allowed?
user.last_credential_check_at = Time.now
user.save
true
else
false
end
end
end
def initialize(user, adapter=nil)
@adapter = adapter
@user = user
@provider = user.ldap_identity.provider
end
def allowed?
2015-12-31 14:22:51 -05:00
if ldap_user
unless ldap_config.active_directory
user.activate if user.ldap_blocked?
return true
end
# 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)
user.ldap_block
false
else
user.activate if user.ldap_blocked?
true
end
else
# Block the user if they no longer exist in LDAP/AD
user.ldap_block
false
end
rescue
false
end
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
end
end
end