module LdapManager
  class FetchUserByNostrKey < LdapManagerService
    def initialize(pubkey:)
      @ou = Setting.primary_domain
      @pubkey = pubkey
    end

    def call
      treebase   = "ou=#{@ou},cn=users,#{ldap_suffix}"
      attributes = %w{ cn }
      filter     = Net::LDAP::Filter.eq("nostrKey", @pubkey)

      entry = client.search(base: treebase, filter: filter, attributes: attributes).first

      User.find_by cn: entry.cn, ou: @ou  unless entry.nil?
    end
  end
end
