Skip to content

Commit

Permalink
Merge branch 'main' into feat/Add-Env-Secrets-in-Server-API
Browse files Browse the repository at this point in the history
  • Loading branch information
jonas-martinez authored May 29, 2024
2 parents 4790525 + 72d8104 commit ce46d0e
Show file tree
Hide file tree
Showing 12 changed files with 1,183 additions and 147 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,20 +147,29 @@ defmodule IdentityWeb.UserAuthController do

case Accounts.get_user(response.body["subject"]) do
# In case the user from the token is nil, the token is invalid
nil -> {:error, {:invalid_token, "The token is invalid.", 403}}
res -> redirect_next_step(conn, res, login_challenge, true)
nil ->
conn
|> redirect_to_register(login_challenge)

res ->
redirect_next_step(conn, res, login_challenge, true)
end
else
# client = response.body["client"]
conn
|> delete_session(:user_id)
|> delete_session(:remember)
|> delete_session(:email)
|> put_session(:login_challenge, login_challenge)
|> redirect(to: Routes.user_auth_path(conn, :register_page))
|> redirect_to_register(login_challenge)
end
end

defp redirect_to_register(conn, login_challenge) do
conn
|> delete_session(:user_id)
|> delete_session(:remember)
|> delete_session(:email)
|> put_session(:login_challenge, login_challenge)
|> redirect(to: Routes.user_auth_path(conn, :register_page))
end

def new(conn, _params),
do: redirect(conn, to: Routes.user_auth_path(conn, :register_page))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ defmodule ApplicationRunner.Environment.ManifestHandler do
{:ok, GenServer.call(get_full_name(env_id), {:get_routes, exposer})}
end

defp get_routes(env_id, exposer) do
defp get_routes(_env_id, exposer) do
{:error, "Exposer #{exposer} not supported"}
end

Expand Down
4 changes: 2 additions & 2 deletions libs/application_runner/lib/session/route_server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ defmodule ApplicationRunner.Session.RouteServer do
query_params
|> Map.merge(%{
"me" => mongo_user_id,
"roles" => session_metadata.user_id
"roles" => session_metadata.roles
})

query_transformed = Parser.replace_params(query, params)
Expand All @@ -221,7 +221,7 @@ defmodule ApplicationRunner.Session.RouteServer do
context
|> Map.merge(%{
"me" => mongo_user_id,
"roles" => session_metadata.user_id,
"roles" => session_metadata.roles,
"pathParams" => query_params["route"]
})
|> project_map(context_projection)
Expand Down
102 changes: 75 additions & 27 deletions libs/application_runner/lib/session/ui_builders/json_builder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,54 +5,102 @@ defmodule ApplicationRunner.Session.UiBuilders.JsonBuilder do
@behaviour ApplicationRunner.Session.UiBuilders.UiBuilderAdapter

alias ApplicationRunner.Environment
alias ApplicationRunner.Session.RouteServer
alias ApplicationRunner.Session.UiBuilders.UiBuilderAdapter

require Logger

@type view :: map()
@type component :: map()

@impl ApplicationRunner.Session.UiBuilders.UiBuilderAdapter
@impl true
def get_routes(env_id, roles) do
Environment.ManifestHandler.get_routes(env_id, "json", roles)
end

@impl ApplicationRunner.Session.UiBuilders.UiBuilderAdapter
@impl true
def build_ui(session_metadata, view_uid) do
Logger.debug("#{__MODULE__} build_ui with session_metadata: #{inspect(session_metadata)}")
UiBuilderAdapter.build_ui(__MODULE__, session_metadata, view_uid)
end

@impl true
def build_components(
session_metadata,
%{"_type" => comp_type} = component,
ui_context,
view_uid
) do
Logger.debug("#{__MODULE__} build_components with component: #{inspect(component)}")

# TODO: validate component ?
case comp_type do
"view" ->
UiBuilderAdapter.handle_view(__MODULE__, session_metadata, component, ui_context, view_uid)

"listener" ->
UiBuilderAdapter.handle_listener(__MODULE__, session_metadata, component, ui_context, view_uid)

