From bf21e18385fc87ec00aab6e37920e56a6a6fa629 Mon Sep 17 00:00:00 2001 From: Eric Teubert Date: Thu, 12 Dec 2019 11:27:41 +0100 Subject: [PATCH] fix(api/contribution): audio permission check This logic existed before in a similar way but was removed[1]. I'm not sure why. Also, we had a discussion[2] before on checking permissions on objects that don't have permissions and decided to avoid that and instead pass in the proper parent object into permission checks. However in that discussion the topic was chapters and it was trivial to just pass in the parent instead. Checking the permission on the Audio however is more envolved and I want this logic in _one_ place only. [1]: https://github.com/podlove/radiator/commit/9b878fc7#diff-8fce5c34154f97d93f212e64bd95fee7L61 [2]: https://github.com/podlove/radiator/pull/152#discussion_r300455042 --- lib/radiator/directory/editor/permission.ex | 23 ++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/radiator/directory/editor/permission.ex b/lib/radiator/directory/editor/permission.ex index a98b9f86..27ec8553 100644 --- a/lib/radiator/directory/editor/permission.ex +++ b/lib/radiator/directory/editor/permission.ex @@ -6,7 +6,14 @@ defmodule Radiator.Directory.Editor.Permission do alias Radiator.Repo alias Radiator.Auth alias Radiator.Perm.Permission - alias Radiator.Directory.{Network, Podcast, Episode, AudioPublication} + + alias Radiator.Directory.{ + Audio, + Network, + Podcast, + Episode, + AudioPublication + } alias Radiator.Perm.Ecto.PermissionType @@ -22,6 +29,9 @@ defmodule Radiator.Directory.Editor.Permission do def get_permission(user = %Auth.User{}, subject = %Episode{}), do: do_get_permission(user, subject) + def get_permission(user = %Auth.User{}, subject = %Audio{}), + do: nil + def get_permission(user = %Auth.User{}, subject = %AudioPublication{}), do: do_get_permission(user, subject) @@ -58,6 +68,17 @@ defmodule Radiator.Directory.Editor.Permission do end end + defp parents(subject = %Audio{}) do + audio_publication = + subject + |> Ecto.assoc(:audio_publication) + |> Repo.one() + + episodes = subject |> Ecto.assoc(:episodes) |> Repo.all() + + if audio_publication, do: [audio_publication | episodes], else: episodes + end + defp parents(subject = %AudioPublication{}) do subject |> Ecto.assoc(:network)