Wire mail change to LDAP backend

This commit is contained in:
Timothée Floure 2020-12-22 15:10:38 +01:00
parent ead51b3082
commit 061a748c2a
Signed by: tfloure
GPG Key ID: 4502C902C00A1E12
2 changed files with 18 additions and 5 deletions

View File

@ -139,7 +139,8 @@ defmodule RecycledCloud.Accounts do
with {:ok, query} <- UserToken.verify_change_email_token_query(token, context),
%UserToken{sent_to: email} <- Repo.one(query),
{:ok, _} <- Repo.transaction(user_email_multi(user, email, context)) do
{:ok, _} <- Repo.transaction(user_email_multi(user, email, context)),
{:ok, _} <- User.set_email(user, email) do
:ok
else
_ -> :error
@ -147,11 +148,13 @@ defmodule RecycledCloud.Accounts do
end
defp user_email_multi(user, email, context) do
changeset = user |> User.email_changeset(%{email: email}) |> User.confirm_changeset()
# TODO: change email in LDAP here.
changeset = user
|> User.email_changeset(%{email: email})
|> User.confirm_changeset()
# Delete existing tokens.
Ecto.Multi.new()
|> Ecto.Multi.update(:user, changeset)
|> Ecto.Multi.delete_all(:tokens, UserToken.user_and_contexts_query(user, [context]))
end
@ -207,7 +210,7 @@ defmodule RecycledCloud.Accounts do
case User.set_password(user, new_password) do
{:ok, _} -> {:ok, user}
{:error, err} ->
changeset_errors = [current_password: {"Unknown error.", []}]
changeset_errors = [current_password: {"Unknown error: #{err}", [err: inspect(err)]}]
updated_changeset = changeset
|> Map.put(:action, :update)
|> Map.put(:errors, changeset_errors)

View File

@ -191,4 +191,14 @@ defmodule RecycledCloud.Accounts.User do
query |> LDAP.execute_single
end
def set_email(user, email) do
ldif = :eldap.mod_replace('mail', [email |> String.to_charlist])
query = fn ldap_conn ->
:eldap.modify(ldap_conn, String.to_charlist(user.dn), [ldif])
end
query |> LDAP.execute_single
end
end