-
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.
- Loading branch information
1 parent
4d8995f
commit bfff9d9
Showing
13 changed files
with
130 additions
and
81 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
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 |
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
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,42 @@ | ||
defmodule TransportWeb.SessionTest do | ||
use ExUnit.Case, async: true | ||
import DB.Factory | ||
import TransportWeb.Session | ||
doctest TransportWeb.Session, import: true | ||
|
||
@pan_org_id Ecto.UUID.generate() | ||
|
||
setup do | ||
:ok = Ecto.Adapters.SQL.Sandbox.checkout(DB.Repo) | ||
end | ||
|
||
test "is_producer?" do | ||
refute is_producer?(%{"organizations" => [pan_org()]}) | ||
insert(:dataset, organization_id: @pan_org_id) | ||
# You're a producer if you're a member of an org with an active dataset | ||
assert is_producer?(%{"organizations" => [pan_org()]}) | ||
end | ||
|
||
test "is_admin?" do | ||
refute is_admin?(%{"organizations" => []}) | ||
refute is_admin?(%{"organizations" => [%{"slug" => "foo"}]}) | ||
assert is_admin?(%{"organizations" => [pan_org()]}) | ||
end | ||
|
||
describe "reader" do | ||
test "is_admin?" do | ||
refute is_admin?(Plug.Test.init_test_session(%Plug.Conn{}, %{})) | ||
assert is_admin?(Plug.Test.init_test_session(%Plug.Conn{}, %{current_user: %{"is_admin" => true}})) | ||
assert is_admin?(%{"is_admin" => true}) | ||
end | ||
|
||
test "is_producer?" do | ||
assert is_producer?(Plug.Test.init_test_session(%Plug.Conn{}, %{current_user: %{"is_producer" => true}})) | ||
refute is_producer?(Plug.Test.init_test_session(%Plug.Conn{}, %{})) | ||
end | ||
end | ||
|
||
def pan_org do | ||
%{"slug" => "equipe-transport-data-gouv-fr", "name" => "PAN", "id" => @pan_org_id} | ||
end | ||
end |