class Ucert::WmsTracker

Class to handle Wms user account IDs

Attributes

file_user_map[RW]

Class constant variables

verbose[RW]

Class constant variables

wms_2_ad_user[R]
wms_role_entitlement[R]
wms_role_report[RW]

Class constant variables

wms_user_entitlement[R]
wms_user_report[RW]

Class constant variables

Public Class Methods

new(params ={}) click to toggle source

Instance default variables

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

  @verbose=params.fetch(:verbose, false)
  # Wms user entitlement report in CSV format, generated by Ryan Li's user table exportation script
  @wms_user_report = File.dirname(__FILE__)+"/../../data/wms/user_rpt.txt"
              # Wms user entitlement report in CSV format, generated by Ryan Li's user table exportation script
  @wms_role_report = File.dirname(__FILE__)+"/../../data/wms/role_rpt.txt"
              # WMS to AD user map file
              @file_user_map =  File.dirname(__FILE__)+"/../../data/wms/wms_user_map.txt"
              # Load the user map file to an instance variable (for performance gain)
              @wms_2_ad_user=load_known_user_map_from_file(@file_user_map)
              # Load the user entitlement instance variable from the native Wms user entitlement report
              @wms_user_entitlement=parse_wms_user_report(@wms_user_report)
              # Insert DN field into the user entitlement data structure
              insert_dn
              # Load the role entitlement instance variable from the native Wms role entitlement reportk=Uc
  @wms_role_entitlement=parse_wms_role_report(@wms_role_report)
              save!
      end

Public Instance Methods

print_role()
print_role_entitlement() click to toggle source

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

Also aliased as: print_role
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_wms_user_map!
save_wms_user_map!(file=@file_user_map) click to toggle source

Save the WMS to AD user mapping relation into the cache file

# File lib/ucert/wms_tracker.rb, line 205
def save_wms_user_map!(file=@file_user_map)
        puts "Saving the known Wms to AD user mapping relationship to file: #{file} ..." if @verbose
        begin
                timestamp=Time.now
                f=File.open(file, 'w')
                f.write "# local Wms to AD user map file created by the #{self.class} class #{__method__} method at: #{timestamp}"
                @wms_user_entitlement.values.map do |record|
                        key = record['USERNAME'].upcase + ':' + record['USERID'].upcase
                        value = record['DN']
                        f.write "\n#{key}|#{value}"
                end
                f.close
                puts "WMS 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: wms_search_by_dn
wms_search_by_dn(dn) click to toggle source

Search user entitlement record by AD DN

# File lib/ucert/wms_tracker.rb, line 225
    def wms_search_by_dn (dn)
            begin
  puts "Perform search on the user entitlement record by AD DN: #{dn}" if @verbose
  @wms_user_entitlement.each do |key, val|
      return val if @wms_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

Private Instance Methods

insert_dn() click to toggle source

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

# File lib/ucert/wms_tracker.rb, line 138
def insert_dn
                begin
                         tracker = Ucert::AdTracker.new(:verbose=>false)
                         @wms_user_entitlement.each do |index, record|
                                puts "\n\nPerform DN lookup for record: #{record}" if @verbose
                                key1 = record['USERNAME'] if record['USERNAME']
                                key2 = record['USERID'] if record['USERID']
                                my_key = key1.upcase + ":" + key2.upcase
                                puts "Perform 1st order search from the local cache: #{my_key}" if @verbose
                                if @wms_2_ad_user.key?(my_key)
                                        dn=@wms_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
                                end
                                @wms_user_entitlement[index]['DN'] = dn
                        end
                        tracker=nil
                rescue => ee
                        puts "Exception on method #{__method__}: #{ee}"
                        tracker=nil
                end
end
parse_wms_role_report(file) click to toggle source

Parsing the WMS role entitlement report in CSV format

# File lib/ucert/wms_tracker.rb, line 91
def parse_wms_role_report (file)
                begin
role_entitlement=Hash.new
line_cnt=0
                        cur_role_id = String.new
File.open(file,'r:gb2312:UTF-8').each do |line|
  puts "Processing line number #{line_cnt.inspect}" if @verbose
  # skip the 1st line of the file (header line)
  if line_cnt == 0
    line_cnt+=1
    next
  end
  entry=line.chomp.split(',').map {|x| x.gsub("\"","")} # clean the entry data
  puts entry.inspect if @verbose
  # Determine the start of a user_record
  entry[0].strip!
  if entry[0].nil?
    #do nothing
                                        line_cnt+=1
                                        next
  elsif entry[0].empty?
    #do nothing
                                        line_cnt+=1
                                        next
  elsif entry[0]!=cur_role_id
                                        puts "Processing role_id: #{entry[0]}" if @verbose
                                        cur_role_id = entry[0]
  end
                        role_entitlement[cur_role_id] = Hash.new unless role_entitlement.key?(cur_role_id)
  role_entitlement[cur_role_id]['ROLENAME'] = entry[1] unless role_entitlement[cur_role_id]['ROLENAME']
                                role_entitlement[cur_role_id]['PRV'] = Array.new unless role_entitlement[cur_role_id]['PRV']
  unless entry[2].nil?
    prv=Hash.new
    prv['PRVID']=entry[2]
                                        prv['PRVNAME']=entry[3]
    prv['URL']=entry[4]
    role_entitlement[cur_role_id]['PRV'].push(prv)
  end
  line_cnt+=1
                  end
return role_entitlement
                rescue => ee
                        puts "Exception on method #{__method__}: #{ee}"
                end
end
parse_wms_user_report(file) click to toggle source

Parsing the Wms user entitlement report in CSV format

# File lib/ucert/wms_tracker.rb, line 39
        def parse_wms_user_report (file)
                        begin
        user_entitlement=Hash.new
        user_record=0
        line_cnt=0
                                cur_user_id=String.new
        File.open(file,'r:gb2312:UTF-8').each do |line|
          puts "Processing line number #{line_cnt.inspect}" if @verbose
          # skip the 1st line of the file (header line)
          if line_cnt == 0
            line_cnt+=1
            next
          end
#          entry=line.chomp.split(/(\t|\,)/).map {|x| x.gsub("\"","")} # clean the entry data
                                        entry=line.chomp.split(",").map {|x| x.gsub("\"","")} # 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
          user_entitlement[user_record]=Hash.new unless user_entitlement.key?(user_record)
                                user_entitlement[user_record]['USERID']=entry[0].strip unless user_entitlement[user_record]['USERID']
          user_entitlement[user_record]['USERNAME']=entry[1].strip unless user_entitlement[user_record]['USERNAME']
                                        user_entitlement[user_record]['STATUS']=entry[2] unless user_entitlement[user_record]['STATUS']
                                        user_entitlement[user_record]['ORGNAME']=entry[3] unless user_entitlement[user_record]['ORGNAME']
          user_entitlement[user_record]['ROLEID']=Hash.new unless user_entitlement[user_record]['ROLEID']
          unless entry[4].nil?
            unless entry[4].empty?
              role=Hash.new
              role[entry[4]]=true
              user_entitlement[user_record]['ROLEID'].merge!(role)
            end
          end
          line_cnt+=1
          #return if line_cnt==3
                          end
        return user_entitlement
      rescue => ee
                                puts "Exception on method #{__method__}: #{ee}"
                        end
        end