forked from AkkomaGang/akkoma
CommonValidations: More refactors.
This commit is contained in:
parent
3655175639
commit
9d7ce1a6d0
1 changed files with 18 additions and 35 deletions
|
@ -84,20 +84,7 @@ def validate_object_or_user_presence(cng, options \\ []) do
|
||||||
end
|
end
|
||||||
|
|
||||||
def validate_host_match(cng, fields \\ [:id, :actor]) do
|
def validate_host_match(cng, fields \\ [:id, :actor]) do
|
||||||
unique_hosts =
|
if same_domain?(cng, fields) do
|
||||||
fields
|
|
||||||
|> Enum.map(fn field ->
|
|
||||||
%URI{host: host} =
|
|
||||||
cng
|
|
||||||
|> get_field(field)
|
|
||||||
|> URI.parse()
|
|
||||||
|
|
||||||
host
|
|
||||||
end)
|
|
||||||
|> Enum.uniq()
|
|
||||||
|> Enum.count()
|
|
||||||
|
|
||||||
if unique_hosts == 1 do
|
|
||||||
cng
|
cng
|
||||||
else
|
else
|
||||||
fields
|
fields
|
||||||
|
@ -109,13 +96,7 @@ def validate_host_match(cng, fields \\ [:id, :actor]) do
|
||||||
end
|
end
|
||||||
|
|
||||||
def validate_fields_match(cng, fields) do
|
def validate_fields_match(cng, fields) do
|
||||||
unique_fields =
|
if map_unique?(cng, fields) do
|
||||||
fields
|
|
||||||
|> Enum.map(fn field -> get_field(cng, field) end)
|
|
||||||
|> Enum.uniq()
|
|
||||||
|> Enum.count()
|
|
||||||
|
|
||||||
if unique_fields == 1 do
|
|
||||||
cng
|
cng
|
||||||
else
|
else
|
||||||
fields
|
fields
|
||||||
|
@ -126,21 +107,23 @@ def validate_fields_match(cng, fields) do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp map_unique?(cng, fields, func \\ & &1) do
|
||||||
|
Enum.reduce_while(fields, nil, fn field, acc ->
|
||||||
|
value =
|
||||||
|
cng
|
||||||
|
|> get_field(field)
|
||||||
|
|> func.()
|
||||||
|
|
||||||
|
case {value, acc} do
|
||||||
|
{value, nil} -> {:cont, value}
|
||||||
|
{value, value} -> {:cont, value}
|
||||||
|
_ -> {:halt, false}
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
def same_domain?(cng, fields \\ [:actor, :object]) do
|
def same_domain?(cng, fields \\ [:actor, :object]) do
|
||||||
unique_domains =
|
map_unique?(cng, fields, fn value -> URI.parse(value).host end)
|
||||||
fields
|
|
||||||
|> Enum.map(fn field ->
|
|
||||||
%URI{host: host} =
|
|
||||||
cng
|
|
||||||
|> get_field(field)
|
|
||||||
|> URI.parse()
|
|
||||||
|
|
||||||
host
|
|
||||||
end)
|
|
||||||
|> Enum.uniq()
|
|
||||||
|> Enum.count()
|
|
||||||
|
|
||||||
unique_domains == 1
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# This figures out if a user is able to create, delete or modify something
|
# This figures out if a user is able to create, delete or modify something
|
||||||
|
|
Loading…
Reference in a new issue