-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ajoute un lien vers l'Espace Producteur dans le menu quand pertinent #3684
Changes from all commits
1b1a89c
03aabea
4e0282e
ffdf43c
0afa9a4
b24229e
5ebe450
55c4636
d7e0ff5
f27ea6f
dba94ae
4d8995f
bfff9d9
804ac4c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Les changements importants dans la session sont dans ce fichier. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -314,6 +314,7 @@ defmodule TransportWeb.Router do | |
end | ||
|
||
defp assign_current_user(conn, _) do | ||
# `current_user` is set by TransportWeb.SessionController.user_params_for_session/1 | ||
assign(conn, :current_user, get_session(conn, :current_user)) | ||
end | ||
|
||
|
@@ -352,13 +353,6 @@ defmodule TransportWeb.Router do | |
end | ||
end | ||
|
||
# NOTE: method visibility set to public because we need to call the same logic from LiveView | ||
def is_transport_data_gouv_member?(current_user) do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ce changement impose aux membres de notre équipe de se déconnecter/reconnecter étant donné qu'on n'a pas l'attribut Pas d'impact pour les autres. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Amélioration sécurité à prévoir en lien avec: En cas de vol de cookie, si je comprends bien (avant la PR ou après la PR d'ailleurs), le rôle restera encodé dans le cookie. Il faudrait avoir un rafraîchissement systématique (ou plus frais en tout cas, ex: cache avec TTL si on veut éviter un appel d'API à chaque tour) sinon on se crée des problèmes (la suppression d'un compte sur data gouv ne protègera pas d'un vol de cookie admin). |
||
current_user | ||
|> Map.get("organizations", []) | ||
|> Enum.any?(fn org -> org["slug"] == "equipe-transport-data-gouv-fr" end) | ||
end | ||
|
||
# Check that a secret key is passed in the URL in the `export_key` query parameter | ||
defp check_export_secret_key(%Plug.Conn{params: params} = conn, _) do | ||
export_key_value = Map.get(params, "export_key", "") | ||
|
@@ -375,7 +369,7 @@ defmodule TransportWeb.Router do | |
end | ||
|
||
defp transport_data_gouv_member(%Plug.Conn{} = conn, _) do | ||
if is_transport_data_gouv_member?(conn.assigns[:current_user]) do | ||
if TransportWeb.Session.is_admin?(conn) do | ||
conn | ||
else | ||
conn | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
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, [%DB.Dataset{}] = _datasets_for_user) do | ||
set_session_attribute_attribute(conn, @is_producer_key_name, true) | ||
end | ||
|
||
def set_is_producer(%Plug.Conn{} = conn, [] = _datasets_for_user) do | ||
set_session_attribute_attribute(conn, @is_producer_key_name, false) | ||
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Supprime cette méthode elle faisait doublon avec ce qui était dans
TransportWeb.Router
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok vu:
(doublon après l'ajout par la PR)
Merci pour l'explication !