diff --git a/lib/recycledcloud/accounts.ex b/lib/recycledcloud/accounts.ex index 75454c1..fd6fd12 100644 --- a/lib/recycledcloud/accounts.ex +++ b/lib/recycledcloud/accounts.ex @@ -23,7 +23,7 @@ defmodule RecycledCloud.Accounts do """ def get_user_by_username(username) when is_binary(username) do - User.get_by_username(username) + User.get_by_username(username) |> User.maybe_populate_email() end @doc """ @@ -58,7 +58,9 @@ defmodule RecycledCloud.Accounts do ** (Ecto.NoResultsError) """ - def get_user!(id), do: Repo.get!(User, id) + def get_user!(id) do + Repo.get!(User, id) |> User.maybe_populate_email() + end ## User registration @@ -149,8 +151,9 @@ defmodule RecycledCloud.Accounts do defp user_email_multi(user, email, context) do changeset = user |> User.email_changeset(%{email: email}) |> User.confirm_changeset() + # TODO: change email in LDAP here. + Ecto.Multi.new() - |> Ecto.Multi.update(:user, changeset) |> Ecto.Multi.delete_all(:tokens, UserToken.user_and_contexts_query(user, [context])) end @@ -233,7 +236,7 @@ defmodule RecycledCloud.Accounts do """ def get_user_by_session_token(token) do {:ok, query} = UserToken.verify_session_token_query(token) - Repo.one(query) + Repo.one(query) |> User.maybe_populate_email() end @doc """ diff --git a/lib/recycledcloud/accounts/user.ex b/lib/recycledcloud/accounts/user.ex index 7363745..3f06e4c 100644 --- a/lib/recycledcloud/accounts/user.ex +++ b/lib/recycledcloud/accounts/user.ex @@ -4,7 +4,7 @@ defmodule RecycledCloud.Accounts.User do require Logger require Exldap alias RecycledCloud.{LDAP,Accounts} - alias RecycledCloud.Accounts.{User, UserToken, UserNotifier} + alias RecycledCloud.Accounts.User alias RecycledCloud.Repo @derive {Inspect, except: [:password]} @@ -22,10 +22,29 @@ defmodule RecycledCloud.Accounts.User do "uid=#{uid},ou=users,dc=recycled,dc=cloud" end + def maybe_populate_email(user) do + query = fn ldap_conn -> Exldap.search_field(ldap_conn, :uid, user.username) end + case query |> LDAP.execute do + {:ok, []} -> + user + {:ok, result} -> + {:ok, entry} = result |> Enum.fetch(0) + attributes = entry |> Map.get(:attributes) |> Enum.into(%{}) + email = attributes + |> Map.get('mail') + |> Enum.at(0) + |> List.to_string + + user |> Map.put(:email, email) + {:error, _} -> + user + end + end + def get_by_username(username) when is_binary(username) do local_user = Repo.get_by(User, username: username) if local_user do - Map.put(local_user, :email, "unknown@domain.tld") + local_user else query = fn ldap_conn -> Exldap.search_field(ldap_conn, :uid, username) end case query |> LDAP.execute do @@ -42,12 +61,7 @@ defmodule RecycledCloud.Accounts.User do |> Enum.at(0) |> List.to_string - email = attributes - |> Map.get('email') - |> Enum.at(0) - |> List.to_string - - case Accounts.register_user(%{username: username, email: email}) do + case Accounts.register_user(%{username: username}) do {:ok, user} -> user {:error, _} -> nil end diff --git a/lib/recycledcloud_web/templates/user_settings/edit.html.eex b/lib/recycledcloud_web/templates/user_settings/edit.html.eex index b1f1d90..b04adec 100644 --- a/lib/recycledcloud_web/templates/user_settings/edit.html.eex +++ b/lib/recycledcloud_web/templates/user_settings/edit.html.eex @@ -1,7 +1,7 @@
-You are currently logged in as <%= @current_user.username %>, using <% +You are currently logged in as <%= @current_user.username %>, using <%= @current_user.email %> as primary contact method.