class Ucert::YstTracker

Class to handle Yi Shi Tong (一事通) user account IDs

Attributes

file_user_map[RW]

Class constant variables

verbose[RW]

Class constant variables

yst_2_ad_user[R]
yst_org_entitlement[R]
yst_org_report[RW]

Class constant variables

yst_user_entitlement[R]
yst_user_report[RW]

Class constant variables

Public Class Methods

new(params ={}) click to toggle source

Instance default variables

# File lib/ucert/yst_tracker.rb, line 18
def initialize (params ={})

  @verbose=params.fetch(:verbose, false)
  # Yst user entitlement report in CSV format, generated by Ryan Li's user table exportation script
  @yst_user_report = File.dirname(__FILE__)+"/../../data/yst/YiShiTong_User.csv"
              # Yst user entitlement report in CSV format, generated by Ryan Li's user table exportation script
  @yst_org_report = File.dirname(__FILE__)+"/../../data/yst/YiShiTong_Org.csv"
              # Yst to AD user map file
              @file_user_map =  File.dirname(__FILE__)+"/../../data/yst/yst_user_map.txt"
              # Load the user map file to an instance variable (for performance gain)
              @yst_2_ad_user=load_known_user_map_from_file(@file_user_map)
              # Load the user entitlement instance variable from the native Yst user entitlement report
              @yst_user_entitlement=parse_yst_user_report(@yst_user_report)
              # Insert DN field into the user entitlement data structure
              insert_dn
              # Load the org entitlement instance variable from the native Yst org entitlement reportk=Uc
  @yst_org_entitlement=parse_yst_org_report(@yst_org_report)
              save!
      end

Public Instance Methods

print_org()
print_org_entitlement() click to toggle source

Print out the org entitlement table in plain text, to be imported into database

Also aliased as: print_org
print_user()
print_user_entitlement() click to toggle source

Print out the user entitlement table in plain text, to be imported into database

Also aliased as: print_user
save!(file=@file_user_map)
Alias for: save_yst_user_map!
save_yst_user_map!(file=@file_user_map) click to toggle source

Save the Yi Shi Tong to AD user mapping relation into the cache file

# File lib/ucert/yst_tracker.rb, line 215
def save_yst_user_map!(file=@file_user_map)
        puts "Saving the known Yst to AD user mapping relationship to file: #{file} ..." if @verbose
        begin
                timestamp=Time.now
                f=File.open(file, 'w')
                f.write "# local Yst to AD user map file created by the #{self.class} class #{__method__} method at: #{timestamp}"
                @yst_user_entitlement.values.map do |record|
                        key = record['USER_NAME'].upcase + ':' + record['EMAIL'].upcase
                        value = record['DN']
                        f.write "\n#{key}|#{value}"
                end
                f.close
                puts "Yst to AD user map file is successfully saved to: #{file}" if @verbose
        rescue => ee
                puts "Exception on method #{__method__}: #{ee}" if @verbose
        end
end
Also aliased as: save!
search_by_dn(dn)
Alias for: yst_search_by_dn
search_by_id(id)
Alias for: yst_search_by_id
yst_id_2_dn(id) click to toggle source

DN lookup via YST for systems under YST, such as CVM

# File lib/ucert/yst_tracker.rb, line 174
def yst_id_2_dn (id)
        @yst_user_entitlement.each do |key,val|
                return val["DN"] if val["USER_ID"] == id.strip
        end
        return nil
end
yst_search_by_dn(dn) click to toggle source

Search user entitlement record by AD DN

# File lib/ucert/yst_tracker.rb, line 235
    def yst_search_by_dn (dn)
            begin
  puts "Perform search on the user entitlement record by AD DN: #{dn}" if @verbose
  @yst_user_entitlement.each do |key, val|
      return val if @yst_user_entitlement[key]['DN'].eql? dn
  end
                    return nil
rescue => ee
  puts "Exception on method #{__method__}: #{ee}"
end
    end
Also aliased as: search_by_dn
yst_search_by_id(id) click to toggle source

Search user entitlement record by USER_ID

# File lib/ucert/yst_tracker.rb, line 249
    def yst_search_by_id (id)
            begin
  puts "Perform search on the user entitlement record by USER_ID: #{id}" if @verbose
  @yst_user_entitlement.each do |key, val|
      return val if @yst_user_entitlement[key]['USER_ID'].eql? id.strip
  end
                    return nil
rescue => ee
  puts "Exception on method #{__method__}: #{ee}"
end
    end
Also aliased as: search_by_id

Private Instance Methods

insert_dn() click to toggle source

Procedures to add additonal field 'dn' into the @yst_user_entitlement data structure, by person the AD search

