1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-01-07 22:46:13 +01:00
fab-manager/lib/omni_auth/data_mapping/base.rb

50 lines
1.3 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2023-02-17 11:44:04 +01:00
# Type-dependant aata mapping functions for SSO authentications (through OmniAuth)
module OmniAuth::DataMapping::Base
extend ActiveSupport::Concern
2023-02-17 11:44:04 +01:00
# rubocop:disable Metrics/BlockLength
included do
def local_sym(mapping)
"#{mapping.local_model}.#{mapping.local_field}".to_sym
end
2023-02-17 11:44:04 +01:00
def map_transformation(transformation, raw_data)
value = nil
transformation['mapping']&.each do |m|
if m['from'] == raw_data
value = m['to']
break
end
end
2023-02-17 11:44:04 +01:00
# if no transformation had set any value, return the raw value
value || raw_data
end
2023-02-17 11:44:04 +01:00
def map_boolean(transformation, raw_data)
return false if raw_data == transformation['false_value']
2023-02-17 11:44:04 +01:00
true if raw_data == transformation['true_value']
end
2023-02-17 11:44:04 +01:00
def map_date(transformation, raw_data)
case transformation['format']
when 'iso8601'
Time.zone.iso8601(raw_data)
when 'rfc2822'
Time.rfc2822(raw_data).in_time_zone
when 'rfc3339'
Time.rfc3339(raw_data).in_time_zone
when 'timestamp-s'
Time.zone.strptime(raw_data, '%s')
when 'timestamp-ms'
Time.zone.strptime(raw_data, '%Q')
else
Time.zone.parse(raw_data)
end
end
end
2023-02-17 11:44:04 +01:00
# rubocop:enable Metrics/BlockLength
end