From 33aa0cecba335465faa61bce55d4e1fc8e5502a0 Mon Sep 17 00:00:00 2001 From: Sebastian Serth Date: Wed, 6 Sep 2023 22:22:33 +0200 Subject: [PATCH] Provide current-user application-wide * This change allows us to reuse the current user in multiple places. * Sentry still receives the same information as before. --- app/assets/javascripts/base.js | 9 +++++---- app/models/user.rb | 3 ++- app/views/layouts/application.html.slim | 2 +- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/app/assets/javascripts/base.js b/app/assets/javascripts/base.js index 1e6ed0b8..6ad97843 100644 --- a/app/assets/javascripts/base.js +++ b/app/assets/javascripts/base.js @@ -36,6 +36,9 @@ $(document).on('turbolinks:load', function() { window.location.reload(); }); + // Set current user + window.current_user = JSON.parse($('meta[name="current-user"]')?.attr('content') || null); + // Set locale for all JavaScript functions const htmlTag = $('html') I18n.defaultLocale = htmlTag.data('default-locale'); @@ -59,10 +62,8 @@ $(document).on('turbolinks:load', function() { integrations: window.SentryIntegrations(), profilesSampleRate: 1.0, initialScope: scope =>{ - const user = $('meta[name="current-user"]').attr('content'); - - if (user) { - scope.setUser(JSON.parse(user)); + if (current_user) { + scope.setUser(_.omit(current_user, 'displayname')); } return scope; } diff --git a/app/models/user.rb b/app/models/user.rb index f2113939..db159004 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -84,11 +84,12 @@ class User < ApplicationRecord displayname end - def to_sentry_context + def to_page_context { id:, type: self.class.name, consumer: consumer.name, + displayname:, } end diff --git a/app/views/layouts/application.html.slim b/app/views/layouts/application.html.slim index a8ddade3..afd05099 100644 --- a/app/views/layouts/application.html.slim +++ b/app/views/layouts/application.html.slim @@ -16,7 +16,7 @@ html lang="#{I18n.locale || I18n.default_locale}" data-default-locale="#{I18n.de = yield(:head) = csrf_meta_tags meta name='sentry' data-enabled=SentryJavascript.active?.to_s data-release=SentryJavascript.release data-dsn=SentryJavascript.dsn data-environment=SentryJavascript.environment - meta name='current-user' content=current_user&.to_sentry_context&.to_json + meta name='current-user' content=current_user&.to_page_context&.to_json body - unless @embed_options[:hide_navbar] nav.navbar.bg-dark.navbar-expand-md.mb-4.py-1 role='navigation' data-bs-theme="dark"