Make validation functions for restricted nicknames and blacklisted domains; do restricted nickname validation in LDAP account registration
This commit is contained in:
parent
0d4aceb9b0
commit
6e7b919637
|
@ -706,7 +706,7 @@ def register_changeset_ldap(struct, params = %{password: password})
|
||||||
])
|
])
|
||||||
|> validate_required([:name, :nickname])
|
|> validate_required([:name, :nickname])
|
||||||
|> unique_constraint(:nickname)
|
|> unique_constraint(:nickname)
|
||||||
|> validate_exclusion(:nickname, Config.get([User, :restricted_nicknames]))
|
|> validate_not_restricted_nickname(:nickname)
|
||||||
|> validate_format(:nickname, local_nickname_regex())
|
|> validate_format(:nickname, local_nickname_regex())
|
||||||
|> put_ap_id()
|
|> put_ap_id()
|
||||||
|> unique_constraint(:ap_id)
|
|> unique_constraint(:ap_id)
|
||||||
|
@ -754,30 +754,9 @@ def register_changeset(struct, params \\ %{}, opts \\ []) do
|
||||||
|> validate_confirmation(:password)
|
|> validate_confirmation(:password)
|
||||||
|> unique_constraint(:email)
|
|> unique_constraint(:email)
|
||||||
|> validate_format(:email, @email_regex)
|
|> validate_format(:email, @email_regex)
|
||||||
|> validate_change(:email, fn :email, email ->
|
|> validate_email_not_in_blacklisted_domain(:email)
|
||||||
valid? =
|
|
||||||
Config.get([User, :email_blacklist])
|
|
||||||
|> Enum.all?(fn blacklisted_domain ->
|
|
||||||
blacklisted_domain_downcase = String.downcase(blacklisted_domain)
|
|
||||||
|
|
||||||
!String.ends_with?(String.downcase(email), [
|
|
||||||
"@" <> blacklisted_domain_downcase,
|
|
||||||
"." <> blacklisted_domain_downcase
|
|
||||||
])
|
|
||||||
end)
|
|
||||||
|
|
||||||
if valid?, do: [], else: [email: "Invalid email"]
|
|
||||||
end)
|
|
||||||
|> unique_constraint(:nickname)
|
|> unique_constraint(:nickname)
|
||||||
|> validate_change(:nickname, fn :nickname, nickname ->
|
|> validate_not_restricted_nickname(:nickname)
|
||||||
valid? =
|
|
||||||
Config.get([User, :restricted_nicknames])
|
|
||||||
|> Enum.all?(fn restricted_nickname ->
|
|
||||||
String.downcase(nickname) != String.downcase(restricted_nickname)
|
|
||||||
end)
|
|
||||||
|
|
||||||
if valid?, do: [], else: [nickname: "Invalid nickname"]
|
|
||||||
end)
|
|
||||||
|> validate_format(:nickname, local_nickname_regex())
|
|> validate_format(:nickname, local_nickname_regex())
|
||||||
|> validate_length(:bio, max: bio_limit)
|
|> validate_length(:bio, max: bio_limit)
|
||||||
|> validate_length(:name, min: 1, max: name_limit)
|
|> validate_length(:name, min: 1, max: name_limit)
|
||||||
|
@ -791,6 +770,35 @@ def register_changeset(struct, params \\ %{}, opts \\ []) do
|
||||||
|> put_following_and_follower_and_featured_address()
|
|> put_following_and_follower_and_featured_address()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def validate_not_restricted_nickname(changeset, field) do
|
||||||
|
validate_change changeset, field, fn _, value ->
|
||||||
|
valid? =
|
||||||
|
Config.get([User, :restricted_nicknames])
|
||||||
|
|> Enum.all?(fn restricted_nickname ->
|
||||||
|
String.downcase(value) != String.downcase(restricted_nickname)
|
||||||
|
end)
|
||||||
|
|
||||||
|
if valid?, do: [], else: [nickname: "Invalid nickname"]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def validate_email_not_in_blacklisted_domain(changeset, field) do
|
||||||
|
validate_change changeset, field, fn _, value ->
|
||||||
|
valid? =
|
||||||
|
Config.get([User, :email_blacklist])
|
||||||
|
|> Enum.all?(fn blacklisted_domain ->
|
||||||
|
blacklisted_domain_downcase = String.downcase(blacklisted_domain)
|
||||||
|
|
||||||
|
!String.ends_with?(String.downcase(value), [
|
||||||
|
"@" <> blacklisted_domain_downcase,
|
||||||
|
"." <> blacklisted_domain_downcase
|
||||||
|
])
|
||||||
|
end)
|
||||||
|
|
||||||
|
if valid?, do: [], else: [email: "Invalid email"]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def maybe_validate_required_email(changeset, true), do: changeset
|
def maybe_validate_required_email(changeset, true), do: changeset
|
||||||
|
|
||||||
def maybe_validate_required_email(changeset, _) do
|
def maybe_validate_required_email(changeset, _) do
|
||||||
|
|
Loading…
Reference in New Issue