Inefficiently fetch email from LDAP directory

This commit is contained in:
Timothée Floure 2020-12-22 13:20:54 +01:00
parent d96d9005f5
commit b567dfa542
Signed by: tfloure
GPG key ID: 4502C902C00A1E12
3 changed files with 30 additions and 13 deletions

View file

@ -23,7 +23,7 @@ defmodule RecycledCloud.Accounts do
""" """
def get_user_by_username(username) when is_binary(username) 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 end
@doc """ @doc """
@ -58,7 +58,9 @@ defmodule RecycledCloud.Accounts do
** (Ecto.NoResultsError) ** (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 ## User registration
@ -149,8 +151,9 @@ defmodule RecycledCloud.Accounts do
defp user_email_multi(user, email, context) do defp user_email_multi(user, email, context) do
changeset = user |> User.email_changeset(%{email: email}) |> User.confirm_changeset() changeset = user |> User.email_changeset(%{email: email}) |> User.confirm_changeset()
# TODO: change email in LDAP here.
Ecto.Multi.new() Ecto.Multi.new()
|> Ecto.Multi.update(:user, changeset)
|> Ecto.Multi.delete_all(:tokens, UserToken.user_and_contexts_query(user, [context])) |> Ecto.Multi.delete_all(:tokens, UserToken.user_and_contexts_query(user, [context]))
end end
@ -233,7 +236,7 @@ defmodule RecycledCloud.Accounts do
""" """
def get_user_by_session_token(token) do def get_user_by_session_token(token) do
{:ok, query} = UserToken.verify_session_token_query(token) {:ok, query} = UserToken.verify_session_token_query(token)
Repo.one(query) Repo.one(query) |> User.maybe_populate_email()
end end
@doc """ @doc """

View file

@ -4,7 +4,7 @@ defmodule RecycledCloud.Accounts.User do
require Logger require Logger
require Exldap require Exldap
alias RecycledCloud.{LDAP,Accounts} alias RecycledCloud.{LDAP,Accounts}
alias RecycledCloud.Accounts.{User, UserToken, UserNotifier} alias RecycledCloud.Accounts.User
alias RecycledCloud.Repo alias RecycledCloud.Repo
@derive {Inspect, except: [:password]} @derive {Inspect, except: [:password]}
@ -22,10 +22,29 @@ defmodule RecycledCloud.Accounts.User do
"uid=#{uid},ou=users,dc=recycled,dc=cloud" "uid=#{uid},ou=users,dc=recycled,dc=cloud"
end 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 def get_by_username(username) when is_binary(username) do
local_user = Repo.get_by(User, username: username) local_user = Repo.get_by(User, username: username)
if local_user do if local_user do
Map.put(local_user, :email, "unknown@domain.tld") local_user
else else
query = fn ldap_conn -> Exldap.search_field(ldap_conn, :uid, username) end query = fn ldap_conn -> Exldap.search_field(ldap_conn, :uid, username) end
case query |> LDAP.execute do case query |> LDAP.execute do
@ -42,12 +61,7 @@ defmodule RecycledCloud.Accounts.User do
|> Enum.at(0) |> Enum.at(0)
|> List.to_string |> List.to_string
email = attributes case Accounts.register_user(%{username: username}) do
|> Map.get('email')
|> Enum.at(0)
|> List.to_string
case Accounts.register_user(%{username: username, email: email}) do
{:ok, user} -> user {:ok, user} -> user
{:error, _} -> nil {:error, _} -> nil
end end

View file

@ -1,7 +1,7 @@
<h1>Account settings</h1> <h1>Account settings</h1>
<p> <p>
You are currently logged in as <i><%= @current_user.username %></i>, using <% You are currently logged in as <i><%= @current_user.username %></i>, using <%=
@current_user.email %> as primary contact method. @current_user.email %> as primary contact method.
</p> </p>