class Skala::AlephAdapter::UpdateUser

Public Instance Methods

call(username, options = {}) click to toggle source
# File lib/skala/aleph_adapter/update_user.rb, line 8
def call(username, options = {})
  username = username.upcase # ensure that username/id is always upcased
  valid_update_options = options.select { |_key, _| respond_to?("update_#{_key}", true) }.to_h

  #
  # Updating multiple properties in one go is a non trivial task, especially
  # if one of n updates fails, which implies rollbacks or partial update
  #
  if valid_update_options.keys.length > 1
    raise ArgumentError.new("Only one property can be changed within one call!")
  elsif valid_update_options.keys.length < 1
    raise ArgumentError.new("Unknown property to update!")
  end

  property_name, new_value = [valid_update_options.keys.first, valid_update_options.values.first]
  send("update_#{property_name}", username, new_value, options)
end

Private Instance Methods

update_email_address(user_id, new_email_address, options = {}) click to toggle source
# File lib/skala/aleph_adapter/update_user.rb, line 28
  def update_email_address(user_id, new_email_address, options = {})
    user_library = options[:user_library] || adapter.default_user_library
    resolved_user_id = resolve_user(user_id)

    raw_aleph_responses = ["01", "02"].map do |_z304_address_type|
      adapter.x_services.post(
        op: :update_bor,
        update_flag: "Y",
        library: user_library,
        xml_full_req: <<-XML.strip_heredoc
          <?xml version="1.0"?>
          <p-file-20>
            <patron-record>
              <z303>
                <match-id-type>00</match-id-type>
                <match-id>#{resolved_user_id}</match-id>
                <record-action>X</record-action>
              </z303>
              <z304>
                <record-action>U</record-action>
                <z304-id>#{resolved_user_id}</z304-id>
                <z304-address-type>#{_z304_address_type}</z304-address-type>
                <z304-sequence>01</z304-sequence>
                <z304-email-address>#{new_email_address}</z304-email-address>
              </z304>
            </patron-record>
          </p-file-20>
        XML
      )
    end

    if raw_aleph_responses.none? { |_raw_aleph_response| _raw_aleph_response[/Succeeded to REWRITE table z304/] }
      raise Skala::Adapter::Error
    end

    return true # if no exception was thrown so far
  end
update_password(user_id, new_password, options = {}) click to toggle source
# File lib/skala/aleph_adapter/update_user.rb, line 66
  def update_password(user_id, new_password, options = {})
    user_library = options[:user_library] || adapter.default_user_library
    resolved_user_id = resolve_user(user_id)

    # we have to change the password for all ids
    [resolved_user_id, user_id].uniq.each do |_user_id|
      _id_type = (_user_id == resolved_user_id) ? "00" : "01"

      raw_aleph_response = adapter.x_services.post(
        op: :update_bor,
        update_flag: "Y",
        library: user_library,
        xml_full_req: <<-XML.strip_heredoc
          <?xml version="1.0"?>
          <p-file-20>
            <patron-record>
              <z303>
                <match-id-type>#{_id_type}</match-id-type>
                <match-id>#{_user_id}</match-id>
                <record-action>X</record-action>
              </z303>
              <z308>
                <record-action>U</record-action>
                <z308-key-type>#{_id_type}</z308-key-type>
                <z308-key-data>#{_user_id}</z308-key-data>
                <z308-verification>#{new_password}</z308-verification>
              </z308>
            </patron-record>
          </p-file-20>
        XML
      )

      unless raw_aleph_response[/Succeeded to REWRITE table z308/]
        raise Skala::Adapter::Error
      end
    end

    return true # if no exception was thrown so far
  end