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
|
|
|
|
|
2014-10-13 11:24:05 -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
|
|
|
|
user.activate if user.ldap_blocked?
|
|
|
|
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)
|
2015-12-30 17:56:26 -05:00
|
|
|
user.ldap_block
|
2015-03-12 14:53:21 -04:00
|
|
|
false
|
|
|
|
else
|
2016-01-14 00:31:27 -05:00
|
|
|
user.activate 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
|
2015-12-30 17:56:26 -05:00
|
|
|
user.ldap_block
|
2014-05-14 12:32:40 -04:00
|
|
|
false
|
|
|
|
end
|
2014-03-10 08:48:08 -04:00
|
|
|
rescue
|
|
|
|
false
|
|
|
|
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
|
2014-03-10 08:48:08 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|