Make the email attribute configurable

While here, fix the System.get_env usage to use the normal fallback value method and improve the UID label description
This commit is contained in:
Mark Felder 2024-09-17 13:36:46 -04:00
parent 14a9663f1a
commit 363b462c54
3 changed files with 17 additions and 7 deletions

View file

@ -612,16 +612,17 @@ config :pleroma, Pleroma.Formatter,
config :pleroma, :ldap, config :pleroma, :ldap,
enabled: System.get_env("LDAP_ENABLED") == "true", enabled: System.get_env("LDAP_ENABLED") == "true",
host: System.get_env("LDAP_HOST") || "localhost", host: System.get_env("LDAP_HOST", "localhost"),
port: String.to_integer(System.get_env("LDAP_PORT") || "389"), port: String.to_integer(System.get_env("LDAP_PORT", "389")),
ssl: System.get_env("LDAP_SSL") == "true", ssl: System.get_env("LDAP_SSL") == "true",
sslopts: [], sslopts: [],
tls: System.get_env("LDAP_TLS") == "true", tls: System.get_env("LDAP_TLS") == "true",
tlsopts: [], tlsopts: [],
base: System.get_env("LDAP_BASE") || "dc=example,dc=com", base: System.get_env("LDAP_BASE", "dc=example,dc=com"),
uid: System.get_env("LDAP_UID") || "cn", uid: System.get_env("LDAP_UID", "cn"),
# defaults to CAStore's Mozilla roots # defaults to CAStore's Mozilla roots
cacertfile: nil cacertfile: System.get_env("LDAP_CACERTFILE", nil),
mail: System.get_env("LDAP_MAIL", "mail")
oauth_consumer_strategies = oauth_consumer_strategies =
System.get_env("OAUTH_CONSUMER_STRATEGIES") System.get_env("OAUTH_CONSUMER_STRATEGIES")

View file

@ -2280,7 +2280,7 @@ config :pleroma, :config_description, [
}, },
%{ %{
key: :uid, key: :uid,
label: "UID", label: "UID Attribute",
type: :string, type: :string,
description: description:
"LDAP attribute name to authenticate the user, e.g. when \"cn\", the filter will be \"cn=username,base\"", "LDAP attribute name to authenticate the user, e.g. when \"cn\", the filter will be \"cn=username,base\"",
@ -2291,6 +2291,13 @@ config :pleroma, :config_description, [
label: "CACertfile", label: "CACertfile",
type: :string, type: :string,
description: "Path to CA certificate file" description: "Path to CA certificate file"
},
%{
key: :mail,
label: "Mail Attribute",
type: :string,
description: "LDAP attribute name to use as the email address when automatically registering the user on first login",
suggestions: ["mail"]
} }
] ]
}, },

View file

@ -205,6 +205,8 @@ defmodule Pleroma.LDAP do
end end
defp try_register(name, attributes) do defp try_register(name, attributes) do
mail_attribute = Config.get([:ldap, :mail])
params = %{ params = %{
name: name, name: name,
nickname: name, nickname: name,
@ -212,7 +214,7 @@ defmodule Pleroma.LDAP do
} }
params = params =
case List.keyfind(attributes, ~c"mail", 0) do case List.keyfind(attributes, to_charlist(mail_attribute), 0) do
{_, [mail]} -> Map.put_new(params, :email, :erlang.list_to_binary(mail)) {_, [mail]} -> Map.put_new(params, :email, :erlang.list_to_binary(mail))
_ -> params _ -> params
end end