2014-03-10 08:48:08 -04:00
|
|
|
module Gitlab
|
|
|
|
module LDAP
|
|
|
|
class Person
|
2014-05-14 13:08:42 -04:00
|
|
|
# Active Directory-specific LDAP filter that checks if bit 2 of the
|
|
|
|
# userAccountControl attribute is set.
|
|
|
|
# Source: http://ctogonewild.com/2009/09/03/bitmask-searches-in-ldap/
|
2014-05-14 12:54:05 -04:00
|
|
|
AD_USER_DISABLED = Net::LDAP::Filter.ex("userAccountControl:1.2.840.113556.1.4.803", "2")
|
2014-05-14 12:26:58 -04:00
|
|
|
|
2014-10-13 11:24:05 -04:00
|
|
|
attr_accessor :entry, :provider
|
|
|
|
|
|
|
|
def self.find_by_uid(uid, adapter)
|
2015-03-06 07:26:33 -05:00
|
|
|
uid = Net::LDAP::Filter.escape(uid)
|
2014-10-14 04:54:43 -04:00
|
|
|
adapter.user(adapter.config.uid, uid)
|
2014-03-10 08:48:08 -04:00
|
|
|
end
|
|
|
|
|
2014-10-13 11:24:05 -04:00
|
|
|
def self.find_by_dn(dn, adapter)
|
2014-03-14 03:52:57 -04:00
|
|
|
adapter.user('dn', dn)
|
2014-03-10 08:48:08 -04:00
|
|
|
end
|
|
|
|
|
2014-10-13 11:24:05 -04:00
|
|
|
def self.disabled_via_active_directory?(dn, adapter)
|
2014-05-14 12:26:58 -04:00
|
|
|
adapter.dn_matches_filter?(dn, AD_USER_DISABLED)
|
|
|
|
end
|
|
|
|
|
2014-10-13 11:24:05 -04:00
|
|
|
def initialize(entry, provider)
|
2014-03-10 08:48:08 -04:00
|
|
|
Rails.logger.debug { "Instantiating #{self.class.name} with LDIF:\n#{entry.to_ldif}" }
|
|
|
|
@entry = entry
|
2014-10-13 11:24:05 -04:00
|
|
|
@provider = provider
|
2014-03-10 08:48:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def name
|
2017-01-05 17:01:04 -05:00
|
|
|
attribute_value(:name).first
|
2014-03-10 08:48:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def uid
|
|
|
|
entry.send(config.uid).first
|
|
|
|
end
|
|
|
|
|
|
|
|
def username
|
|
|
|
uid
|
|
|
|
end
|
|
|
|
|
2014-10-13 11:24:05 -04:00
|
|
|
def email
|
2017-01-03 11:05:47 -05:00
|
|
|
attribute_value(:email)
|
2014-10-13 11:24:05 -04:00
|
|
|
end
|
|
|
|
|
2017-02-22 12:51:46 -05:00
|
|
|
delegate :dn, to: :entry
|
2014-03-10 08:48:08 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def entry
|
|
|
|
@entry
|
|
|
|
end
|
|
|
|
|
|
|
|
def config
|
2014-10-13 11:24:05 -04:00
|
|
|
@config ||= Gitlab::LDAP::Config.new(provider)
|
2014-03-10 08:48:08 -04:00
|
|
|
end
|
2017-01-03 11:05:47 -05:00
|
|
|
|
|
|
|
# Using the LDAP attributes configuration, find and return the first
|
|
|
|
# attribute with a value. For example, by default, when given 'email',
|
|
|
|
# this method looks for 'mail', 'email' and 'userPrincipalName' and
|
|
|
|
# returns the first with a value.
|
|
|
|
def attribute_value(attribute)
|
2017-01-05 17:01:04 -05:00
|
|
|
attributes = Array(config.attributes[attribute.to_s])
|
2017-01-03 11:05:47 -05:00
|
|
|
selected_attr = attributes.find { |attr| entry.respond_to?(attr) }
|
|
|
|
|
|
|
|
return nil unless selected_attr
|
|
|
|
|
2017-01-05 17:01:04 -05:00
|
|
|
entry.public_send(selected_attr)
|
2017-01-03 11:05:47 -05:00
|
|
|
end
|
2014-03-10 08:48:08 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|