diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 6c3e96f6..b965fad6 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -8,6 +8,7 @@ class ApplicationController < ActionController::Base before_action :http_basic_authenticate, unless: -> { FeatureFlags::FeatureFlag.active?(:service_open) } before_action :authenticate_dsi_user! before_action :handle_expired_session! + before_action :enforce_terms_and_conditions_acceptance! def http_basic_authenticate valid_credentials = [ @@ -63,4 +64,10 @@ def trigger_request_event DfE::Analytics::SendEvents.do([request_event.as_json]) end + + def enforce_terms_and_conditions_acceptance! + if current_dsi_user&.acceptance_required? + redirect_to terms_and_conditions_path + end + end end diff --git a/app/controllers/sign_in_controller.rb b/app/controllers/sign_in_controller.rb index 5e0687a8..fb22b198 100644 --- a/app/controllers/sign_in_controller.rb +++ b/app/controllers/sign_in_controller.rb @@ -1,6 +1,8 @@ class SignInController < ApplicationController skip_before_action :authenticate_dsi_user! skip_before_action :handle_expired_session! + skip_before_action :enforce_terms_and_conditions_acceptance! + before_action :reset_session before_action :handle_failed_sign_in, if: -> { params[:oauth_failure] == "true" } diff --git a/app/controllers/sign_out_controller.rb b/app/controllers/sign_out_controller.rb index d961e4dc..129c8437 100644 --- a/app/controllers/sign_out_controller.rb +++ b/app/controllers/sign_out_controller.rb @@ -1,5 +1,7 @@ class SignOutController < ApplicationController skip_before_action :handle_expired_session! + skip_before_action :enforce_terms_and_conditions_acceptance! + before_action :reset_session def new diff --git a/app/controllers/terms_and_conditions_controller.rb b/app/controllers/terms_and_conditions_controller.rb new file mode 100644 index 00000000..2cbdd9f4 --- /dev/null +++ b/app/controllers/terms_and_conditions_controller.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +class TermsAndConditionsController < ApplicationController + skip_before_action :enforce_terms_and_conditions_acceptance! + + def show + end + + def update + current_dsi_user.accept_terms! + redirect_to root_path, notice: "Terms and conditions accepted" + end +end diff --git a/app/models/dsi_user.rb b/app/models/dsi_user.rb index e952cd2a..707d1c38 100644 --- a/app/models/dsi_user.rb +++ b/app/models/dsi_user.rb @@ -5,6 +5,8 @@ class DsiUser < ApplicationRecord encrypts :first_name, :last_name encrypts :email, deterministic: true + CURRENT_TERMS_AND_CONDITIONS_VERSION = "1.0".freeze + def self.create_or_update_from_dsi(dsi_payload, role = nil) dsi_user = find_or_initialize_by(email: dsi_payload.info.fetch(:email)) @@ -26,6 +28,25 @@ def self.create_or_update_from_dsi(dsi_payload, role = nil) dsi_user end + def accept_terms! + update!( + terms_and_conditions_version_accepted: CURRENT_TERMS_AND_CONDITIONS_VERSION, + terms_and_conditions_accepted_at: Time.zone.now + ) + end + + def acceptance_required? + !current_version_accepted || acceptance_expired + end + + def current_version_accepted + terms_and_conditions_version_accepted == CURRENT_TERMS_AND_CONDITIONS_VERSION + end + + def acceptance_expired + terms_and_conditions_accepted_at < 12.months.ago + end + def internal? DfESignIn.bypass? || Role.enabled.internal.pluck(:code).include?(current_session&.role_code) end diff --git a/app/views/layouts/base.html.erb b/app/views/layouts/base.html.erb index 14d0d57f..542e3f6e 100644 --- a/app/views/layouts/base.html.erb +++ b/app/views/layouts/base.html.erb @@ -83,6 +83,9 @@