Class: Ldap::NetLdapFacade
- Inherits:
-
Object
- Object
- Ldap::NetLdapFacade
- Includes:
- Slf4r::Logger
- Defined in:
- lib/ldap/net_ldap_facade.rb
Class Method Summary collapse
Instance Method Summary collapse
- #authenticate(dn, password) ⇒ Object
-
#base(treebase = nil, ldap_base = @ldap.base) ⇒ Object
helper to concat the base from the various parts.
-
#create_object(dn_prefix, treebase, key_field, props, silence = false) ⇒ Object
Nil in case of an error or the new id of the created object.
-
#delete_object(dn_prefix, treebase) ⇒ Object
Nil in case of an error or true.
-
#dn(dn_prefix, treebase) ⇒ Object
helper to concat the dn from the various parts.
-
#initialize(config) ⇒ NetLdapFacade
constructor
A new instance of NetLdapFacade.
-
#read_objects(treebase, key_fields, conditions, field_names, order_field = nil) ⇒ Object
Array of Hashes with a name/values pair for each attribute.
- #retrieve_next_id(treebase, key_field) ⇒ Object
-
#update_object(dn_prefix, treebase, actions) ⇒ Object
Nil in case of an error or true.
Constructor Details
#initialize(config) ⇒ NetLdapFacade
Returns a new instance of NetLdapFacade.
18 19 20 21 22 23 24 25 |
# File 'lib/ldap/net_ldap_facade.rb', line 18 def initialize(config) @ldap_config = config if config.is_a? Hash @ldap = Net::LDAP.new( config ) else @ldap = config end end |
Class Method Details
.open(config) ⇒ Object
9 10 11 12 13 |
# File 'lib/ldap/net_ldap_facade.rb', line 9 def self.open(config) Net::LDAP.open( config ) do |ldap| yield ldap end end |
Instance Method Details
#authenticate(dn, password) ⇒ Object
128 129 130 131 132 133 134 135 |
# File 'lib/ldap/net_ldap_facade.rb', line 128 def authenticate(dn, password) config = @ldap_config.merge(:auth => { :method => :simple, :username => dn, :password => password }) Net::LDAP.new(config).bind end |
#base(treebase = nil, ldap_base = @ldap.base) ⇒ Object
helper to concat the base from the various parts
149 150 151 |
# File 'lib/ldap/net_ldap_facade.rb', line 149 def base(treebase = nil, ldap_base = @ldap.base) [ treebase, ldap_base ].compact.join(",") end |
#create_object(dn_prefix, treebase, key_field, props, silence = false) ⇒ Object
Returns nil in case of an error or the new id of the created object.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/ldap/net_ldap_facade.rb', line 44 def create_object(dn_prefix, treebase, key_field, props, silence = false) if @ldap.add( :dn => dn(dn_prefix, treebase), :attributes => props) || @ldap.get_operation_result.code.to_s == "0" props[key_field.to_sym] else unless silence msg = ldap_error("create", dn(dn_prefix, treebase)) + "\n\t#{props.inspect}" # TODO maybe raise always an error if @ldap.get_operation_result.code.to_s == "68" raise ::DataMapper::PersistenceError.new(msg) else logger.warn(msg) end end nil end end |
#delete_object(dn_prefix, treebase) ⇒ Object
Returns nil in case of an error or true.
114 115 116 117 118 119 120 121 122 123 |
# File 'lib/ldap/net_ldap_facade.rb', line 114 def delete_object(dn_prefix, treebase) if @ldap.delete( :dn => dn(dn_prefix, treebase) ) true else logger.warn(ldap_error("delete", dn(dn_prefix, treebase))) nil end end |
#dn(dn_prefix, treebase) ⇒ Object
helper to concat the dn from the various parts
141 142 143 |
# File 'lib/ldap/net_ldap_facade.rb', line 141 def dn(dn_prefix, treebase) [ dn_prefix, base(treebase) ].compact.join(",") end |
#read_objects(treebase, key_fields, conditions, field_names, order_field = nil) ⇒ Object
Returns Array of Hashes with a name/values pair for each attribute.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/ldap/net_ldap_facade.rb', line 67 def read_objects(treebase, key_fields, conditions, field_names, order_field = nil) searchbase = base(treebase) filter = Conditions2Filter.convert(conditions) # If there is a :dn in the filter skip everything and look it up if dn = conditions.detect { |c| c[1] == "dn" } then searchbase = dn[2] filter = nil end result = [] @ldap.search( :base => searchbase, :attributes => field_names, :filter => filter ) do |res| mapp = to_map(field_names, res) #puts map[key_field.to_sym] # TODO maybe make filter which removes this unless # TODO move this into the ldap_Adapter to make it more general, so that # all field with Integer gets converted, etc result << mapp if key_fields.all? do |key_field| mapp.keys.detect {|k| k.to_s.downcase == key_field.downcase } end end result end |
#retrieve_next_id(treebase, key_field) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/ldap/net_ldap_facade.rb', line 27 def retrieve_next_id(treebase, key_field) id_sym = key_field.downcase.to_sym max = 0 @ldap.search( :base => base(treebase), :attributes => [key_field], :return_result => false ) do |entry| n = entry[id_sym].first.to_i max = n if max < n end max + 1 end |
#update_object(dn_prefix, treebase, actions) ⇒ Object
Returns nil in case of an error or true.
99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/ldap/net_ldap_facade.rb', line 99 def update_object(dn_prefix, treebase, actions) if @ldap.modify( :dn => dn(dn_prefix, treebase), :operations => actions ) || @ldap.get_operation_result.code.to_s == "0" true else puts caller.join("\n") logger.warn(ldap_error("update", dn(dn_prefix, treebase) + "\n\t#{actions.inspect}")) nil end end |