# File lib/ucert/yst_tracker.rb, line 127
def insert_dn
                begin
                         puts "Insert DN into the record ..." if @verbose
                         tracker = Ucert::AdTracker.new(:verbose=>@verbose)
                         @yst_user_entitlement.each do |index, record|
                                puts "\n\nPerform DN lookup for record: #{record}" if @verbose
                                key1 = record['USER_NAME'] if record['USER_NAME']
                                key2 = record['EMAIL'] if record['EMAIL']
                                key3 = key2.gsub(/\d+/,"")
                                key4 = key3.split("_")[0]
                                my_key = key1.upcase + ":" + key2.upcase
                                puts "Perform 1st order search from the local cache: #{my_key}" if @verbose
                                if @yst_2_ad_user.key?(my_key)
                                        dn=@yst_2_ad_user[my_key]
                                        # additional logic to update the existing DN record
                                        unless tracker.ad_person_records.key?(dn)
                                                dn = update_dn(tracker,dn)
                                        end
                                        puts "Found in the local cache file: #{dn}" if @verbose
                                else
                                        if dn.nil? and !key1.nil?
                                                puts "Perform 2nd order search only if the 1st one fail, by using: #{key1}" if @verbose
                                                dn = tracker.ad_search_by_text(key1, "person")
                                        end
                                        if dn.nil? and !key2.nil?
                                                puts "Perform 3rd order search only if the last fail, by using: #{key2}" if @verbose
                                                dn = tracker.ad_search_by_text(key2, "person")
                                        end
                                        if dn.nil? and !key3.nil?
                                                puts "Perform 4th order search only if the last fail, by using: #{key3}" if @verbose
                                                dn = tracker.ad_search_by_text(key3, "person")
                                        end
                                        if dn.nil? and !key4.nil?
                                                puts "Perform 5th order search only if the last fail, by using: #{key4}" if @verbose
                                                dn = tracker.ad_search_by_text(key4, "person")
                                        end
                                end
                                @yst_user_entitlement[index]['DN'] = dn
                        end
                        tracker=nil
                rescue => ee
                        puts "Exception on method #{__method__}: #{ee}"
                        tracker=nil
                end
end
parse_yst_org_report(file) click to toggle source

Parsing the Yi Shi Tong org entitlement report in CSV format

# File lib/ucert/yst_tracker.rb, line 84
def parse_yst_org_report (file)
        begin
                org_entitlement=Hash.new
                line_cnt=0
                cur_org_id=String.new
                header=Array.new
                #File.open(file,'r:gb18030:UTF-8').each do |line|
                # YST report encoding format change since September 2017. Refer to Ryan Li for more info. - Yang Li
                File.open(file,'r').each do |line|
                        puts "Processing YST organization report line number #{line_cnt.inspect}" if @verbose
                        entry=line.chomp.split(",").map {|x| x.gsub(/(\s|\")/,"")}
                        # skip the 1st line of the file (header line)
                        if line_cnt == 0
                                header=entry
                                line_cnt+=1
                                next
                        end
                         # clean the entry data
                        puts entry.inspect if @verbose
                        # Determine the start of a user_user_record
                        entry[0].strip!
                        if entry[0].nil?
                                line_cnt+=1
                                next
                        elsif entry[0].empty?
                                line_cnt+=1
                                next
                                #app=Hash.new
                        elsif entry[0] != cur_org_id
                                cur_org_id=entry[0]
                        end
                        record=Hash[header.zip(entry)] unless entry[0].nil?
                        puts "User record: #{record}" if @verbose
                        org_entitlement[cur_org_id]=record unless org_entitlement.key?(cur_org_id)
                        line_cnt+=1
                end
                return org_entitlement
        rescue => ee
                puts "Exception on method #{__method__}: #{ee}"
        end
end
parse_yst_user_report(file) click to toggle source

Parsing the Yst user entitlement report in CSV format

# File lib/ucert/yst_tracker.rb, line 39
        def parse_yst_user_report (file)
                        begin
        user_entitlement=Hash.new
        user_record=0
        line_cnt=0
                                cur_user_id=String.new
                                header=Array.new
#        File.open(file,'r:gb18030:UTF-8').each do |line|
                                # YST report encoding format change since September 2017. Refer to Ryan Li for more info. - Yang Li
                                File.open(file,'r').each do |line|
          puts "Processing YST user report line number #{line_cnt.inspect}" if @verbose
                                        entry=line.chomp.split(",").map {|x| x.gsub(/(\s|\")/,"")}
          # skip the 1st line of the file (header line)
          if line_cnt == 0
                                                header=entry
            line_cnt+=1
            next
          end
                                         # clean the entry data
          puts entry.inspect if @verbose
          # Determine the start of a user_user_record
          entry[0].strip!
          if entry[0].nil?
                                                line_cnt+=1
            next
                elsif entry[0].empty?
                                                line_cnt+=1
                                                next
                                                #app=Hash.new
          elsif entry[0] != cur_user_id
            user_record+=1
                                                cur_user_id=entry[0]
          end
          record=Hash[header.zip(entry)] unless entry[0].nil?
                                        puts "User record: #{record}" if @verbose
          user_entitlement[user_record]=record unless user_entitlement.key?(user_record)
          line_cnt+=1
                          end
        return user_entitlement
      rescue => ee
                                puts "Exception on method #{__method__}: #{ee}"
                        end
        end