with {:ok, json} <- RouteServer.fetch_view(session_metadata, view_uid) do
build_listeners(session_metadata, json)
_ ->
Logger.warn("Unknown component type for JSON view: #{comp_type}")
{:ok, component, ui_context}
end
end

def build_listeners(session_metadata, view) do
{:ok, do_build_listeners(session_metadata, view)}
rescue
err -> {:error, err}
end
def build_components(
session_metadata,
component,
ui_context,
view_uid
)
when is_map(component) do
{new_context, new_component} =
Enum.reduce(
component,
{ui_context, %{}},
fn {k, v}, {context, acc} ->
case build_components(session_metadata, v, context, view_uid) do
{:ok, new_sub_component, new_ui_context} ->
{new_ui_context, Map.put(acc, k, new_sub_component)}

_ ->
{context, Map.put(acc, k, v)}
end
end
)

defp do_build_listeners(session_metadata, list) when is_list(list) do
Enum.map(list, &do_build_listeners(session_metadata, &1))
{:ok, new_component, new_context}
end

defp do_build_listeners(session_metadata, %{"_type" => "listener"} = listener) do
case RouteServer.build_listener(session_metadata, listener) do
{:ok, built_listener} ->
built_listener
def build_components(
session_metadata,
component,
ui_context,
view_uid
)
when is_list(component) do
{new_context, new_component} =
Enum.reduce(
component,
{ui_context, []},
fn v, {context, acc} ->
case build_components(session_metadata, v, context, view_uid) do
{:ok, new_sub_component, new_ui_context} ->
{new_ui_context, [new_sub_component | acc]}

err ->
raise err
end
end
_ ->
{context, [v | acc]}
end
end
)

defp do_build_listeners(session_metadata, map) when is_map(map) do
map
|> Enum.map(fn {k, v} -> {k, do_build_listeners(session_metadata, v)} end)
|> Map.new()
{:ok, Enum.reverse(new_component), new_context}
end

defp do_build_listeners(_session_metadata, e) do
e
def build_components(
_session_metadata,
component,
ui_context,
_view_uid
) do
{:ok, component, ui_context}
end
end
127 changes: 21 additions & 106 deletions libs/application_runner/lib/session/ui_builders/lenra_builder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,78 +15,33 @@ defmodule ApplicationRunner.Session.UiBuilders.LenraBuilder do
@type view :: map()
@type component :: map()

@impl true
def get_routes(env_id, roles) do
Environment.ManifestHandler.get_routes(env_id, "lenra", roles)
end

@impl true
def build_ui(session_metadata, view_uid) do
Logger.debug("#{__MODULE__} build_ui with session_metadata: #{inspect(session_metadata)}")

with {:ok, ui_context} <- get_and_build_view(session_metadata, Ui.Context.new(), view_uid) do
{:ok,
transform_ui(%{
"rootView" => view_id(view_uid),
"views" => ui_context.views_map
})}
end
end

defp transform_ui(%{"rootView" => root_views, "views" => views}) do
transform(%{"root" => Map.fetch!(views, root_views)}, views)
end

defp transform(%{"type" => "view", "id" => id}, views) do
transform(Map.fetch!(views, id), views)
end

defp transform(view, views) when is_map(view) do
Enum.map(view, fn
{k, v} -> {k, transform(v, views)}
end)
|> Map.new()
end

defp transform(view, views) when is_list(view) do
Enum.map(view, &transform(&1, views))
end

defp transform(view, _views) do
view
end

@spec get_and_build_view(Session.Metadata.t(), Ui.Context.t(), ViewUid.t()) ::
{:ok, Ui.Context.t()} | {:error, UiBuilderAdapter.common_error()}
defp get_and_build_view(
%Session.Metadata{} = session_metadata,
%Ui.Context{} = ui_context,
%ViewUid{} = view_uid
) do
with {:ok, view} <- RouteServer.fetch_view(session_metadata, view_uid),
{:ok, component, new_app_context} <-
build_component(session_metadata, view, ui_context, view_uid) do
str_view_id = view_id(view_uid)
{:ok, put_in(new_app_context.views_map[str_view_id], component)}
with {:ok, ui} <- UiBuilderAdapter.build_ui(__MODULE__, session_metadata, view_uid) do
{:ok, %{"root" => ui}}
end
end

