Skip to content

Commit

Permalink
Redirect unpublished signs to login if not editor
Browse files Browse the repository at this point in the history
  • Loading branch information
r-tae committed Aug 29, 2024
1 parent ad8ca1f commit a2828f4
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 53 deletions.
62 changes: 37 additions & 25 deletions lib/signbank/dictionary.ex
Original file line number Diff line number Diff line change
Expand Up @@ -64,27 +64,36 @@ defmodule Signbank.Dictionary do
## Examples
iex> get_sign_by_id_gloss!("house1a")
iex> get_sign_by_id_gloss("house1a")
%Sign{}
"""
def get_sign_by_id_gloss!(id_gloss),
do:
Repo.get_by!(
from(s in Sign,
preload: [
citation: [definitions: [], variants: []],
definitions: [],
variants: [videos: [], regions: []],
regions: [],
videos: [],
suggested_signs: [],
active_video: []
]
),
id_gloss: id_gloss
def get_sign_by_id_gloss(id_gloss, current_user \\ nil) do
query =
from(s in Sign,
preload: [
citation: [definitions: [], variants: []],
definitions: [],
variants: [videos: [], regions: []],
regions: [],
videos: [],
suggested_signs: [],
active_video: []
],
where: s.id_gloss == ^id_gloss
)

Repo.one(
case current_user do
%User{role: role} when role in [:tech, :editor] ->
query

_ ->
from s in query, where: s.published == true
end
)
end

@doc """
Returns a sign with the given `id_gloss`. It only returns citation entries.
Expand Down Expand Up @@ -198,15 +207,18 @@ defmodule Signbank.Dictionary do
end
)

Repo.one!(
from so in subquery(query),
left_join: p in Sign,
on: [id: so.previous],
left_join: n in Sign,
on: [id: so.next],
select: %{previous: p, next: n, position: so.position},
where: so.id == ^id
)
case Repo.one(
from so in subquery(query),
left_join: p in Sign,
on: [id: so.previous],
left_join: n in Sign,
on: [id: so.next],
select: %{previous: p, next: n, position: so.position},
where: so.id == ^id
) do
nil -> %{previous: nil, next: nil, position: nil}
record -> record
end
end

@doc """
Expand Down
7 changes: 5 additions & 2 deletions lib/signbank/sign_order.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@ defmodule Signbank.SignOrder do
select: %{
id: selected_as(s.id, :id),
position:
selected_as(row_number()
|> over(:sign_order), :position),
selected_as(
row_number()
|> over(:sign_order),
:position
),
previous: selected_as(lag(s.id) |> over(:sign_order), :previous),
next: selected_as(lead(s.id) |> over(:sign_order), :next)
}
Expand Down
46 changes: 26 additions & 20 deletions lib/signbank_web/live/entry_live/basic_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,32 @@ defmodule SignbankWeb.SignLive.BasicView do
def handle_params(%{"id" => id_gloss} = params, _, socket) do
search_query = Map.get(params, "q")

socket =
assign(
socket,
:search_results,
if is_nil(search_query) do
[]
else
{:ok, search_results} = Dictionary.get_sign_by_keyword!(search_query)
search_results
end
)

# TODO: this is really quite broken, it doesn't take into account the logged in user
sign = Dictionary.get_sign_by_id_gloss!(id_gloss)

{:noreply,
socket
|> assign(:page_title, page_title(socket.assigns.live_action))
|> assign(:sign, sign)
|> assign(:search_query, search_query)}
case Dictionary.get_sign_by_id_gloss(id_gloss, socket.assigns.current_user) do
nil ->
{:noreply,
socket
|> put_flash(:error, "You do not have permission to access this page, please log in.")
|> redirect(to: ~p"/users/log_in")}

sign ->
socket =
assign(
socket,
:search_results,
if is_nil(search_query) do
[]
else
{:ok, search_results} = Dictionary.get_sign_by_keyword!(search_query)
search_results
end
)

{:noreply,
socket
|> assign(:page_title, page_title(socket.assigns.live_action))
|> assign(:sign, sign)
|> assign(:search_query, search_query)}
end
end

# TODO: fix the page title
Expand Down
2 changes: 1 addition & 1 deletion lib/signbank_web/live/entry_live/basic_view.html.heex
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<nav class="level entry-page__switch_entry">
<.entry_nav class="level-left" sign={@sign} current_user={@current_user}/>
<.entry_nav class="level-left" sign={@sign} current_user={@current_user} />

Check warning on line 2 in lib/signbank_web/live/entry_live/basic_view.html.heex

View workflow job for this annotation

GitHub Actions / Build release

undefined attribute "current_user" for component SignbankWeb.CoreComponents.entry_nav/1

<div class="level-right">
<%!-- <p :if={@search_query != nil and not Enum.empty?(@search_results)}> --%>
Expand Down
17 changes: 12 additions & 5 deletions lib/signbank_web/live/entry_live/linguistic_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,19 @@ defmodule SignbankWeb.SignLive.LinguisticView do

@impl true
def handle_params(%{"id" => id_gloss}, _, socket) do
sign = Dictionary.get_sign_by_id_gloss!(id_gloss)
case Dictionary.get_sign_by_id_gloss(id_gloss, socket.assigns.current_user) do
nil ->
{:noreply,
socket
|> put_flash(:error, "You do not have permission to access this page, please log in.")
|> redirect(to: ~p"/users/log_in")}

{:noreply,
socket
|> assign(:page_title, page_title(socket.assigns.live_action))
|> assign(:sign, sign)}
sign ->
{:noreply,
socket
|> assign(:page_title, page_title(socket.assigns.live_action))
|> assign(:sign, sign)}
end
end

# TODO: fix the page title
Expand Down

0 comments on commit a2828f4

Please sign in to comment.