diff --git a/elixir/apps/web/lib/web/live/policies/components.ex b/elixir/apps/web/lib/web/live/policies/components.ex index 4a73dd93b..00cfc848b 100644 --- a/elixir/apps/web/lib/web/live/policies/components.ex +++ b/elixir/apps/web/lib/web/live/policies/components.ex @@ -13,6 +13,24 @@ defmodule Web.Policies.Components do {"U", "Sunday"} ] + @all_conditions [ + :remote_ip_location_region, + :remote_ip, + :provider_id, + :client_verified, + :current_utc_datetime + ] + + # current_utc_datetime is a condition evaluated at the time of the request, + # so we don't need to include it in the list of conditions that can be set + # for internet resources, otherwise it would be blocking all the requests. + @conditions_by_resource_type %{ + internet: @all_conditions -- [:current_utc_datetime], + dns: @all_conditions, + ip: @all_conditions, + cidr: @all_conditions + } + attr(:policy, :map, required: true) def policy_name(assigns) do @@ -256,9 +274,13 @@ defmodule Web.Policies.Components do def conditions_form(assigns) do assigns = - assign_new(assigns, :policy_conditions_enabled?, fn -> + assigns + |> assign_new(:policy_conditions_enabled?, fn -> Domain.Accounts.policy_conditions_enabled?(assigns.account) end) + |> assign_new(:enabled_conditions, fn -> + Map.fetch!(@conditions_by_resource_type, assigns.selected_resource.type) + end) ~H"""
<.conditions_form - :if={not is_nil(@selected_resource) and @selected_resource.type != :internet} + :if={not is_nil(@selected_resource)} form={@form} account={@account} timezone={@timezone} providers={@providers} + selected_resource={@selected_resource} /> <.options_form - :if={not is_nil(@selected_resource) and @selected_resource.type == :internet} + :if={not is_nil(@selected_resource)} form={@form} account={@account} - timezone={@timezone} - providers={@providers} + selected_resource={@selected_resource} /> diff --git a/elixir/apps/web/test/web/live/policies/new_test.exs b/elixir/apps/web/test/web/live/policies/new_test.exs index f9cc2bc91..700365521 100644 --- a/elixir/apps/web/test/web/live/policies/new_test.exs +++ b/elixir/apps/web/test/web/live/policies/new_test.exs @@ -158,6 +158,43 @@ defmodule Web.Live.Policies.NewTest do assert Floki.attribute(value_input, "value") == [resource.id] end + test "form changes depending on resource type", %{ + account: account, + identity: identity, + conn: conn + } do + resource = Fixtures.Resources.create_resource(account: account, type: :internet) + + {:ok, lv, _html} = + conn + |> authorize_conn(identity) + |> live(~p"/#{account}/policies/new?resource_id=#{resource.id}") + + form = form(lv, "form") + + assert find_inputs(form) == [ + "policy[actor_group_id]", + "policy[actor_group_id]_name", + "policy[conditions][client_verified][operator]", + "policy[conditions][client_verified][property]", + "policy[conditions][client_verified][values][]", + "policy[conditions][provider_id][operator]", + "policy[conditions][provider_id][property]", + "policy[conditions][provider_id][values][]", + "policy[conditions][remote_ip][operator]", + "policy[conditions][remote_ip][property]", + "policy[conditions][remote_ip][values][]", + "policy[conditions][remote_ip_location_region][operator]", + "policy[conditions][remote_ip_location_region][property]", + "policy[conditions][remote_ip_location_region][values][]", + "policy[description]", + "policy[resource_id]", + "policy[resource_id]_name", + "search_query-policy_actor_group_id", + "search_query-policy_resource_id" + ] + end + test "renders changeset errors on input change", %{ account: account, identity: identity,