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
Executes a previously prepared query
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
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
.
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
.
Ensure all applications necessary to run the adapter are started.
Callback implementation for Ecto.Adapter.ensure_all_started/2
.
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
.
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
.
Inserts multiple entries into the data store.
Callback implementation for Ecto.Adapter.insert_all/7
.
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
.
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
.
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
.