cirandas.net

ref: master

plugins/stoa/lib/stoa_plugin/usp_user.rb


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
require_relative '../../test/setup_db' if Rails.env.test?

class StoaPlugin::UspUser < ApplicationRecord

  establish_connection :stoa

  self.table_name = :pessoa

  SALT=YAML::load(File.open(StoaPlugin.root_path + 'config.yml'))['salt']

  alias_attribute :cpf, :numcpf
  alias_attribute :birth_date, :dtanas

  def self.exists?(usp_id)
    StoaPlugin::UspUser.find_by codpes: usp_id.to_i
  end

  def self.matches?(usp_id, field, value)
    usp_id.to_s.gsub!(/[.-]/,'')
    user = StoaPlugin::UspUser.find_by codpes: usp_id.to_i
    return false if user.nil? || field.blank? || !user.respond_to?(field) || value.blank?
    case field.to_sym
    when :cpf
      value.to_s.gsub!(/[.-]/,'')
      user.cpf == Digest::MD5.hexdigest(SALT+value.to_i.to_s)
    when :birth_date
      user.birth_date.to_s == value
    end
  end

end