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.
This commit is contained in:
Sebastian Serth
2023-09-06 22:22:33 +02:00
committed by Sebastian Serth
parent 50a4707f65
commit 33aa0cecba
3 changed files with 8 additions and 6 deletions

View File

@ -36,6 +36,9 @@ $(document).on('turbolinks:load', function() {
window.location.reload(); window.location.reload();
}); });
// Set current user
window.current_user = JSON.parse($('meta[name="current-user"]')?.attr('content') || null);
// Set locale for all JavaScript functions // Set locale for all JavaScript functions
const htmlTag = $('html') const htmlTag = $('html')
I18n.defaultLocale = htmlTag.data('default-locale'); I18n.defaultLocale = htmlTag.data('default-locale');
@ -59,10 +62,8 @@ $(document).on('turbolinks:load', function() {
integrations: window.SentryIntegrations(), integrations: window.SentryIntegrations(),
profilesSampleRate: 1.0, profilesSampleRate: 1.0,
initialScope: scope =>{ initialScope: scope =>{
const user = $('meta[name="current-user"]').attr('content'); if (current_user) {
scope.setUser(_.omit(current_user, 'displayname'));
if (user) {
scope.setUser(JSON.parse(user));
} }
return scope; return scope;
} }

View File

@ -84,11 +84,12 @@ class User < ApplicationRecord
displayname displayname
end end
def to_sentry_context def to_page_context
{ {
id:, id:,
type: self.class.name, type: self.class.name,
consumer: consumer.name, consumer: consumer.name,
displayname:,
} }
end end

View File

@ -16,7 +16,7 @@ html lang="#{I18n.locale || I18n.default_locale}" data-default-locale="#{I18n.de
= yield(:head) = yield(:head)
= csrf_meta_tags = 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='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 body
- unless @embed_options[:hide_navbar] - unless @embed_options[:hide_navbar]
nav.navbar.bg-dark.navbar-expand-md.mb-4.py-1 role='navigation' data-bs-theme="dark" nav.navbar.bg-dark.navbar-expand-md.mb-4.py-1 role='navigation' data-bs-theme="dark"