diff --git a/lib/recycledcloud/accounts.ex b/lib/recycledcloud/accounts.ex index cafd9b5..94ce1ce 100644 --- a/lib/recycledcloud/accounts.ex +++ b/lib/recycledcloud/accounts.ex @@ -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) diff --git a/lib/recycledcloud/accounts/user.ex b/lib/recycledcloud/accounts/user.ex index 77ee9fd..c9a1653 100644 --- a/lib/recycledcloud/accounts/user.ex +++ b/lib/recycledcloud/accounts/user.ex @@ -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