Skip to content

Commit

Permalink
Application Config Page Toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
chrislaskey committed Apr 14, 2021
1 parent bbdf640 commit e557f51
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 16 deletions.
1 change: 1 addition & 0 deletions .env.demo
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ ARTEMIS_WEB_ENABLED_AUTH_PROVIDERS=system_user
ARTEMIS_WEB_HOSTNAME=localhost
ARTEMIS_WEB_PORT=4077
ARTEMIS_WEB_LIVE_VIEW_SECRET_KEY=avY1wGQQwgSIDF1v7GXT5p8AmSbUvb32
ARTEMIS_WEB_APPLICATION_CONFIG_PAGE_ENABLED=true

### Ueberauth

Expand Down
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ ARTEMIS_WEB_PORT=4077
# Copy the output below and uncomment env variable:
#
# ARTEMIS_WEB_LIVE_VIEW_SECRET_KEY=
ARTEMIS_WEB_APPLICATION_CONFIG_PAGE_ENABLED=false

### Ueberauth

Expand Down
1 change: 1 addition & 0 deletions .env.travis
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ ARTEMIS_WEB_ENABLED_AUTH_PROVIDERS=system_user
ARTEMIS_WEB_HOSTNAME=localhost
ARTEMIS_WEB_PORT=4077
ARTEMIS_WEB_LIVE_VIEW_SECRET_KEY=KUf9d9YMmWmNw9xnEdSXWDspN+F5+VaX
ARTEMIS_WEB_APPLICATION_CONFIG_PAGE_ENABLED=false

### Ueberauth

Expand Down
3 changes: 2 additions & 1 deletion apps/artemis_web/config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ config :artemis_web, ArtemisWeb.Endpoint,
secret_key_base: System.get_env("ARTEMIS_SECRET_KEY"),
render_errors: [view: ArtemisWeb.ErrorView, accepts: ~w(html json)],
pubsub_server: ArtemisPubSub,
live_view: [signing_salt: System.get_env("ARTEMIS_WEB_LIVE_VIEW_SECRET_KEY")]
live_view: [signing_salt: System.get_env("ARTEMIS_WEB_LIVE_VIEW_SECRET_KEY")],
application_config_page: [enabled: System.get_env("ARTEMIS_WEB_APPLICATION_CONFIG_PAGE_ENABLED")]

config :artemis_web, ArtemisWeb.Guardian,
allowed_algos: ["HS512"],
Expand Down
23 changes: 23 additions & 0 deletions apps/artemis_web/lib/artemis_web.ex
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,29 @@ defmodule ArtemisWeb do
end
end

# Config

defp app_config_enabled?(conn, app_config, render_controller) do
case apply(Artemis.Helpers.AppConfig, :enabled?, app_config) do
true -> render_controller.()
false -> render_not_found(conn)
end
end

defp app_config_enabled_any?(conn, app_configs, render_controller) do
case Enum.any?(app_configs, &apply(Artemis.Helpers.AppConfig, :enabled?, &1)) do
true -> render_controller.()
false -> render_not_found(conn)
end
end

defp app_config_enabled_all?(conn, app_configs, render_controller) do
case Enum.all?(app_configs, &apply(Artemis.Helpers.AppConfig, :enabled?, &1)) do
true -> render_controller.()
false -> render_not_found(conn)
end
end

# Features

defp feature_active?(conn, feature, render_controller) do
Expand Down
5 changes: 4 additions & 1 deletion apps/artemis_web/lib/artemis_web/config/navigation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ defmodule ArtemisWeb.Config.Navigation do
[
label: "Application Configs",
path: &Routes.application_config_path(&1, :index),
verify: &has?(&1, "application-configs:list")
verify: fn current_user ->
has?(current_user, "application-configs:list") &&
Artemis.Helpers.AppConfig.enabled?(:artemis_web, ArtemisWeb.Endpoint, :application_config_page)
end
],
[
label: "System Tasks",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule ArtemisWeb.ApplicationConfigController do
use ArtemisWeb, :controller

@allowed_application_configs [
@allowed_applications [
:artemis,
:artemis_api,
:artemis_log,
Expand All @@ -11,34 +11,50 @@ defmodule ArtemisWeb.ApplicationConfigController do

def index(conn, _params) do
authorize(conn, "application-configs:list", fn ->
assigns = [
application_configs: @allowed_application_configs
]
app_config_enabled?(conn, [:artemis_web, ArtemisWeb.Endpoint, :application_config_page], fn ->
assigns = [
application_configs: @allowed_applications
]

render(conn, "index.html", assigns)
render(conn, "index.html", assigns)
end)
end)
end

def show(conn, %{"id" => id}) do
authorize(conn, "application-configs:show", fn ->
assigns = [
application_config: get_application_config(id),
application_id: id
]

render(conn, "show.html", assigns)
app_config_enabled?(conn, [:artemis_web, ArtemisWeb.Endpoint, :application_config_page], fn ->
assigns = [
application_config: get_application_config(id),
application_id: id,
application_spec: get_application_spec(id),
applications_started: Application.started_applications()
]

render(conn, "show.html", assigns)
end)
end)
end

# Helpers

defp get_application_config(id) do
application = String.to_existing_atom(id)
allowed? = Enum.member?(@allowed_application_configs, application)
allowed? = Enum.member?(@allowed_applications, application)

case allowed? do
true -> Application.get_all_env(application)
false -> {:error, "Invalid Application"}
end
end

defp get_application_spec(id) do
application = String.to_existing_atom(id)
allowed? = Enum.member?(@allowed_applications, application)

case allowed? do
true -> Application.spec(application)
false -> {:error, "Invalid Application"}
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,17 @@
<%= render_flash_notifications @conn %>

<section>
<%= h3 "Raw Data" %>
<pre><code><%= inspect @application_config, pretty: true %></code></pre>
<%= h3 "Application Config" %>
<pre><code><%= inspect @application_config, limit: :infinity, pretty: true, printable_limit: :infinity %></code></pre>
</section>

<section>
<%= h3 "Application Spec" %>
<pre><code><%= inspect @application_spec, limit: :infinity, pretty: true, printable_limit: :infinity %></code></pre>
</section>

<section>
<%= h3 "Applications Started" %>
<pre><code><%= inspect @applications_started, limit: :infinity, pretty: true, printable_limit: :infinity %></code></pre>
</section>
</div>

0 comments on commit e557f51

Please sign in to comment.