From 6df48c1413a14516e7ee8919f33fbc13f0141966 Mon Sep 17 00:00:00 2001 From: erdgeist Date: Sat, 1 Aug 2026 02:39:59 +0200 Subject: Show and extend the elevation window A banner appears while elevated, counting down in minutes and carrying extend and drop controls. Three extensions of 30 minutes are allowed, so the window is at most two hours without a fresh code; a code resets the budget. The countdown is advisory, the server-side check authoritative, so it says "expired" rather than vanishing. The post-login flash tells an admin the timer has started. --- lib/authenticated_system.rb | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/authenticated_system.rb b/lib/authenticated_system.rb index 9a351dde..04d8051f 100644 --- a/lib/authenticated_system.rb +++ b/lib/authenticated_system.rb @@ -1,6 +1,7 @@ module AuthenticatedSystem SESSION_MAX_AGE = 7.days ELEVATION_MAX_AGE = 30.minutes + MAX_ELEVATION_EXTENSIONS = 3 protected # Returns true or false if the user is logged in. @@ -35,10 +36,26 @@ module AuthenticatedSystem def elevate! session[:elevated_at] = Time.now.to_i + session[:elevation_extensions] = 0 + end + + def elevation_extensions_left + return 0 unless elevated? + MAX_ELEVATION_EXTENSIONS - session[:elevation_extensions].to_i + end + + def renew_elevation! + return false unless elevated? + return false unless elevation_extensions_left > 0 + + session[:elevation_extensions] = session[:elevation_extensions].to_i + 1 + session[:elevated_at] = Time.now.to_i + true end def drop_elevation! session.delete(:elevated_at) + session.delete(:elevation_extensions) end # Check if the user is authorized @@ -113,7 +130,7 @@ module AuthenticatedSystem # available as ActionView helper methods. def self.included(base) base.send :helper_method, :current_user, :logged_in?, :authorized?, - :elevated?, :elevation_expires_at if base.respond_to? :helper_method + :elevated?, :elevation_expires_at, :elevation_extensions_left if base.respond_to? :helper_method end # @@ -147,6 +164,7 @@ module AuthenticatedSystem session[:user_id] = nil # keeps the session but kill our variable session.delete(:elevated_at) session.delete(:elevation_attempts) + session.delete(:elevation_extensions) end # The session should only be reset at the tail end of a form POST -- -- cgit v1.3