Class: V0::TermsOfUseAgreementsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/v0/terms_of_use_agreements_controller.rb

Constant Summary

Constants inherited from ApplicationController

ApplicationController::VERSION_STATUS

Constants included from SignIn::Authentication

SignIn::Authentication::BEARER_PATTERN

Constants included from ExceptionHandling

ExceptionHandling::SKIP_SENTRY_EXCEPTION_TYPES

Instance Attribute Summary

Attributes inherited from ApplicationController

#current_user

Instance Method Summary collapse

Methods inherited from ApplicationController

#clear_saved_form, #cors_preflight, #pagination_params, #render_job_id, #routing_error, #set_csrf_header

Methods included from Traceable

#set_trace_tags

Methods included from SentryControllerLogging

#set_tags_and_extra_context, #tags_context, #user_context

Methods included from SentryLogging

#log_exception_to_sentry, #log_message_to_sentry, #non_nil_hash?, #normalize_level, #rails_logger, #set_sentry_metadata

Methods included from Instrumentation

#append_info_to_payload

Methods included from SignIn::Authentication

#access_token, #access_token_authenticate, #authenticate, #authenticate_access_token, #bearer_token, #cookie_access_token, #handle_authenticate_error, #load_user, #load_user_object, #scrub_bearer_token, #validate_request_ip

Methods included from Headers

#set_app_info_headers

Methods included from ExceptionHandling

#render_errors, #report_mapped_exception, #report_original_exception, #skip_sentry_exception?, #skip_sentry_exception_types

Methods included from AuthenticationAndSSOConcerns

#authenticate, #clear_session, #extend_session!, #load_user, #log_sso_info, #render_unauthorized, #reset_session, #set_api_cookie!, #set_current_user, #set_session_expiration_header, #set_session_object, #sign_in_service_exp_time, #sign_in_service_session, #sso_cookie_content, #sso_logging_info, #validate_inbound_login_params, #validate_session

Methods included from SignIn::AudienceValidator

#authenticate, #validate_audience!

Instance Method Details

#acceptObject



18
19
20
21
22
23
24
25
26
27
28
# File 'app/controllers/v0/terms_of_use_agreements_controller.rb', line 18

def accept
  terms_of_use_agreement = acceptor.perform!
  unless terms_code_temporary_auth?
    recache_user
    current_user. unless 
  end

  render_success(action: 'accept', body: { terms_of_use_agreement: }, status: :created)
rescue TermsOfUse::Errors::AcceptorError => e
  render_error(action: 'accept', message: e.message)
end

#accept_and_provisionObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/controllers/v0/terms_of_use_agreements_controller.rb', line 30

def accept_and_provision
  terms_of_use_agreement = acceptor(sync: true).perform!
  if terms_of_use_agreement.accepted?
    provisioner.perform
    create_cerner_cookie

    unless terms_code_temporary_auth?
      recache_user
      current_user. unless 

    end

    render_success(action: 'accept_and_provision', body: { terms_of_use_agreement:, provisioned: true },
                   status: :created)
  else
    render_error(action: 'accept_and_provision', message: 'Failed to accept and provision')
  end
rescue TermsOfUse::Errors::AcceptorError, TermsOfUse::Errors::ProvisionerError => e
  render_error(action: 'accept_and_provision', message: e.message)
end

#acceptor(sync: false) ⇒ Object (private)



69
70
71
# File 'app/controllers/v0/terms_of_use_agreements_controller.rb', line 69

def acceptor(sync: false)
  TermsOfUse::Acceptor.new(user_account: @user_account, version: params[:version], sync:)
end

#authenticate_current_userObject (private)



108
109
110
111
112
113
# File 'app/controllers/v0/terms_of_use_agreements_controller.rb', line 108

def authenticate_current_user
  load_user(skip_terms_check: true)
  return unless current_user

  @user_account = current_user.
end

#authenticate_one_time_terms_codeObject (private)



99
100
101
102
103
104
105
106
# File 'app/controllers/v0/terms_of_use_agreements_controller.rb', line 99

def authenticate_one_time_terms_code
  terms_code_container = SignIn::TermsCodeContainer.find(params[:terms_code])
  return unless terms_code_container

  @user_account = UserAccount.find(terms_code_container.)
ensure
  terms_code_container&.destroy
end


86
87
88
89
90
91
92
93
# File 'app/controllers/v0/terms_of_use_agreements_controller.rb', line 86