# Build a component.
# If the component type is "view" this is considered a view and will be handled like one.
# Everything else will be handled as a simple component.
@spec build_component(Session.Metadata.t(), view(), Ui.Context.t(), ViewUid.t()) ::
{:ok, component(), Ui.Context.t()} | {:error, UiBuilderAdapter.common_error()}
defp build_component(
session_metadata,
%{"_type" => comp_type} = component,
ui_context,
view_uid
) do
# Build the view result components.
@impl true
def build_components(
session_metadata,
%{"_type" => comp_type} = component,
ui_context,
view_uid
) do
Logger.debug("#{__MODULE__} build_component with component: #{inspect(component)}")

with schema_path <- JsonSchemata.get_component_path(comp_type),
{:ok, validation_data} <- validate_with_error(schema_path, component, view_uid) do
case comp_type do
"view" ->
handle_view(session_metadata, component, ui_context, view_uid)
UiBuilderAdapter.handle_view(__MODULE__, session_metadata, component, ui_context, view_uid)

_ ->
handle_component(
Expand All @@ -100,55 +55,19 @@ defmodule ApplicationRunner.Session.UiBuilders.LenraBuilder do
end
end

defp build_component(
_session_metadata,
component,
_ui_context,
view_uid
) do
def build_components(
_session_metadata,
component,
_ui_context,
view_uid
) do
ApplicationRunner.Errors.BusinessError.components_malformated_tuple(%{
view: view_uid.name,
at: view_uid.prefix_path,
receive: component
})
end

# Build a view means :
# - getting the name and props, coll and query of the view
# - create the ID of the view with name/data/props
# - Create a new viewContext corresponding to the view
# - Recursively get_and_build_view.
@spec handle_view(Session.Metadata.t(), view(), Ui.Context.t(), ViewUid.t()) ::
{:ok, component(), Ui.Context.t()} | {:error, UiBuilderAdapter.common_error()}
defp handle_view(session_metadata, component, ui_context, view_uid) do
name = Map.get(component, "name")
props = Map.get(component, "props")
find = Map.get(component, "find", %{})
context_projection = Map.get(component, "context")

{coll, query, projection} = RouteServer.extract_find(component, find)

with {:ok, new_view_uid} <-
RouteServer.create_view_uid(
session_metadata,
name,
%{coll: coll, query: query, projection: projection},
%{},
props,
view_uid.context,
context_projection,
view_uid.prefix_path
),
{:ok, new_app_context} <-
get_and_build_view(session_metadata, ui_context, new_view_uid) do
{
:ok,
%{"type" => "view", "id" => view_id(new_view_uid), "name" => name},
new_app_context
}
end
end

# Build a components means to :
# - Recursively build all children (list of child) properties
# - Recursively build all single child properties
Expand Down Expand Up @@ -287,7 +206,7 @@ defmodule ApplicationRunner.Session.UiBuilders.LenraBuilder do
ui_context,
view_uid
) do
case build_component(
case build_components(
session_metadata,
child_comp,
ui_context,
Expand Down Expand Up @@ -401,7 +320,7 @@ defmodule ApplicationRunner.Session.UiBuilders.LenraBuilder do
|> Parallel.map(fn {child, index} ->
children_path = "#{prefix_path}/#{index}"

build_component(
build_components(
session_metadata,
child,
ui_context,
Expand Down Expand Up @@ -459,8 +378,4 @@ defmodule ApplicationRunner.Session.UiBuilders.LenraBuilder do
end
)
end

defp view_id(%ViewUid{} = view_uid) do
Crypto.hash({view_uid.name, view_uid.coll, view_uid.query_parsed, view_uid.props})
end
end
Loading

0 comments on commit ce46d0e

Please sign in to comment.