-
Notifications
You must be signed in to change notification settings - Fork 31
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 1 commit
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 | ||
---|---|---|---|---|
|
@@ -4,6 +4,7 @@ defmodule TransportWeb.SessionController do | |||
""" | ||||
use TransportWeb, :controller | ||||
alias Datagouvfr.Authentication | ||||
import Ecto.Query | ||||
require Logger | ||||
|
||||
def new(conn, _) do | ||||
|
@@ -127,24 +128,45 @@ defmodule TransportWeb.SessionController do | |||
end | ||||
|
||||
def save_current_user(%Plug.Conn{} = conn, %{} = user_params) do | ||||
conn |> put_session(:current_user, user_params |> user_params_for_session()) | ||||
conn |> put_session(:current_user, user_params_for_session(user_params)) | ||||
end | ||||
|
||||
thbar marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
def user_params_for_session(%{} = params) do | ||||
params | ||||
# Remove the list of `organizations` from the final map: it's already stored in the database | ||||
# and maintained up-to-date by `Transport.Jobs.UpdateContactsJob` | ||||
# and it can be too big to be stored in a cookie | ||||
|> Map.delete("organizations") | ||||
# - `is_admin` is needed to check permissions | ||||
# - `is_producer` is used to get access to the "Espace producteur" | ||||
|> Map.merge(%{"is_producer" => is_producer?(params), "is_admin" => is_admin?(params)}) | ||||
AntoineAugusti marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
end | ||||
|
||||
@doc """ | ||||
iex> pan_org = %{"slug" => "equipe-transport-data-gouv-fr", "name" => "PAN"} | ||||
iex> other_org = %{"slug" => "foo-inc", "name" => "Foo Inc"} | ||||
iex> user_params_for_session(%{"foo" => "bar", "organizations" => [pan_org, other_org]}) | ||||
%{"foo" => "bar", "organizations" => [pan_org]} | ||||
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, we can refresh this field more often in the future. | ||||
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. La durée de notre session est de 15 jours
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. Mis à jour dans l'Espace Producteur aussi b24229e |
||||
""" | ||||
def user_params_for_session(%{} = params) do | ||||
Map.put( | ||||
params, | ||||
"organizations", | ||||
Enum.filter( | ||||
params["organizations"], | ||||
&match?(%{"slug" => "equipe-transport-data-gouv-fr"}, &1) | ||||
) | ||||
) | ||||
def is_producer?(%{"organizations" => orgs}) do | ||||
AntoineAugusti marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
org_ids = Enum.map(orgs, & &1["id"]) | ||||
|
||||
DB.Dataset.base_query() |> where([dataset: d], d.organization_id in ^org_ids) |> DB.Repo.exists?() | ||||
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. | ||||
|
||||
AntoineAugusti marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
iex> is_admin?(%{"organizations" => [%{"slug" => "equipe-transport-data-gouv-fr"}, %{"slug" => "foo"}]}) | ||||
true | ||||
iex> is_admin?(%{"organizations" => [%{"slug" => "foo"}]}) | ||||
false | ||||
iex> is_admin?(%{"organizations" => []}) | ||||
false | ||||
""" | ||||
def is_admin?(%{"organizations" => orgs}) do | ||||
Enum.any?(orgs, &(&1["slug"] == "equipe-transport-data-gouv-fr")) | ||||
end | ||||
|
||||
defp get_redirect_path(%Plug.Conn{} = conn) do | ||||
|
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 | ||
|
||
|
@@ -353,11 +354,9 @@ defmodule TransportWeb.Router do | |
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 | ||
# `current_user` is set by TransportWeb.SessionController.user_params_for_session/1 | ||
def is_transport_data_gouv_member?(%{"is_admin" => true} = _current_user), do: true | ||
def is_transport_data_gouv_member?(_), do: false | ||
|
||
# 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 | ||
|
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 !