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
Limit traffic notification changes (#6828)
Cenk Kücük7f329f650684
diff --git a/lib/plausible/site/traffic_change_notification.ex b/lib/plausible/site/traffic_change_notification.exindex d84af9787f..4617a369cb 100644--- a/lib/plausible/site/traffic_change_notification.ex+++ b/lib/plausible/site/traffic_change_notification.ex@@ -24,6 +24,13 @@ defmodule Plausible.Site.TrafficChangeNotification do|> unique_constraint([:site_id, :type])end+ def update_changeset(schema, attrs) do+ schema+ |> cast(attrs, [:recipients, :threshold])+ |> validate_required([:recipients, :threshold])+ |> validate_number(:threshold, greater_than_or_equal_to: 1)+ end+def add_recipient(schema, recipient) doschema|> change(recipients: schema.recipients ++ [recipient])diff --git a/lib/plausible_web/controllers/site_controller.ex b/lib/plausible_web/controllers/site_controller.exindex 3a866bc6a5..bd8f8502c8 100644--- a/lib/plausible_web/controllers/site_controller.ex+++ b/lib/plausible_web/controllers/site_controller.ex@@ -501,7 +501,7 @@ defmodule PlausibleWeb.SiteController donotification =Repo.get_by(Plausible.Site.TrafficChangeNotification, site_id: site.id, type: type)- Plausible.Site.TrafficChangeNotification.changeset(notification, params)+ Plausible.Site.TrafficChangeNotification.update_changeset(notification, params)|> Repo.update!()conndiff --git a/test/plausible_web/controllers/site_controller_test.exs b/test/plausible_web/controllers/site_controller_test.exsindex 5b3ee9bad9..e2efc74e52 100644--- a/test/plausible_web/controllers/site_controller_test.exs+++ b/test/plausible_web/controllers/site_controller_test.exs@@ -1761,6 +1761,27 @@ defmodule PlausibleWeb.SiteControllerTest doassert notification.threshold == 15end++ test "does not allow changing site_id or type of #{type} notification", %{+ conn: conn,+ site: site+ } do+ other_site = new_site()+ notification = insert(:"#{unquote(type)}_notification", site: site)+ other_type = if unquote(type) == :spike, do: "drop", else: "spike"++ put(conn, "/sites/#{site.domain}/traffic-change-notification/#{unquote(type)}", %{+ "traffic_change_notification" => %{+ "site_id" => to_string(other_site.id),+ "type" => other_type+ }+ })++ notification = Repo.reload!(notification)++ assert notification.site_id == site.id+ assert notification.type == unquote(type)+ endenddescribe "POST /sites/:domain/traffic-change-notification/#{type}/recipients" do