LDAP: Rename client method, add modify method

This commit is contained in:
Râu Cao 2024-03-13 14:26:44 +01:00
parent eac8fa6edb
commit 38b3d68fd5
Signed by: raucao
GPG Key ID: 37036C356E56CC51
2 changed files with 24 additions and 17 deletions

View File

@ -9,7 +9,7 @@ module LdapManager
attributes = %w{ jpegPhoto } attributes = %w{ jpegPhoto }
filter = Net::LDAP::Filter.eq("cn", @cn) filter = Net::LDAP::Filter.eq("cn", @cn)
entry = ldap_client.search(base: treebase, filter: filter, attributes: attributes).first entry = client.search(base: treebase, filter: filter, attributes: attributes).first
entry.try(:jpegPhoto) ? entry.jpegPhoto.first : nil entry.try(:jpegPhoto) ? entry.jpegPhoto.first : nil
end end
end end

View File

@ -3,30 +3,37 @@ class LdapService < ApplicationService
@suffix = ENV["LDAP_SUFFIX"] || "dc=kosmos,dc=org" @suffix = ENV["LDAP_SUFFIX"] || "dc=kosmos,dc=org"
end end
def modify(dn, operations=[])
client.modify dn: dn, operations: operations
client.get_operation_result.code
end
def add_attribute(dn, attr, values) def add_attribute(dn, attr, values)
ldap_client.add_attribute dn, attr, values client.add_attribute dn, attr, values
client.get_operation_result.code
end end
def replace_attribute(dn, attr, values) def replace_attribute(dn, attr, values)
ldap_client.replace_attribute dn, attr, values client.replace_attribute dn, attr, values
client.get_operation_result.code
end end
def delete_attribute(dn, attr) def delete_attribute(dn, attr)
ldap_client.delete_attribute dn, attr client.delete_attribute dn, attr
client.get_operation_result.code
end end
def add_entry(dn, attrs, interactive=false) def add_entry(dn, attrs, interactive=false)
puts "Adding entry: #{dn}" if interactive puts "Add entry: #{dn}" if interactive
res = ldap_client.add dn: dn, attributes: attrs client.add dn: dn, attributes: attrs
puts res.inspect if interactive && !res client.get_operation_result.code
res
end end
def delete_entry(dn, interactive=false) def delete_entry(dn, interactive=false)
puts "Deleting entry: #{dn}" if interactive puts "Delete entry: #{dn}" if interactive
res = ldap_client.delete dn: dn client.delete dn: dn
puts res.inspect if interactive && !res client.get_operation_result.code
res end
end end
def delete_all_entries! def delete_all_entries!
@ -35,7 +42,7 @@ class LdapService < ApplicationService
end end
filter = Net::LDAP::Filter.eq("objectClass", "*") filter = Net::LDAP::Filter.eq("objectClass", "*")
entries = ldap_client.search(base: @suffix, filter: filter, attributes: %w{dn}) entries = client.search(base: @suffix, filter: filter, attributes: %w{dn})
entries.sort_by!{ |e| e.dn.length }.reverse! entries.sort_by!{ |e| e.dn.length }.reverse!
entries.each do |e| entries.each do |e|
@ -56,7 +63,7 @@ class LdapService < ApplicationService
] ]
filter = Net::LDAP::Filter.eq("uid", args[:uid] || "*") filter = Net::LDAP::Filter.eq("uid", args[:uid] || "*")
entries = ldap_client.search(base: treebase, filter: filter, attributes: attributes) entries = client.search(base: treebase, filter: filter, attributes: attributes)
entries.sort_by! { |e| e.cn[0] } entries.sort_by! { |e| e.cn[0] }
entries = entries.collect do |e| entries = entries.collect do |e|
{ {
@ -77,7 +84,7 @@ class LdapService < ApplicationService
# filter = Net::LDAP::Filter.eq("objectClass", "*") # filter = Net::LDAP::Filter.eq("objectClass", "*")
treebase = "cn=users,#{@suffix}" treebase = "cn=users,#{@suffix}"
entries = ldap_client.search(base: treebase, filter: filter, attributes: attributes) entries = client.search(base: treebase, filter: filter, attributes: attributes)
entries.sort_by! { |e| e.ou[0] } entries.sort_by! { |e| e.ou[0] }
@ -129,8 +136,8 @@ class LdapService < ApplicationService
private private
def ldap_client def client
ldap_client ||= Net::LDAP.new host: ldap_config['host'], client ||= Net::LDAP.new host: ldap_config['host'],
port: ldap_config['port'], port: ldap_config['port'],
# TODO has to be :simple_tls if TLS is enabled # TODO has to be :simple_tls if TLS is enabled
# encryption: ldap_config['ssl'], # encryption: ldap_config['ssl'],