def create_cerner_cookie
  cookies[TermsOfUse::Constants::PROVISIONER_COOKIE_NAME] = {
    value: TermsOfUse::Constants::PROVISIONER_COOKIE_VALUE,
    expires: TermsOfUse::Constants::PROVISIONER_COOKIE_EXPIRATION.from_now,
    path: TermsOfUse::Constants::PROVISIONER_COOKIE_PATH,
    domain: TermsOfUse::Constants::PROVISIONER_COOKIE_DOMAIN
  }
end

#declineObject



51
52
53
54
55
56
57
# File 'app/controllers/v0/terms_of_use_agreements_controller.rb', line 51

def decline
  terms_of_use_agreement = decliner.perform!
  recache_user unless terms_code_temporary_auth?
  render_success(action: 'decline', body: { terms_of_use_agreement: }, status: :created)
rescue TermsOfUse::Errors::DeclinerError => e
  render_error(action: 'decline', message: e.message)
end

#declinerObject (private)



73
74
75
# File 'app/controllers/v0/terms_of_use_agreements_controller.rb', line 73

def decliner
  TermsOfUse::Decliner.new(user_account: @user_account, version: params[:version])
end

#find_latest_agreement_by_version(version) ⇒ Object (private)



95
96
97
# File 'app/controllers/v0/terms_of_use_agreements_controller.rb', line 95

def find_latest_agreement_by_version(version)
  @user_account.terms_of_use_agreements.where(agreement_version: version).last
end

#latestObject



13
14
15
16
# File 'app/controllers/v0/terms_of_use_agreements_controller.rb', line 13

def latest
  terms_of_use_agreement = find_latest_agreement_by_version(params[:version])
  render_success(action: 'latest', body: { terms_of_use_agreement: })
end

#mpi_profileObject (private)



125
126
127
128
# File 'app/controllers/v0/terms_of_use_agreements_controller.rb', line 125

def mpi_profile
  @mpi_profile ||= MPI::Service.new.find_profile_by_identifier(identifier: @user_account.icn,
                                                               identifier_type: MPI::Constants::ICN)&.profile
end

#provisionerObject (private)



77
78
79
# File 'app/controllers/v0/terms_of_use_agreements_controller.rb', line 77

def provisioner
  TermsOfUse::Provisioner.new(icn: @user_account.icn)
end

#recache_userObject (private)



81
82
83
84
# File 'app/controllers/v0/terms_of_use_agreements_controller.rb', line 81

def recache_user
  current_user.needs_accepted_terms_of_use = current_user.&.needs_accepted_terms_of_use?
  current_user.save
end

#render_error(action:, message:, status: :unprocessable_entity) ⇒ Object (private)



139
140
141
142
# File 'app/controllers/v0/terms_of_use_agreements_controller.rb', line 139

def render_error(action:, message:, status: :unprocessable_entity)
  Rails.logger.error("[TermsOfUseAgreementsController] #{action} error: #{message}", { icn: @user_account.icn })
  render json: { error: message }, status:
end

#render_success(action:, body:, status: :ok, icn: @user_account.icn) ⇒ Object (private)



134
135
136
137
# File 'app/controllers/v0/terms_of_use_agreements_controller.rb', line 134

def render_success(action:, body:, status: :ok, icn: @user_account.icn)
  Rails.logger.info("[TermsOfUseAgreementsController] #{action} success", { icn: })
  render json: body, status:
end

#skip_mhv_account_creation?Boolean (private)

Returns:

  • (Boolean)


130
131
132
# File 'app/controllers/v0/terms_of_use_agreements_controller.rb', line 130

def 
  ActiveModel::Type::Boolean.new.cast(params[:skip_mhv_account_creation])
end

#terms_authenticateObject (private)



119
120
121
122
123
# File 'app/controllers/v0/terms_of_use_agreements_controller.rb', line 119

def terms_authenticate
  terms_code_temporary_auth? ? authenticate_one_time_terms_code : authenticate_current_user

  raise Common::Exceptions::Unauthorized unless @user_account
end

#terms_code_temporary_auth?Boolean (private)

Returns:

  • (Boolean)


115
116
117
# File 'app/controllers/v0/terms_of_use_agreements_controller.rb', line 115

def terms_code_temporary_auth?
  params[:terms_code].present?
end

#update_provisioningObject



59
60
61
62
63
64
65
# File 'app/controllers/v0/terms_of_use_agreements_controller.rb', line 59

def update_provisioning
  provisioner.perform
  create_cerner_cookie
  render_success(action: 'update_provisioning', body: { provisioned: true }, status: :ok)
rescue TermsOfUse::Errors::ProvisionerError => e
  render_error(action: 'update_provisioning', message: e.message)
end