Skip to content

Commit

Permalink
fix(api/contribution): audio permission check
Browse files Browse the repository at this point in the history
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]: 9b878fc7#diff-8fce5c34154f97d93f212e64bd95fee7L61
[2]: #152 (comment)
  • Loading branch information
eteubert committed Dec 12, 2019
1 parent 502fe98 commit bf21e18
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion lib/radiator/directory/editor/permission.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit bf21e18

Please sign in to comment.