class ActiveDirectory::Client

Public Class Methods

add(dn, attrs) click to toggle source
# File lib/active_directory/client.rb, line 78
def self.add(dn, attrs)
  puts dn
  puts attrs
  result = connection.add(:dn => dn, :attributes => attrs)
  if result
    message = "LDAP-Object #{dn} was created"
    return result, message
  else
    mesage = "LDAP-Object #{dn} was not created. \
    Error: #{connection.get_operation_result}"
    return result, message
  end

  # DirectoryUser.add(
  #   "CN=Test Dude,OU=Testing,OU=Accounts,DC=synapsedev,DC=com",
  #   {
  #     name: "Test dude",
  #     samaccountname: "test_dude_123",
  #     objectclass:["top", "user"],
  #     sn: "Dude",
  #     cn: "Test Dude",
  #     givenname: "Test"
  #   }
  # )
end
add_attribute(dn, field, value) click to toggle source
# File lib/active_directory/client.rb, line 69
def self.add_attribute(dn, field, value)
  if connection.add_attribute(dn, field, value)
    true
  else
    raise StandardError, "LDAP-Attribute (#{field}) wasnt added for \
      #{dn}. Error: #{connection.get_operation_result}"
  end
end
connection() click to toggle source
# File lib/active_directory/client.rb, line 3
def self.connection
  ldap = Net::LDAP.new(
    host:  ActiveDirectory::Configuration.ldap_host,
    port: ActiveDirectory::Configuration.ldap_port,
    encryption: :simple_tls
  )
  ldap.authenticate(
    ActiveDirectory::Configuration.username,
    ActiveDirectory::Configuration.password
  )
  ldap
end
delete(dn) click to toggle source
# File lib/active_directory/client.rb, line 104
def self.delete(dn)
  if connection.delete(dn: dn)
    true
  else
    raise StandardError,  "LDAP-Object #{dn} was not deleted. Error: #{connection.get_operation_result}"
  end
end
delete_attribute(dn, field) click to toggle source
# File lib/active_directory/client.rb, line 47
def self.delete_attribute(dn, field)
  if connection.delete_attribute(dn, field.to_sym)
    # remove_instance_variable("@#{field}".to_sym)
    true
  else
    raise StandardError, "LDAP-Attribute #{field} was not deleted for [#{dn}]"
  end
end
modify(dn, operations) click to toggle source
# File lib/active_directory/client.rb, line 30
def self.modify(dn, operations)
  if connection.modify(dn: dn, operations: operations)
    true
  else
    false
  end
end
modify_unicode_pwd(dn, old_password, new_password) click to toggle source
# File lib/active_directory/client.rb, line 56
def self.modify_unicode_pwd(dn, old_password, new_password)
  ops = [
    [:delete, :unicodePwd, old_password],
    [:add, :unicodePwd, new_password]
  ]
  if connection.modify(dn: dn, operations: ops)
    true
  else
    raise StandardError, "LDAP-Attribute password for #{dn} was not \
      updated. Error: #{connection.get_operation_result}"
  end
end
rename(old_dn, new_dn) click to toggle source
# File lib/active_directory/client.rb, line 38
def self.rename(old_dn, new_dn)
  connection.rename(
    olddn: old_dn,
    newrdn: new_dn,
    delete_attributes: true,
    new_superior: "OU=Automated Groups,OU=Groups,DC=synapsedev,DC=com"
  )
end
update_attribute(dn, field, value) click to toggle source
# File lib/active_directory/client.rb, line 21
def self.update_attribute(dn, field, value)
  if connection.replace_attribute(dn, field, value)
    true
  else
    raise StandardError, "LDAP-Attribute #{field} was not updated for \
    [#{dn}]. Error #{connection.get_operation_result}"
  end
end