Skip to content

Commit

Permalink
Merge pull request #9093 from CitizenLabDotCo/TAN-2794-keycloak-impro…
Browse files Browse the repository at this point in the history
…vements

[TAN-2794] Added the ability to hide verification methods from the front end (for Oslo)
  • Loading branch information
jamesspeake authored Oct 14, 2024
2 parents 01c8f63 + 4d49fd9 commit fda5b03
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 14 deletions.
2 changes: 1 addition & 1 deletion back/app/models/app_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def closest_locale_to(locale)
end

def public_settings
@public_settings ||= SettingsService.new.remove_private_settings(settings, Settings.json_schema)
@public_settings ||= SettingsService.new.format_for_front_end(settings, Settings.json_schema)
end

def location
Expand Down
22 changes: 14 additions & 8 deletions back/app/services/settings_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,8 @@ def remove_additional_settings(settings, schema)
res
end

def remove_private_settings(settings, schema)
res = settings.deep_dup
schema['properties'].each do |feature, feature_schema|
feature_schema['properties'].each do |setting, setting_schema|
res[feature]&.delete(setting) if setting_schema['private']
end
end
res
def format_for_front_end(settings, schema)
remove_private_settings(settings, schema)
end

def activate_feature!(feature, config: nil, settings: {})
Expand Down Expand Up @@ -112,7 +106,19 @@ def minimal_required_settings(locales: ['en'], lifecycle_stage: 'demo')

private

def remove_private_settings(settings, schema)
res = settings.deep_dup
schema['properties'].each do |feature, feature_schema|
feature_schema['properties'].each do |setting, setting_schema|
res[feature]&.delete(setting) if setting_schema['private']
end
end
res
end

def default_setting(schema, feature, setting)
schema.dig('properties', feature, 'properties', setting, 'default')
end
end

SettingsService.prepend(Verification::Patches::SettingsService)
2 changes: 2 additions & 0 deletions back/config/schemas/settings.schema.json.erb
Original file line number Diff line number Diff line change
Expand Up @@ -1368,6 +1368,8 @@
"id_gent_rrn": ["verification"],
"id_oostende_rrn": ["verification"],
"id_id_card_lookup": ["verification"],
"id_keycloak": ["verification"],

"power_bi": ["public_api_tokens"],
"large_summaries": ["analysis"],
"ask_a_question": ["analysis"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ def verification_prioritized?
end

def email_confirmed?(auth)
# Response will tell us if the email is verified
auth&.info&.email_verified
# Even if the response says the email is NOT verified, we assume that it is if email is present
auth&.info&.email.present?
end

def filter_auth_to_persist(auth)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ def config_parameters
domain
client_id
client_secret
enabled_for_verified_actions
hide_from_profile
]
end

Expand All @@ -31,6 +33,16 @@ def config_parameters_schema
type: 'string',
description: 'The name this verification method will have in the UI',
default: 'ID-Porten'
},
enabled_for_verified_actions: {
private: true,
type: 'boolean',
description: 'Whether this verification method should be enabled for verified actions.'
},
hide_from_profile: {
private: true,
type: 'boolean',
description: 'Should verification be hidden in the user profile and under the username?'
}
}
end
Expand All @@ -56,5 +68,13 @@ def profile_to_uid(auth)
def updateable_user_attrs
super + %i[first_name last_name]
end

def enabled_for_verified_actions?
config[:enabled_for_verified_actions] || false
end

def ui_method_name
config[:ui_method_name] || name
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

module Verification
module Patches
module SettingsService
def format_for_front_end(settings, schema)
settings = disable_verification_if_no_methods_enabled(settings)
super
end

private

# Ensures the FE does not show verification if:
# a) There are no verification methods
# b) All verification methods are flagged as 'hide_from_profile'
def disable_verification_if_no_methods_enabled(settings)
return settings if !settings['verification'] || settings['verification']['enabled'] == false

enabled = settings['verification']['verification_methods'].present?
enabled = false if settings['verification']['verification_methods']&.pluck('hide_from_profile')&.all?(true)

settings['verification']['enabled'] = enabled
settings
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# frozen_string_literal: true

require 'rails_helper'

describe SettingsService do
let(:ss) { described_class.new }

describe 'disable_verification_if_no_methods_enabled' do
it 'disables verification if there are no methods enabled' do
settings = {
'verification' => {
'allowed' => true,
'enabled' => true
}
}

updated_settings = ss.send(:disable_verification_if_no_methods_enabled, settings)
expect(updated_settings['verification']['enabled']).to be false
end

it 'disables verification if all methods are hidden from the profile' do
settings = {
'verification' => {
'allowed' => true,
'enabled' => true,
'verification_methods' => [
{
'name' => 'nemlog_in',
'hide_from_profile' => true
},
{
'name' => 'keycloak',
'hide_from_profile' => true
}
]
}
}

updated_settings = ss.send(:disable_verification_if_no_methods_enabled, settings)
expect(updated_settings['verification']['enabled']).to be false
end

it 'does not disable verification if at least one method is NOT hidden from the profile' do
settings = {
'verification' => {
'allowed' => true,
'enabled' => true,
'verification_methods' => [
{
'name' => 'nemlog_in'
},
{
'name' => 'keycloak',
'hide_in_profile' => true
}
]
}
}

updated_settings = ss.send(:disable_verification_if_no_methods_enabled, settings)
expect(updated_settings['verification']['enabled']).to be true
end
end
end
6 changes: 3 additions & 3 deletions back/spec/services/settings_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@
end
end

describe 'remove_private_settings' do
describe 'format_for_front_end' do
let(:schema) do
{
'type' => 'object',
Expand All @@ -214,7 +214,7 @@
settings = {
'a' => { 'settings1' => true }
}
expect(ss.remove_private_settings(settings, schema)).to eq settings
expect(ss.format_for_front_end(settings, schema)).to eq settings
end

it 'removes private settings' do
Expand All @@ -224,7 +224,7 @@
expected_settings = {
'a' => { 'settings1' => true }
}
expect(ss.remove_private_settings(settings, schema)).to eq expected_settings
expect(ss.format_for_front_end(settings, schema)).to eq expected_settings
end
end
end

0 comments on commit fda5b03

Please sign in to comment.