mirror of
https://github.com/rickbarrette/simple-aircraft-manager-rails.git
synced 2026-11-17 15:50:36 -05:00
SAM_VIEWER=true (set only by the records package's viewer/docker-compose.yml) makes the app a read-only copy of an archive, for whoever receives the records: `docker compose up`, then open http://localhost:3000. - At start (bin/docker-entrypoint → sam:viewer:prepare) the package's .sam.zip, mounted read-only, is imported once. - No sign-in: everyone browses as a built-in account. Nothing can be changed: any request but GET/HEAD is refused, no role permission applies (so no edit buttons), and there's no sign-out or profile. A banner names the archive and when it was exported. - It refuses to start, and every page answers 503, when the database has any other account, so turning it on for a real installation stops the app instead of exposing its records. - The viewer listens on 127.0.0.1 only, and skips scheduled jobs (reminders, backups, AD searches). - Docs: the package README (three steps now), Access & sharing, README settings and features, the suggested recordkeeping procedures. Checked end to end with a built image and a real records package: the archive imported at start, pages browsed without an account, a change refused, and the container refusing to start once an account existed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
94 lines
2.8 KiB
Ruby
94 lines
2.8 KiB
Ruby
module Authentication
|
|
extend ActiveSupport::Concern
|
|
|
|
# A session unused for this long is signed out.
|
|
SESSION_IDLE_TIMEOUT = 30.days
|
|
|
|
included do
|
|
before_action :require_authentication
|
|
helper_method :authenticated?
|
|
end
|
|
|
|
class_methods do
|
|
def allow_unauthenticated_access(**options)
|
|
skip_before_action :require_authentication, **options
|
|
end
|
|
end
|
|
|
|
private
|
|
def authenticated?
|
|
resume_session
|
|
end
|
|
|
|
def require_authentication
|
|
resume_session || start_viewer_session || request_authentication
|
|
end
|
|
|
|
# Viewer mode: everyone is its built-in account, without signing in.
|
|
def start_viewer_session
|
|
start_new_session_for(Viewer.user) if Viewer.enabled? && !Viewer.real_accounts?
|
|
end
|
|
|
|
def resume_session
|
|
Current.session ||= find_session_by_cookie
|
|
end
|
|
|
|
def find_session_by_cookie
|
|
session = Session.find_by(id: cookies.signed[:session_id]) if cookies.signed[:session_id]
|
|
return unless session
|
|
if session.updated_at < SESSION_IDLE_TIMEOUT.ago || session.user.disabled?
|
|
session.destroy
|
|
cookies.delete(:session_id)
|
|
return
|
|
end
|
|
session.touch if session.updated_at < 1.hour.ago # last-active time, without a write per request
|
|
session
|
|
end
|
|
|
|
def request_authentication
|
|
session[:return_to_after_authenticating] = request.url
|
|
redirect_to new_session_path
|
|
end
|
|
|
|
def after_authentication_url
|
|
session.delete(:return_to_after_authenticating) || root_url
|
|
end
|
|
|
|
def start_new_session_for(user)
|
|
raise ArgumentError, "disabled account" if user.disabled?
|
|
user.update_column(:last_signed_in_at, Time.current)
|
|
user.sessions.create!(user_agent: request.user_agent, ip_address: request.remote_ip).tap do |session|
|
|
Current.session = session
|
|
cookies.signed.permanent[:session_id] = { value: session.id, httponly: true, same_site: :lax }
|
|
end
|
|
end
|
|
|
|
# Password or emailed-link sign-in: straight in, or first the
|
|
# authenticator code if the user has one set up (SSO skips this; the
|
|
# provider handles its own second factor).
|
|
OTP_PENDING_FOR = 5.minutes
|
|
|
|
def begin_sign_in(user, return_to:, notice: nil)
|
|
reset_session # fresh session id after signing in
|
|
if user.otp_enabled?
|
|
session[:otp_user_id] = user.id
|
|
session[:otp_started_at] = Time.current.to_i
|
|
session[:return_to_after_authenticating] = return_to
|
|
redirect_to two_factor_session_path
|
|
else
|
|
start_new_session_for user
|
|
redirect_to return_to, notice: notice
|
|
end
|
|
end
|
|
|
|
def pending_otp_user
|
|
return unless (id = session[:otp_user_id]) && Time.zone.at(session[:otp_started_at].to_i) > OTP_PENDING_FOR.ago
|
|
User.active.find_by(id: id)
|
|
end
|
|
|
|
def terminate_session
|
|
Current.session.destroy
|
|
cookies.delete(:session_id)
|
|
end
|
|
end
|