Add email to user account only if it exists in LDAP
authorMark Felder <feld@FreeBSD.org>
Wed, 5 Aug 2020 16:36:12 +0000 (11:36 -0500)
committerMark Felder <feld@FreeBSD.org>
Wed, 5 Aug 2020 16:36:12 +0000 (11:36 -0500)
lib/pleroma/user.ex
lib/pleroma/web/auth/ldap_authenticator.ex

index df9f34baa072d1fbe9cadaafd156b9083abd99fe..6d39c9d1b1e8986827e15c593b93fc12a2963956 100644 (file)
@@ -643,12 +643,21 @@ defmodule Pleroma.User do
       when is_nil(password) do
     params = Map.put_new(params, :accepts_chat_messages, true)
 
+    params =
+      if Map.has_key?(params, :email) do
+        Map.put_new(params, :email, params[:email])
+      else
+        params
+      end
+
     struct
     |> cast(params, [
       :name,
       :nickname,
+      :email,
       :accepts_chat_messages
     ])
+    |> validate_required([:name, :nickname])
     |> unique_constraint(:nickname)
     |> validate_exclusion(:nickname, Config.get([User, :restricted_nicknames]))
     |> validate_format(:nickname, local_nickname_regex())
index f667da68b90fa726a04c84c8e0ec41cdcf878455..b1645a3591d9aa58a78872608bde203a888a6828 100644 (file)
@@ -103,13 +103,19 @@ defmodule Pleroma.Web.Auth.LDAPAuthenticator do
            {:scope, :eldap.wholeSubtree()},
            {:timeout, @search_timeout}
          ]) do
-      {:ok, {:eldap_search_result, [{:eldap_entry, _, _}], _}} ->
+      {:ok, {:eldap_search_result, [{:eldap_entry, _, attributes}], _}} ->
         params = %{
           name: name,
           nickname: name,
           password: nil
         }
 
+        params =
+          case List.keyfind(attributes, 'mail', 0) do
+            {_, [mail]} -> Map.put_new(params, :email, :erlang.list_to_binary(mail))
+            _ -> params
+          end
+
         changeset = User.register_changeset(%User{}, params)
 
         case User.register(changeset) do