formatting

This commit is contained in:
Moon Man 2024-12-02 07:38:14 -05:00
parent 8ccd8e6586
commit c6ef4a5dd3
7 changed files with 38 additions and 22 deletions

View File

@ -26,16 +26,18 @@ defmodule BallsPDS.Ecto.Schema.Agent do
end end
def get_by_acl(acl) when is_binary(acl) do def get_by_acl(acl) when is_binary(acl) do
query = from a in __MODULE__, query =
where: a.acl == ^acl from(a in __MODULE__,
where: a.acl == ^acl
)
Repo.one(query) Repo.one(query)
end end
def get_or_create_by_acl(acl) when is_binary(acl) do def get_or_create_by_acl(acl) when is_binary(acl) do
with {:existing, nil} <- {:existing, get_by_acl(acl)}, with {:existing, nil} <- {:existing, get_by_acl(acl)},
{:insert, {:ok, new}} <- {:insert, Repo.insert(%__MODULE__{acl: acl, disabled: false})} do {:insert, {:ok, new}} <- {:insert, Repo.insert(%__MODULE__{acl: acl, disabled: false})} do
{:ok, new} {:ok, new}
else else
{:existing, existing} -> {:ok, existing} {:existing, existing} -> {:ok, existing}
{:insert, {:error, _} = error} -> error {:insert, {:error, _} = error} -> error

View File

@ -51,16 +51,24 @@ defmodule BallsPDS.Ecto.Schema.CollectionObject do
end end
def delete_all(%Object{id: collection_id}, ids) when is_list(ids) do def delete_all(%Object{id: collection_id}, ids) when is_list(ids) do
{object_ids, remote_ids} = ids |> Enum.map(&resolve_id/1) |> Enum.reduce({[], []}, fn {object_ids, remote_ids} =
nil, acc -> acc ids
|> Enum.map(&resolve_id/1)
|> Enum.reduce({[], []}, fn
nil, acc ->
acc
object_id, {object_ids, remote_ids} when is_integer(object_id) -> {[object_id | object_ids], remote_ids} object_id, {object_ids, remote_ids} when is_integer(object_id) ->
{[object_id | object_ids], remote_ids}
remote_id, {object_ids, remote_ids} when is_binary(remote_id) -> {object_ids, [remote_id | remote_ids]} remote_id, {object_ids, remote_ids} when is_binary(remote_id) ->
end) {object_ids, [remote_id | remote_ids]}
end)
from(co in __MODULE__, from(co in __MODULE__,
where: co.collection_id == ^collection_id and (co.object_id in ^object_ids or co.remote_id in ^remote_ids) where:
co.collection_id == ^collection_id and
(co.object_id in ^object_ids or co.remote_id in ^remote_ids)
) )
|> Repo.delete_all() |> Repo.delete_all()
end end

View File

@ -50,7 +50,8 @@ defmodule BallsPDS.Ecto.Schema.Object do
defp validate_path(changeset, field) do defp validate_path(changeset, field) do
validate_change(changeset, field, fn _, path -> validate_change(changeset, field, fn _, path ->
case BallsPDS.Util.Util.is_valid_path?(path) do case BallsPDS.Util.Util.is_valid_path?(path) do
:ok -> [] :ok ->
[]
{:error, :invalid_char} -> {:error, :invalid_char} ->
[{field, "Invalid characters"}] [{field, "Invalid characters"}]

View File

@ -34,10 +34,10 @@ defmodule BallsPDS.JWT do
with {:subject, {:ok, %{"sub" => subject}}} <- {:subject, JOSE.JWT.peek_payload(jwt)}, with {:subject, {:ok, %{"sub" => subject}}} <- {:subject, JOSE.JWT.peek_payload(jwt)},
{:kid, {:ok, %{"kid" => kid}}} <- {:kid, JOSE.JWT.peek_protected(jwt)} do {:kid, {:ok, %{"kid" => kid}}} <- {:kid, JOSE.JWT.peek_protected(jwt)} do
{:ok, %{subject: subject, id: kid}} {:ok, %{subject: subject, id: kid}}
else else
{err, {:error, error}} -> {err, {:error, error}} ->
Logger.error("extracting key info from JWT: #{err}: #{inspect(error)}") Logger.error("extracting key info from JWT: #{err}: #{inspect(error)}")
{:error, error} {:error, error}
end end
end end

View File

@ -91,9 +91,11 @@ defmodule BallsPDS.Util.ACL do
} }
} }
graph = Enum.with_index(read_acls) |> Enum.map(fn {acl, i} -> graph =
make_read_authorization(path, acl, "#read_acl_#{i}") Enum.with_index(read_acls)
end) |> Enum.map(fn {acl, i} ->
make_read_authorization(path, acl, "#read_acl_#{i}")
end)
owner_read = %{ owner_read = %{
"@id" => "#owner-read", "@id" => "#owner-read",

View File

@ -13,7 +13,7 @@ defmodule BallsPDS.Util.Util do
{:trailing_or_no_starting_slash, Regex.match?(@starting_trailing_slashes_regex, path)}, {:trailing_or_no_starting_slash, Regex.match?(@starting_trailing_slashes_regex, path)},
{:sequential_slashes, false} <- {:sequential_slashes, false} <-
{:sequential_slashes, Regex.match?(@sequential_slashes_regex, path)}, {:sequential_slashes, Regex.match?(@sequential_slashes_regex, path)},
{:relative_path, false} <- {:relative_path, Regex.match?(@relative_path_regex, path)} do {:relative_path, false} <- {:relative_path, Regex.match?(@relative_path_regex, path)} do
:ok :ok
else else
{error, _} -> {:error, error} {error, _} -> {:error, error}

View File

@ -138,10 +138,12 @@ defmodule BallsPDS.WAC do
end end
end end
defp extract_key(controller, key = %{}, id) when is_binary(controller) and (is_binary(id) or is_nil(id)), defp extract_key(controller, key = %{}, id)
do: extract_key(controller, [key], id) when is_binary(controller) and (is_binary(id) or is_nil(id)),
do: extract_key(controller, [key], id)
defp extract_key(controller, keys, id) when is_list(keys) and is_binary(controller) and (is_binary(id) or is_nil(id)) do defp extract_key(controller, keys, id)
when is_list(keys) and is_binary(controller) and (is_binary(id) or is_nil(id)) do
Enum.reduce_while(keys, nil, fn Enum.reduce_while(keys, nil, fn
%{"controller" => ^controller, "publicKeyMultibase" => multikey, "id" => key_id}, _ %{"controller" => ^controller, "publicKeyMultibase" => multikey, "id" => key_id}, _
when is_binary(multikey) and is_binary(key_id) -> when is_binary(multikey) and is_binary(key_id) ->
@ -165,6 +167,7 @@ defmodule BallsPDS.WAC do
# Match the entire key id or else check if it's just a fragment. # Match the entire key id or else check if it's just a fragment.
defp match_id?(key_id, test_id) when is_binary(key_id) and is_binary(test_id), defp match_id?(key_id, test_id) when is_binary(key_id) and is_binary(test_id),
do: key_id == test_id || String.ends_with?(key_id, "#" <> test_id) do: key_id == test_id || String.ends_with?(key_id, "#" <> test_id)
# If no test id is passed then match whatever was passed. # If no test id is passed then match whatever was passed.
defp match_id?(key_id, nil) when is_binary(key_id), do: true defp match_id?(key_id, nil) when is_binary(key_id), do: true
end end