-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'search-spike' of github.com:etalab/transport-site into …
…search-spike
- Loading branch information
Showing
57 changed files
with
409 additions
and
218 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
defmodule TransportWeb.Session do | ||
@moduledoc """ | ||
Web session getters and setters. | ||
""" | ||
import Ecto.Query | ||
import Plug.Conn | ||
|
||
@is_admin_key_name "is_admin" | ||
@is_producer_key_name "is_producer" | ||
|
||
@doc """ | ||
Are you a data producer? | ||
You're a data producer if you're a member of an organization with an active dataset | ||
on transport.data.gouv.fr. | ||
This is set when you log in and refreshed when you visit your "Espace producteur". | ||
""" | ||
@spec set_is_producer(Plug.Conn.t(), map() | [DB.Dataset.t()]) :: Plug.Conn.t() | ||
def set_is_producer(%Plug.Conn{} = conn, %{"organizations" => _} = params) do | ||
set_session_attribute_attribute(conn, @is_producer_key_name, is_producer?(params)) | ||
end | ||
|
||
def set_is_producer(%Plug.Conn{} = conn, datasets_for_user) when is_list(datasets_for_user) do | ||
is_producer = not Enum.empty?(datasets_for_user) | ||
set_session_attribute_attribute(conn, @is_producer_key_name, is_producer) | ||
end | ||
|
||
@doc """ | ||
Are you a transport.data.gouv.fr admin? | ||
You're an admin if you're a member of the PAN organization on data.gouv.fr. | ||
""" | ||
def set_is_admin(%Plug.Conn{} = conn, %{"organizations" => _} = params) do | ||
set_session_attribute_attribute(conn, @is_admin_key_name, is_admin?(params)) | ||
end | ||
|
||
def is_admin?(%{"organizations" => orgs}) do | ||
Enum.any?(orgs, &(&1["slug"] == "equipe-transport-data-gouv-fr")) | ||
end | ||
|
||
def is_admin?(%Plug.Conn{} = conn) do | ||
conn |> current_user() |> Map.get(@is_admin_key_name, false) | ||
end | ||
|
||
def is_admin?(%Phoenix.LiveView.Socket{assigns: %{current_user: current_user}}) do | ||
Map.get(current_user, @is_admin_key_name, false) | ||
end | ||
|
||
def is_producer?(%Plug.Conn{} = conn) do | ||
conn |> current_user() |> Map.get(@is_producer_key_name, false) | ||
end | ||
|
||
def is_producer?(%{"organizations" => orgs}) do | ||
org_ids = Enum.map(orgs, & &1["id"]) | ||
DB.Dataset.base_query() |> where([dataset: d], d.organization_id in ^org_ids) |> DB.Repo.exists?() | ||
end | ||
|
||
@spec set_session_attribute_attribute(Plug.Conn.t(), binary(), boolean()) :: Plug.Conn.t() | ||
defp set_session_attribute_attribute(%Plug.Conn{} = conn, key, value) do | ||
current_user = current_user(conn) | ||
conn |> put_session(:current_user, Map.put(current_user, key, value)) | ||
end | ||
|
||
defp current_user(%Plug.Conn{} = conn), do: get_session(conn, :current_user, %{}) | ||
end |
2 changes: 1 addition & 1 deletion
2
apps/transport/lib/transport_web/templates/dataset/_dataset_scores_chart.html.heex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.