Inefficiently fetch email from LDAP directory
This commit is contained in:
parent
d96d9005f5
commit
b567dfa542
3 changed files with 30 additions and 13 deletions
|
@ -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 """
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<h1>Account settings</h1>
|
||||
|
||||
<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.
|
||||
</p>
|
||||
|
||||
|
|
Loading…
Reference in a new issue