File size: 20,246 Bytes
8da2481 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 | defmodule PlausibleWeb.SiteController do
use PlausibleWeb, :controller
use Plausible.Repo
use Plausible
alias Plausible.Sites
@unrestricted_actions [:new, :create_site]
@destructive_actions [:settings_danger_zone, :reset_stats, :delete_site]
@special_cased_actions @unrestricted_actions ++ @destructive_actions
plug(PlausibleWeb.RequireAccountPlug)
plug(
PlausibleWeb.Plugs.AuthorizeSiteAccess,
[:owner, :admin, :editor, :super_admin]
when action not in @special_cased_actions
)
plug(
PlausibleWeb.Plugs.AuthorizeSiteAccess,
[:owner, :admin, :super_admin] when action in @destructive_actions
)
def new(conn, params) do
flow = params["flow"] || PlausibleWeb.Flows.register()
team = conn.assigns.current_team
render(conn, "new.html",
changeset: Plausible.Site.changeset(%Plausible.Site{}),
site_limit: Plausible.Teams.Billing.site_limit(team),
site_limit_exceeded?: Plausible.Teams.Billing.ensure_can_add_new_site(team) != :ok,
form_submit_url: "/sites?flow=#{flow}",
flow: flow
)
end
def create_site(conn, %{"site" => site_params}) do
team = conn.assigns.current_team
user = conn.assigns.current_user
first_site? = Plausible.Teams.Billing.site_usage(team) == 0
flow = conn.params["flow"]
case Sites.create(user, site_params, team) do
{:ok, %{site: site}} ->
if first_site? do
PlausibleWeb.Email.welcome_email(user)
|> Plausible.Mailer.send()
end
redirect(conn,
to:
Routes.site_path(conn, :installation, site.domain,
site_created: true,
flow: flow
)
)
{:error, _, :permission_denied, _} ->
conn
|> put_flash(:error, "You are not permitted to add sites in the current team")
|> render("new.html",
changeset: Plausible.Site.changeset(%Plausible.Site{}),
first_site?: first_site?,
site_limit: Plausible.Teams.Billing.site_limit(team),
site_limit_exceeded?: false,
flow: flow,
form_submit_url: "/sites?flow=#{flow}"
)
{:error, _, {:over_limit, limit}, _} ->
render(conn, "new.html",
changeset: Plausible.Site.changeset(%Plausible.Site{}),
first_site?: first_site?,
site_limit: limit,
site_limit_exceeded?: true,
flow: flow,
form_submit_url: "/sites?flow=#{flow}"
)
{:error, _, changeset, _} ->
case check_can_already_access(changeset, user) do
{:ok, domain} ->
redirect(conn, to: Routes.stats_path(PlausibleWeb.Endpoint, :stats, domain, []))
{:error, :no_access} ->
render(conn, "new.html",
changeset: changeset,
first_site?: first_site?,
site_limit: Plausible.Teams.Billing.site_limit(team),
site_limit_exceeded?: false,
flow: flow,
form_submit_url: "/sites?flow=#{flow}"
)
end
end
end
def settings(conn, %{"domain" => domain}) do
redirect(conn, to: Routes.site_path(conn, :settings_general, domain))
end
def settings_general(conn, _params) do
site = conn.assigns[:site]
conn
|> render("settings_general.html",
site: site,
changeset: Plausible.Site.changeset(site, %{}),
connect_live_socket: true,
dogfood_page_path: "/:dashboard/settings/general",
layout: {PlausibleWeb.LayoutView, "site_settings.html"}
)
end
def settings_people(conn, _params) do
site = Repo.preload(conn.assigns.site, :team)
%{memberships: memberships, invitations: invitations} =
Sites.list_people(site)
conn
|> render("settings_people.html",
site: site,
memberships: memberships,
invitations: invitations,
dogfood_page_path: "/:dashboard/settings/people",
layout: {PlausibleWeb.LayoutView, "site_settings.html"}
)
end
def settings_visibility(conn, _params) do
site = conn.assigns[:site]
conn
|> render("settings_visibility.html",
site: site,
dogfood_page_path: "/:dashboard/settings/visibility",
connect_live_socket: true,
layout: {PlausibleWeb.LayoutView, "site_settings.html"}
)
end
def settings_goals(conn, _params) do
conn
|> render("settings_goals.html",
dogfood_page_path: "/:dashboard/settings/goals",
connect_live_socket: true,
layout: {PlausibleWeb.LayoutView, "site_settings.html"}
)
end
def settings_funnels(conn, _params) do
conn
|> render("settings_funnels.html",
dogfood_page_path: "/:dashboard/settings/funnels",
connect_live_socket: true,
layout: {PlausibleWeb.LayoutView, "site_settings.html"}
)
end
def settings_props(conn, _params) do
conn
|> render("settings_props.html",
dogfood_page_path: "/:dashboard/settings/properties",
layout: {PlausibleWeb.LayoutView, "site_settings.html"},
connect_live_socket: true
)
end
def settings_email_reports(conn, _params) do
site = conn.assigns[:site]
conn
|> render("settings_email_reports.html",
site: site,
weekly_report: Repo.get_by(Plausible.Site.WeeklyReport, site_id: site.id),
monthly_report: Repo.get_by(Plausible.Site.MonthlyReport, site_id: site.id),
spike_notification:
Repo.get_by(Plausible.Site.TrafficChangeNotification, site_id: site.id, type: :spike),
drop_notification:
Repo.get_by(Plausible.Site.TrafficChangeNotification, site_id: site.id, type: :drop),
dogfood_page_path: "/:dashboard/settings/email-reports",
layout: {PlausibleWeb.LayoutView, "site_settings.html"}
)
end
def settings_danger_zone(conn, _params) do
site = conn.assigns[:site]
conn
|> render("settings_danger_zone.html",
site: site,
connect_live_socket: true,
dogfood_page_path: "/:dashboard/settings/danger-zone",
layout: {PlausibleWeb.LayoutView, "site_settings.html"}
)
end
def settings_integrations(conn, _params) do
site =
conn.assigns.site
|> Repo.preload([:google_auth])
search_console_domains =
if site.google_auth do
Plausible.Google.API.fetch_verified_properties(site.google_auth)
end
has_plugins_tokens? = Plausible.Plugins.API.Tokens.any?(site)
conn
|> render("settings_integrations.html",
site: site,
has_plugins_tokens?: has_plugins_tokens?,
search_console_domains: search_console_domains,
dogfood_page_path: "/:dashboard/settings/integrations",
connect_live_socket: true,
layout: {PlausibleWeb.LayoutView, "site_settings.html"}
)
end
def settings_shields(conn, %{"shield" => shield})
when shield in ["ip_addresses", "countries", "pages", "hostnames"] do
site = conn.assigns.site
conn
|> render("settings_shields.html",
site: site,
shield: shield,
dogfood_page_path: "/:dashboard/settings/shields/#{shield}",
connect_live_socket: true,
layout: {PlausibleWeb.LayoutView, "site_settings.html"}
)
end
def settings_imports_exports(conn, _params) do
site = conn.assigns.site
conn
|> render("settings_imports_exports.html",
site: site,
dogfood_page_path: "/:dashboard/settings/imports-exports",
connect_live_socket: true,
layout: {PlausibleWeb.LayoutView, "site_settings.html"}
)
end
def update_google_auth(conn, %{"google_auth" => attrs}) do
site = conn.assigns[:site] |> Repo.preload(:google_auth)
Plausible.Site.GoogleAuth.set_property(site.google_auth, attrs)
|> Repo.update!()
conn
|> put_flash(:success, "Google integration saved successfully")
|> redirect(to: Routes.site_path(conn, :settings_integrations, site.domain))
end
def delete_google_auth(conn, _params) do
site =
conn.assigns[:site]
|> Repo.preload(:google_auth)
if site.google_auth do
Repo.delete!(site.google_auth)
end
put_flash(conn, :success, "Google account unlinked from Plausible")
redirect(conn, to: Routes.site_path(conn, :settings_integrations, site.domain))
end
def update_settings(conn, %{"site" => site_params}) do
site = conn.assigns[:site]
changeset = Plausible.Site.update_changeset(site, site_params)
case Repo.update(changeset) do
{:ok, site} ->
site_session_key = "authorized_site__" <> site.domain
conn
|> put_session(site_session_key, nil)
|> put_flash(:success, "Your site settings have been saved")
|> redirect(to: Routes.site_path(conn, :settings_general, site.domain))
{:error, changeset} ->
conn
|> put_flash(:error, "Could not update your site settings")
|> render("settings_general.html",
site: site,
changeset: changeset,
layout: {PlausibleWeb.LayoutView, "site_settings.html"}
)
end
end
def reset_stats(conn, _params) do
site = conn.assigns[:site]
Plausible.Purge.reset!(site)
conn
|> put_flash(:success, "#{site.domain} stats will be reset in a few minutes")
|> redirect(to: Routes.site_path(conn, :settings_danger_zone, site.domain))
end
def delete_site(conn, _params) do
site = conn.assigns[:site]
Plausible.Site.Removal.run(site)
conn
|> put_flash(:success, "Your site and page views deletion process has started.")
|> redirect(to: "/sites")
end
def make_public(conn, _params) do
site =
conn.assigns[:site]
|> Plausible.Site.make_public()
|> Repo.update!()
conn
|> put_flash(:success, "Stats for #{site.domain} are now public.")
|> redirect(to: Routes.site_path(conn, :settings_visibility, site.domain))
end
def make_private(conn, _params) do
site =
conn.assigns[:site]
|> Plausible.Site.make_private()
|> Repo.update!()
conn
|> put_flash(:success, "Stats for #{site.domain} are now private.")
|> redirect(to: Routes.site_path(conn, :settings_visibility, site.domain))
end
def enable_weekly_report(conn, _params) do
site = conn.assigns[:site]
result =
Plausible.Site.WeeklyReport.changeset(%Plausible.Site.WeeklyReport{}, %{
site_id: site.id,
recipients: [conn.assigns[:current_user].email]
})
|> Repo.insert()
:ok = tolerate_unique_contraint_violation(result, "weekly_reports_site_id_index")
conn
|> put_flash(:success, "You will receive an email report every Monday going forward")
|> redirect(to: Routes.site_path(conn, :settings_email_reports, site.domain))
end
def disable_weekly_report(conn, _params) do
site = conn.assigns[:site]
Repo.delete_all(from(wr in Plausible.Site.WeeklyReport, where: wr.site_id == ^site.id))
conn
|> put_flash(:success, "You will not receive weekly email reports going forward")
|> redirect(to: Routes.site_path(conn, :settings_email_reports, site.domain))
end
def add_weekly_report_recipient(conn, %{"recipient" => recipient}) do
site = conn.assigns[:site]
Repo.get_by(Plausible.Site.WeeklyReport, site_id: site.id)
|> Plausible.Site.WeeklyReport.add_recipient(recipient)
|> Repo.update!()
conn
|> put_flash(:success, "Added #{recipient} as a recipient for the weekly report")
|> redirect(to: Routes.site_path(conn, :settings_email_reports, site.domain))
end
def remove_weekly_report_recipient(conn, %{"recipient" => recipient}) do
site = conn.assigns[:site]
Repo.get_by(Plausible.Site.WeeklyReport, site_id: site.id)
|> Plausible.Site.WeeklyReport.remove_recipient(recipient)
|> Repo.update!()
conn
|> put_flash(
:success,
"Removed #{recipient} as a recipient for the weekly report"
)
|> redirect(to: Routes.site_path(conn, :settings_email_reports, site.domain))
end
def enable_monthly_report(conn, _params) do
site = conn.assigns[:site]
result =
%Plausible.Site.MonthlyReport{}
|> Plausible.Site.MonthlyReport.changeset(%{
site_id: site.id,
recipients: [conn.assigns[:current_user].email]
})
|> Repo.insert()
:ok = tolerate_unique_contraint_violation(result, "monthly_reports_site_id_index")
conn
|> put_flash(:success, "You will receive an email report every month going forward")
|> redirect(to: Routes.site_path(conn, :settings_email_reports, site.domain))
end
def disable_monthly_report(conn, _params) do
site = conn.assigns[:site]
Repo.delete_all(from(mr in Plausible.Site.MonthlyReport, where: mr.site_id == ^site.id))
conn
|> put_flash(:success, "You will not receive monthly email reports going forward")
|> redirect(to: Routes.site_path(conn, :settings_email_reports, site.domain))
end
def add_monthly_report_recipient(conn, %{"recipient" => recipient}) do
site = conn.assigns[:site]
Repo.get_by(Plausible.Site.MonthlyReport, site_id: site.id)
|> Plausible.Site.MonthlyReport.add_recipient(recipient)
|> Repo.update!()
conn
|> put_flash(:success, "Added #{recipient} as a recipient for the monthly report")
|> redirect(to: Routes.site_path(conn, :settings_email_reports, site.domain))
end
def remove_monthly_report_recipient(conn, %{"recipient" => recipient}) do
site = conn.assigns[:site]
Repo.get_by(Plausible.Site.MonthlyReport, site_id: site.id)
|> Plausible.Site.MonthlyReport.remove_recipient(recipient)
|> Repo.update!()
conn
|> put_flash(
:success,
"Removed #{recipient} as a recipient for the monthly report"
)
|> redirect(to: Routes.site_path(conn, :settings_email_reports, site.domain))
end
def enable_traffic_change_notification(conn, %{"type" => type}) do
site = conn.assigns[:site]
res =
Plausible.Site.TrafficChangeNotification.changeset(
%Plausible.Site.TrafficChangeNotification{},
%{
site_id: site.id,
type: type,
threshold: if(type == "spike", do: 10, else: 1),
recipients: [conn.assigns[:current_user].email]
}
)
|> Repo.insert()
case res do
{:ok, _} ->
conn
|> put_flash(:success, "Traffic #{type} notifications enabled")
|> redirect(to: Routes.site_path(conn, :settings_email_reports, site.domain))
{:error, _} ->
conn
|> put_flash(:error, "Unable to create a #{type} notification")
|> redirect(to: Routes.site_path(conn, :settings_email_reports, site.domain))
end
end
def disable_traffic_change_notification(conn, %{"type" => type}) do
site = conn.assigns[:site]
Repo.delete_all(
from(mr in Plausible.Site.TrafficChangeNotification,
where: mr.site_id == ^site.id and mr.type == ^type
)
)
conn
|> put_flash(:success, "Traffic #{type} notifications disabled")
|> redirect(to: Routes.site_path(conn, :settings_email_reports, site.domain))
end
def update_traffic_change_notification(conn, %{
"traffic_change_notification" => params,
"type" => type
}) do
site = conn.assigns[:site]
notification =
Repo.get_by(Plausible.Site.TrafficChangeNotification, site_id: site.id, type: type)
Plausible.Site.TrafficChangeNotification.changeset(notification, params)
|> Repo.update!()
conn
|> put_flash(:success, "Notification settings updated")
|> redirect(to: Routes.site_path(conn, :settings_email_reports, site.domain))
end
def add_traffic_change_notification_recipient(conn, %{"recipient" => recipient, "type" => type}) do
site = conn.assigns[:site]
Repo.get_by(Plausible.Site.TrafficChangeNotification, site_id: site.id, type: type)
|> Plausible.Site.TrafficChangeNotification.add_recipient(recipient)
|> Repo.update!()
conn
|> put_flash(:success, "Added #{recipient} as a recipient for the traffic spike notification")
|> redirect(to: Routes.site_path(conn, :settings_email_reports, site.domain))
end
def remove_traffic_change_notification_recipient(conn, %{
"recipient" => recipient,
"type" => type
}) do
site = conn.assigns[:site]
Repo.get_by(Plausible.Site.TrafficChangeNotification, site_id: site.id, type: type)
|> Plausible.Site.TrafficChangeNotification.remove_recipient(recipient)
|> Repo.update!()
conn
|> put_flash(
:success,
"Removed #{recipient} as a recipient for the monthly report"
)
|> redirect(to: Routes.site_path(conn, :settings_email_reports, site.domain))
end
def forget_import(conn, %{"import_id" => import_id}) do
site = conn.assigns.site
if site_import = Plausible.Imported.get_import(site, import_id) do
Oban.cancel_all_jobs(
from(j in Oban.Job,
where:
j.queue == "analytics_imports" and
fragment("(? ->> 'import_id')::int", j.args) == ^site_import.id
)
)
Plausible.Purge.delete_imported_stats!(site_import)
Plausible.Repo.delete!(site_import)
end
conn
|> put_flash(:success, "Imported data has been cleared")
|> redirect(to: Routes.site_path(conn, :settings_imports_exports, site.domain))
end
def forget_imported(conn, _params) do
site = conn.assigns.site
import_ids =
site
|> Plausible.Imported.list_all_imports()
|> Enum.map(& &1.id)
if import_ids != [] do
Oban.cancel_all_jobs(
from(j in Oban.Job,
where:
j.queue == "analytics_imports" and
fragment("(? ->> 'import_id')::int", j.args) in ^import_ids
)
)
Plausible.Purge.delete_imported_stats!(site)
Plausible.Imported.delete_imports_for_site(site)
end
conn
|> put_flash(:success, "Imported data has been cleared")
|> redirect(to: Routes.site_path(conn, :settings_integrations, site.domain))
end
on_ee do
def download_export(conn, _params) do
%{id: site_id, domain: domain} = conn.assigns.site
if s3_export = Plausible.Exports.get_s3_export!(site_id) do
s3_bucket = Plausible.S3.exports_bucket()
download_url = Plausible.S3.download_url(s3_bucket, s3_export.path)
redirect(conn, external: download_url)
else
conn
|> put_flash(:error, "Export not found")
|> redirect(to: Routes.site_path(conn, :settings_imports_exports, domain))
end
end
else
def download_export(conn, _params) do
%{id: site_id, domain: domain, timezone: timezone} = conn.assigns.site
if local_export = Plausible.Exports.get_local_export(site_id, domain, timezone) do
%{path: export_path, name: name} = local_export
conn
|> put_resp_content_type("application/zip")
|> put_resp_header("content-disposition", Plausible.Exports.content_disposition(name))
|> send_file(200, export_path)
else
conn
|> put_flash(:error, "Export not found")
|> redirect(to: Routes.site_path(conn, :settings_imports_exports, domain))
end
end
end
def csv_import(conn, _params) do
conn
|> assign(:skip_plausible_tracking, true)
|> render("csv_import.html",
connect_live_socket: true
)
end
defp tolerate_unique_contraint_violation(result, name) do
case result do
{:ok, _} ->
:ok
{:error,
%{
errors: [
site_id: {_, [constraint: :unique, constraint_name: ^name]}
]
}} ->
:ok
other ->
other
end
end
defp check_can_already_access(changeset, user) do
domain = Ecto.Changeset.get_change(changeset, :domain)
with true <- domain_taken_error?(changeset),
site = Sites.get_by_domain(domain),
true <- not is_nil(site),
true <- Plausible.Teams.Memberships.has_editor_access?(site, user) do
{:ok, domain}
else
false -> {:error, :no_access}
end
end
defp domain_taken_error?(changeset) do
Enum.any?(changeset.errors, fn
{:domain, {_, [constraint: :unique, constraint_name: "sites_domain_index"]}} -> true
_ -> false
end)
end
end
|