analytics/lib/plausible_web/plugs/authorize_site_access.ex

59 lines
1.6 KiB
Elixir
Raw Normal View History

defmodule PlausibleWeb.AuthorizeSiteAccess do
import Plug.Conn
use Plausible.Repo
def init([]), do: [:public, :viewer, :admin, :super_admin, :owner]
def init(allowed_roles), do: allowed_roles
def call(conn, allowed_roles) do
2022-08-05 10:24:24 +03:00
site =
Repo.get_by(Plausible.Site,
domain: conn.path_params["domain"] || conn.path_params["website"]
)
shared_link_auth = conn.params["auth"]
shared_link_record =
shared_link_auth && Repo.get_by(Plausible.Site.SharedLink, slug: shared_link_auth)
if !site do
PlausibleWeb.ControllerHelpers.render_error(conn, 404) |> halt
else
user_id = get_session(conn, :current_user_id)
membership_role = user_id && Plausible.Sites.role(user_id, site)
role =
cond do
2021-06-16 15:33:37 +03:00
user_id && membership_role ->
membership_role
Plausible.Auth.is_super_admin?(user_id) ->
:super_admin
site.public ->
:public
shared_link_record && shared_link_record.site_id == site.id ->
:public
true ->
nil
end
if role in allowed_roles do
Sentry.Context.set_user_context(%{id: user_id})
Plausible.OpenTelemetry.add_user_attributes(user_id)
Sentry.Context.set_extra_context(%{site_id: site.id, domain: site.domain})
Plausible.OpenTelemetry.add_site_attributes(site)
Add multiple imports per site (#3724) * Clean up references to no longer active `google_analytics_imports` Oban queue * Stub CSV importer * Add SiteImport schema * Rename `Plausible.Imported` module file to match module name * Add `import_id` column to `Imported.*` CH schemas * Implement Importer behavior and manage imports state using new entities * Implement importer callbacks and maintain site.imported_data for UA * Keep imports in sync when forgetting all imports * Scope imported data queries to completed import IDs * Mark newly imported data with respective import ID * Clean up Importer implementation a bit * Test querying legacy and new imported data * Send Oban notifications on import worker failure too * Fix checking for forgettable imports and remove redundant function * Fix UA integration test * Change site import source to atom enum and add source label * Add typespecs and reduce repetition in `Plausible.Imported` * Improve documentation and typespecs * Add test for purging particular import * Switch email notification templates depending on import source * Document running import synchronously * Fix UA importer args parsing and ensure it's covered by tests * Clear `site.stats_start_date` on complete import to force recalculation * Test Oban notifications (h/t @ruslandoga) * Purge stats on import failure right away to reduce a chance of leaving debris behind * Fix typos Co-authored-by: hq1 <hq@mtod.org> * Fix another typo * Refactor fetching earliest import and earliest stats start date * Use `Date.after?` instead of `Timex.after?` * Cache import data in site virtual fields and limit queried imports to 5 * Ensure always current `stats_start_date` is used * Work around broken typespec in Timex * Make `SiteController.forget_imported` action idempotent * Discard irrecoverably failed import tasks * Use macros for site import statuses There's also a fix ensuring only complete imports are considered where relevant - couldn't isolate it as it was in a common hunk * Use `import_id` as worker job uniqueness criterion * Do not load imported stats data in plugins API context --------- Co-authored-by: hq1 <hq@mtod.org>
2024-02-14 11:32:36 +03:00
site = Plausible.Imported.load_import_data(site)
merge_assigns(conn, site: site, current_user_role: role)
else
PlausibleWeb.ControllerHelpers.render_error(conn, 404) |> halt
end
end
end
end