class Ucert::YstTracker
Class to handle Yi Shi Tong (一事通) user account IDs
Attributes
Class constant variables
Class constant variables
Class constant variables
Class constant variables
Public Class Methods
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 out the org entitlement table in plain text, to be imported into database
# File lib/ucert/yst_tracker.rb, line 199 def print_org_entitlement begin puts "Organization Entitlement Report in Plain-text Format" if @verbose @yst_org_entitlement.first[1].each {|k,v| print k,"|"} if @verbose puts if @verbose @yst_org_entitlement.values.map do |rec| rec.each {|k,v| print v,"|"} puts end rescue => ee puts "Exception on method #{__method__}: #{ee}" end end
Print out the user entitlement table in plain text, to be imported into database
# File lib/ucert/yst_tracker.rb, line 183 def print_user_entitlement begin puts "User Entitlement Report in Plain-text Format" if @verbose @yst_user_entitlement.first[1].each {|k,v| print k,"|"} if @verbose puts if @verbose @yst_user_entitlement.values.map do |rec| rec.each {|k,v| print v,"|"} puts end rescue => ee puts "Exception on method #{__method__}: #{ee}" end end
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
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
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
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
Private Instance Methods
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
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
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