class Decidim::Mpassid::Verification::MetadataCollector

Attributes

saml_attributes[R]

Public Class Methods

new(saml_attributes) click to toggle source
# File lib/decidim/mpassid/verification/metadata_collector.rb, line 7
def initialize(saml_attributes)
  @saml_attributes = saml_attributes
end

Public Instance Methods

metadata() click to toggle source
# File lib/decidim/mpassid/verification/metadata_collector.rb, line 11
def metadata
  {
    # Straight forward fetching of the "single" value attributes
    first_name: saml_attributes[:first_names] || saml_attributes[:given_name],
    given_name: saml_attributes[:given_name],
    last_name: saml_attributes[:last_name]
  }.tap do |data|
    # Map the SAML attribute keys to specific metadata attribute keys.
    {
      municipality: :municipality_code,
      municipality_name: :municipality_name,
      school_code: :school_code,
      school_name: :school_name,
      student_class: :class,
      student_class_level: :class_level
    }.each do |key, saml_key|
      # For all the "multi" value attributes, join the values with a
      # comma.
      val = saml_attributes[saml_key]
      val = val.join(",") if val
      data[key] = val
    end

    full_role = saml_attributes[:role]
    if full_role
      data[:role] = full_role.map do |role_string|
        # The fole string consists of four parts with the following
        # indexes:
        # - 0: Municipality name (same as `:municipality_name`)
        # - 1: School code (same as `:school_code`)
        # - 2: Group (same as `:class`)
        # - 3: User's role in the group
        role_parts = role_string.split(";")
        role_parts[3] if role_parts.length > 3
      end.join(",")
      # Do not store anything in case no roles were found
      data[:role] = nil if data[:role].empty?
    end
  end
end