Skip to content

Self-hosted project management for devs and their clients: tickets, time tracking, docs and meetings, with an AI assistant that cites its sources. AGPL-3.0.

Link the demo back to the website it was reached from

Guido Boyke4f970dee0a1b
diff --git a/app/helpers/demo_helper.rb b/app/helpers/demo_helper.rb
new file mode 100644
index 0000000..a6f1f3e
--- /dev/null
+++ b/app/helpers/demo_helper.rb
@@ -0,0 +1,11 @@
+# frozen_string_literal: true
+
+# The way out of the public demo (Lalabase::Demo.website_url): one link, on the
+# sign-in page and in the banner. Nil when the demo instance names no website.
+module DemoHelper
+ def demo_website_link(**options)
+ url = Lalabase::Demo.website_url or return
+
+ link_to t('views.demo.website.back', site: Lalabase::Demo.website_name), url, **options
+ end
+end
diff --git a/app/views/devise/sessions/new.html.erb b/app/views/devise/sessions/new.html.erb
index ea311e7..4695bcf 100644
--- a/app/views/devise/sessions/new.html.erb
+++ b/app/views/devise/sessions/new.html.erb
@@ -12,8 +12,15 @@
is DELIBERATELY not implemented: it is marked there as an optional
suggestion, not an existing feature; with it the OR divider goes away too. %>
-<%# The demo has no sign-up: everybody uses the login shown in the form. %>
-<% unless Lalabase::Demo.enabled? %>
+<%# The demo has no sign-up: everybody uses the login shown in the form. Its place
+ holds the way back to the website the visitor came from, if the demo names one. %>
+<% if Lalabase::Demo.enabled? %>
+ <% if (website_link = demo_website_link(class: 'lp-auth-cross-link')) %>
+ <% content_for :auth_cross do %>
+ <p class="lp-auth-cross"><%= website_link %></p>
+ <% end %>
+ <% end %>
+<% else %>
<% content_for :auth_cross do %>
<p class="lp-auth-cross">
<%= t('.no_account') %>
diff --git a/app/views/layouts/_demo_banner.html.erb b/app/views/layouts/_demo_banner.html.erb
index f5b77dd..060620d 100644
--- a/app/views/layouts/_demo_banner.html.erb
+++ b/app/views/layouts/_demo_banner.html.erb
@@ -8,5 +8,6 @@
<strong><%= t('views.demo.banner.title') %></strong>
<span class="ms-2"><%= t('views.demo.banner.body', time: Lalabase::Demo.reset_time) %></span>
</p>
+ <%= demo_website_link(class: 'ms-auto shrink-0 font-semibold underline decoration-2 underline-offset-2 hover:no-underline') %>
</div>
</div>
diff --git a/config/locales/views/demo.de.yml b/config/locales/views/demo.de.yml
index 3ed4591..92aac6e 100644
--- a/config/locales/views/demo.de.yml
+++ b/config/locales/views/demo.de.yml
@@ -5,6 +5,8 @@ de:
banner:
title: "Demo"
body: "Alle Besucher melden sich mit diesem Konto an, andere sehen also, was du änderst. Jede Nacht um %{time} Uhr UTC wird alles zurückgesetzt."
+ website:
+ back: "Zurück zu %{site}"
sign_in:
lead: "Probier die Demo aus: Melde dich als %{name} an, Projektleitung bei einer erfundenen Softwarefirma."
email: "E-Mail"
diff --git a/config/locales/views/demo.en.yml b/config/locales/views/demo.en.yml
index 2c7de6f..9f5ca32 100644
--- a/config/locales/views/demo.en.yml
+++ b/config/locales/views/demo.en.yml
@@ -5,6 +5,8 @@ en:
banner:
title: "Demo"
body: "Every visitor signs in to this account, so others see what you change. Everything is reset every night at %{time} UTC."
+ website:
+ back: "Back to %{site}"
sign_in:
lead: "Try the demo: sign in as %{name}, project lead at an invented software company."
email: "Email"
diff --git a/lib/lalabase/demo.rb b/lib/lalabase/demo.rb
index e46e042..92265bc 100644
--- a/lib/lalabase/demo.rb
+++ b/lib/lalabase/demo.rb
@@ -6,8 +6,8 @@ module Lalabase
#
# Off unless LALABASE_DEMO=1. Everything that changes in demo mode asks
# enabled? when it runs, not at boot: the mail interceptor, the restrictions
- # (DemoRestrictions), the sign-in page, the banner, the lockout and the
- # per-address login throttle, the upload limits and budget, and the throttle on
+ # (DemoRestrictions), the sign-in page, the banner, the link back to the
+ # website, the lockout and the per-address login throttle, the upload limits and budget, and the throttle on
# everything that calls a model. An instance without the flag behaves as before.
#
# The nightly reset (Lalabase::Demo::Reset) needs a second latch, the marker
@@ -49,6 +49,12 @@ module Lalabase
# A body above this size carries a file; below it, a form or a message.
LARGE_BODY_BYTES = 64 * 1024
UPLOAD_CONTENT_TYPES = %w[image/png image/jpeg image/gif image/webp application/pdf].freeze
+ # The website a visitor came from, linked on the sign-in page and in the banner so
+ # the demo is no dead end. Set in the environment of the demo instance, never
+ # written here: the core names no operator's website
+ # (spec/architecture/app_operator_claims_guard_spec.rb).
+ WEBSITE_ENV = 'LALABASE_DEMO_WEBSITE_URL'
+ WEBSITE_SCHEMES = %w[https http].freeze
class << self
def enabled?(env = ENV)
@@ -75,6 +81,24 @@ module Lalabase
# The clock reads the same in both languages, so no locale format.
def reset_time = format('%02d:00', RESET_HOUR_UTC)
+
+ # The address in WEBSITE_ENV, or nil: without the flag, and for anything but an
+ # absolute http(s) address (a link with another scheme would run in the page).
+ def website_url(env = ENV)
+ return unless enabled?(env)
+
+ uri = URI.parse(env[WEBSITE_ENV].to_s.strip)
+ uri.to_s if WEBSITE_SCHEMES.include?(uri.scheme) && uri.host.present?
+ rescue URI::InvalidURIError
+ nil
+ end
+
+ # What the link says: the host without "www.", so the visitor sees where it goes.
+ def website_name(env = ENV)
+ url = website_url(env) or return
+
+ URI.parse(url).host.delete_prefix('www.')
+ end
end
# Drops every outgoing mail while demo mode is on. An invitation or a
diff --git a/spec/requests/demo_mode_spec.rb b/spec/requests/demo_mode_spec.rb
index 44557bb..cf9ee84 100644
--- a/spec/requests/demo_mode_spec.rb
+++ b/spec/requests/demo_mode_spec.rb
@@ -74,6 +74,72 @@ RSpec.describe 'Demo mode', :ci_guard, type: :request do
end
end
+ # Without it a visitor who came from the website finds no way back: the demo has
+ # no sign-up link, and the logo leads into the demo itself.
+ describe 'the way back to the website' do
+ let(:website) { 'https://www.example.com/en' }
+
+ around do |example|
+ website_was = ENV.fetch(Lalabase::Demo::WEBSITE_ENV, nil)
+ ENV[Lalabase::Demo::WEBSITE_ENV] = website
+ example.run
+ ensure
+ ENV[Lalabase::Demo::WEBSITE_ENV] = website_was
+ end
+
+ def sign_in_link
+ get new_user_session_path
+ Nokogiri::HTML(response.body).at_css('.lp-auth-top .lp-auth-cross a')
+ end
+
+ def banner_link
+ sign_in demo_user
+ get organisation_root_path(organisation)
+ expect(response).to have_http_status(:ok)
+ banner = Nokogiri::HTML(response.body).at_css('[data-testid="demo-banner"]')
+ expect(banner).to be_present
+ banner.at_css('a')
+ end
+
+ it 'is linked on the sign-in page and in the banner, named by its host' do
+ [sign_in_link, banner_link].each do |link|
+ expect(link['href']).to eq(website)
+ expect(link.text.strip).to eq('Back to example.com')
+ end
+ end
+
+ context 'when the instance names none' do
+ let(:website) { nil }
+
+ it 'is linked nowhere' do
+ expect(sign_in_link).to be_nil
+ expect(banner_link).to be_nil
+ end
+ end
+
+ ['javascript:alert(1)', 'example.com', 'ftp://example.com/', 'https://'].each do |value|
+ context "when the address is #{value.inspect}" do
+ let(:website) { value }
+
+ it 'is linked nowhere' do
+ expect(sign_in_link).to be_nil
+ expect(banner_link).to be_nil
+ end
+ end
+ end
+
+ context 'without the flag' do
+ let(:demo_on) { false }
+
+ it 'leaves the sign-up link where it was' do
+ link = sign_in_link
+ expect(link['href']).to eq(new_user_registration_path)
+ expect(response.body).not_to include(website)
+ expect(Lalabase::Demo.website_url).to be_nil
+ end
+ end
+ end
+
describe 'search engines' do
it 'are told not to index any response' do
get new_user_session_path