Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.
TypeScript analyticsanalytics-dashboardclickhouseelixirgoogle-analyticsmarketingopen-source-analyticsphoenixplausible-analyticspostgresqlprivacyprivacy-friendlyself-hostedsimple-analyticsstatisticstailwindcssweb-analyticswebsite-analyticswebsite-statswebsite-tracking
Handle team policy update on empty default struct gracefully (#5671)
Adrian Gruntkowski0c9d42daf37d
diff --git a/extra/lib/plausible/auth/sso.ex b/extra/lib/plausible/auth/sso.exindex 6c37379ce5..a92e3e97d8 100644--- a/extra/lib/plausible/auth/sso.ex+++ b/extra/lib/plausible/auth/sso.ex@@ -120,12 +120,11 @@ defmodule Plausible.Auth.SSO do{:ok, Teams.Team.t()} | {:error, Ecto.Changeset.t()}def update_policy(team, attrs \\ []) doparams = Map.new(attrs)- policy_changeset = Teams.Policy.update_changeset(team.policy, params)changeset =team- |> Ecto.Changeset.change()- |> Ecto.Changeset.put_embed(:policy, policy_changeset)+ |> Ecto.Changeset.cast(%{policy: params}, [])+ |> Ecto.Changeset.cast_embed(:policy, with: &Teams.Policy.update_changeset/2)case Repo.update_with_audit(changeset, "sso_policy_updated", %{team_id: team.id}) do{:ok, integration} -> {:ok, integration}@@ -143,11 +142,13 @@ defmodule Plausible.Auth.SSO do| :no_sso_user}def set_force_sso(team, mode) dowith :ok <- check_force_sso(team, mode) do- policy_changeset = Teams.Policy.force_sso_changeset(team.policy, mode)+ params = %{policy: %{force_sso: mode}}team- |> Ecto.Changeset.change()- |> Ecto.Changeset.put_embed(:policy, policy_changeset)+ |> Ecto.Changeset.cast(params, [])+ |> Ecto.Changeset.cast_embed(:policy,+ with: &Teams.Policy.force_sso_changeset(&1, &2.force_sso)+ )|> Repo.update_with_audit("sso_force_mode_changed", %{team_id: team.id})endenddiff --git a/lib/plausible/teams/policy.ex b/lib/plausible/teams/policy.exindex f26b2e25d2..e726e7bfa0 100644--- a/lib/plausible/teams/policy.ex+++ b/lib/plausible/teams/policy.ex@@ -28,6 +28,7 @@ defmodule Plausible.Teams.Policy doonly: [:force_sso, :sso_default_role, :sso_session_timeout_minutes]}end+ @primary_key falseembedded_schema do# SSO options apply to all team's integrations, should there# ever be more than one allowed at once.diff --git a/test/plausible/auth/sso_test.exs b/test/plausible/auth/sso_test.exsindex b4c6ca9fbf..d2fc341295 100644--- a/test/plausible/auth/sso_test.exs+++ b/test/plausible/auth/sso_test.exs@@ -627,6 +627,34 @@ defmodule Plausible.Auth.SSOTest doassert team.policy.sso_session_timeout_minutes == 360end+ test "handles empty policy default gracefully" do+ team = new_site().team++ Repo.update_all(+ from(t in Teams.Team,+ where: t.id == ^team.id,+ update: [set: [policy: fragment("'{}'::json")]]+ ),+ []+ )++ team = Repo.reload!(team)++ assert {:ok, team} = SSO.update_policy(team, sso_default_role: "editor")++ assert team.policy.sso_default_role == :editor+ assert team.policy.sso_session_timeout_minutes == 360++ team = Repo.reload!(team)++ assert {:ok, team} = SSO.update_policy(team, sso_session_timeout_minutes: 200)++ team = Repo.reload!(team)++ assert team.policy.sso_default_role == :editor+ assert team.policy.sso_session_timeout_minutes == 200+ end+test "returns changeset on invalid input" doteam = new_site().team@@ -752,7 +780,7 @@ defmodule Plausible.Auth.SSOTest doendend- describe "set_enforce_sso/2" do+ describe "set_force_sso/2" dotest "sets enforce mode to all_but_owners when conditions met" doowner = new_user(){:ok, owner, _} = Auth.TOTP.initiate(owner)@@ -779,6 +807,50 @@ defmodule Plausible.Auth.SSOTest doassert audited_entry("sso_force_mode_changed", team_id: team.id, entity_id: "#{team.id}")end+ test "handles empty policy default gracefully" do+ owner = new_user()+ {:ok, owner, _} = Auth.TOTP.initiate(owner)+ {:ok, owner, _} = Auth.TOTP.enable(owner, :skip_verify)+ team = new_site(owner: owner).team++ Repo.update_all(+ from(t in Teams.Team,+ where: t.id == ^team.id,+ update: [set: [policy: fragment("'{}'::json")]]+ ),+ []+ )++ team = Repo.reload!(team)++ # Setup integration+ integration = SSO.initiate_saml_integration(team)+ domain = "example-#{Enum.random(1..10_000)}.com"++ {:ok, sso_domain} = SSO.Domains.add(integration, domain)+ _sso_domain = SSO.Domains.verify(sso_domain, skip_checks?: true)++ # Provisioned SSO identity+ #+ identity = new_identity("Lance Wurst", "lance@" <> domain, integration)+ {:ok, _, _, _sso_user} = SSO.provision_user(identity)++ assert {:ok, updated_team} = SSO.set_force_sso(team, :all_but_owners)++ assert updated_team.id == team.id+ assert updated_team.policy.force_sso == :all_but_owners++ # Set another team policy setting to check if it remains intact+ {:ok, team} = SSO.update_policy(updated_team, sso_default_role: "editor")++ assert {:ok, updated_team} = SSO.set_force_sso(team, :none)++ updated_team = Repo.reload!(updated_team)++ assert updated_team.policy.force_sso == :none+ assert updated_team.policy.sso_default_role == :editor+ end+test "returns error when conditions not met" doteam = new_site().team