ecto_ldap v0.4.0 Ecto.Ldap.Adapter

Allows talking to an LDAP directory as an Ecto data store.

Sample Configuration

use Mix.Config

config :my_app, MyApp.Repo,
  adapter: Ecto.Ldap.Adapter,
  hostname: "ldap.example.com",
  base: "dc=example,dc=com",
  port: 636,
  ssl: true,
  user_dn: "uid=sample_user,ou=users,dc=example,dc=com",
  password: "password",
  pool_size: 1

Currently ecto_ldap does not support a pool_size larger than 1. If this is a bottleneck for you, please open an issue.

Example schema

defmodule TestUser do
  use Ecto.Schema
  import Ecto.Changeset

  @primary_key {:dn, :string, autogenerate: false}
  schema "users" do
    field :objectClass, {:array, :string}
    field :loginShell, :string
    field :mail, :string
    field :mobile, :string
    field :skills, {:array, :string}
    field :sn, :string
    field :st, :string
    field :startDate, Ecto.DateTime
    field :uid, :string
    field :jpegPhoto, :binary
  end

  def changeset(model, params \ :empty) do
    model
    |> cast(params, ~w(dn), ~w(objectClass loginShell mail mobile skills sn uid))
    |> unique_constraint(:dn)
  end

end

Example Usage

iex> require Ecto.Query
iex> Ecto.Query.from(u in TestUser, select: u.uid) |> TestRepo.all
["jeff.weiss", "manny"]

iex> TestRepo.all(TestUser, uid: "jeff.weiss") |> Enum.count
1

iex> TestRepo.get(TestUser, "uid=jeff.weiss,ou=users,dc=example,dc=com").mail
"jeff.weiss@example.com"

iex> TestRepo.get_by(TestUser, uid: "jeff.weiss").loginShell
"/bin/zsh"

iex> Ecto.Query.from(u in TestUser, where: u.st == "OR" and "elixir" in u.skills) |> TestRepo.all |> List.first |> Map.get(:uid)
"jeff.weiss"

iex> Ecto.Query.from(u in TestUser, where: like(u.sn, "%Weis%")) |> TestRepo.all |> List.first |> Map.get(:uid)
"jeff.weiss"

Link to this section Summary

Functions

Called to autogenerate a value for id/embed_id/binary_id

Deletes a single struct with the given filters

Ensure all applications necessary to run the adapter are started

Inserts a single new struct in the data store

Inserts multiple entries into the data store

Commands invoked to prepare a query for all, update_all and delete_all

Drops the storage given by options

Creates the storage given by options

Updates a single struct with the given filters

Link to this section Functions

Link to this function autogenerate(_)

Called to autogenerate a value for id/embed_id/binary_id.

Returns the autogenerated value, or nil if it must be autogenerated inside the storage or raise if not supported.

Callback implementation for Ecto.Adapter.autogenerate/1.

Link to this function count_fields(fields, sources)
Link to this function delete(_, _, _, _)

Deletes a single struct with the given filters.

While filters can be any record column, it is expected that at least the primary key (or any other key that uniquely identifies an existing record) be given as a filter. Therefore, in case there is no record matching the given filters, {:error, :stale} is returned.

Callback implementation for Ecto.Adapter.delete/4.

Link to this function ensure_all_started(_, _)

Ensure all applications necessary to run the adapter are started.

Callback implementation for Ecto.Adapter.ensure_all_started/2.

Link to this function execute(repo, query_metadata, arg, params, preprocess, options)

Executes a previously prepared query.

It must return a tuple containing the number of entries and the result set as a list of lists. The result set may also be nil if a particular operation does not support them.

The meta field is a map containing some of the fields found in the Ecto.Query struct.

It receives a process function that should be invoked for each selected field in the query result in order to convert them to the expected Ecto type. The process function will be nil if no result set is expected from the query.

Callback implementation for Ecto.Adapter.execute/6.

Link to this function insert(_, _, _, _, _, _)

Inserts a single new struct in the data store.

Autogenerate

The primary key will be automatically included in returning if the field has type :id or :binary_id and no value was set by the developer or none was autogenerated by the adapter.

Callback implementation for Ecto.Adapter.insert/6.

Link to this function insert_all(_, _, _, _, _, _, _)

Inserts multiple entries into the data store.

Callback implementation for Ecto.Adapter.insert_all/7.

Link to this function prepare(atom, query)

Commands invoked to prepare a query for all, update_all and delete_all.

The returned result is given to execute/6.

Callback implementation for Ecto.Adapter.prepare/2.

Link to this function storage_down(_)

Drops the storage given by options.

Returns :ok if it was dropped successfully.

Returns {:error, :already_down} if the storage has already been dropped or {:error, term} in case anything else goes wrong.

Examples

storage_down(username: postgres,
             database: 'ecto_test',
             hostname: 'localhost')

Callback implementation for Ecto.Adapter.Storage.storage_down/1.

Creates the storage given by options.

Returns :ok if it was created successfully.

Returns {:error, :already_up} if the storage has already been created or {:error, term} in case anything else goes wrong.

Examples

storage_up(username: postgres,
           database: 'ecto_test',
           hostname: 'localhost')

Callback implementation for Ecto.Adapter.Storage.storage_up/1.

Link to this function update(repo, schema_meta, fields, filters, returning, options)

Updates a single struct with the given filters.

While filters can be any record column, it is expected that at least the primary key (or any other key that uniquely identifies an existing record) be given as a filter. Therefore, in case there is no record matching the given filters, {:error, :stale} is returned.

Callback implementation for Ecto.Adapter.update/6.