class Ucert::Madison535Tracker

Class to handle Madison 535 building pass user report

Attributes

file_madison535_user_map[RW]

Class constant variables

madison535_2_ad_user[R]
madison535_user_access[R]
madison535_user_access_report[RW]

Class constant variables

madison535_user_access_report_2[RW]

Class constant variables

verbose[RW]

Class constant variables

Public Class Methods

new(params ={}) click to toggle source

Instance default variables

# File lib/ucert/madison535_tracker.rb, line 21
def initialize (params ={})
  @verbose=params.fetch(:verbose, false)
  # Madison535 user entitlement report generation:
              # Contact IT/GO for the Secured Access List report. Note you'll need to export the PDF report into Excel Workbook,
              # then scrubbing the data further, into the exact format we need here
  @madison535_user_access_report = File.dirname(__FILE__)+"/../../data/madison535/535madison_bldg_pass.xlsx"
              # This one is from GO directly
              @madison535_user_access_report_2 = File.dirname(__FILE__)+"/../../data/madison535/535madison_bldg_pass_2.xlsx"
  # Madison535 to AD user map file
  @file_madison535_user_map =  File.dirname(__FILE__)+"/../../data/madison535/madison535_user_map.txt"
              @madison535_user_access = Hash.new
              # Load user map from the local cacsh file
              @madison535_2_ad_user=load_known_user_map_from_file(@file_madison535_user_map)
              # Read the building security report
              @madison535_user_access=parse_madison535_user_access_report(@madison535_user_access_report)
              # Procedure to add DN foreign key to the @madison535_user_access, by performing the AD search
              insert_dn
              # Read the floor security report from GO
              parse_madison535_user_access_report_2(@madison535_user_access_report_2)
              insert_dn
              # Save the user map to local cache file
              save!
      end

Public Instance Methods

card_num_2_index(id) click to toggle source

Retrieve the user index based on card number

# File lib/ucert/madison535_tracker.rb, line 168
def card_num_2_index (id)
                begin
(1..@madison535_user_access.count).map do |index|
  return index if @madison535_user_access[index]["Card #"]==id.strip
end
                        return nil
                rescue => ee
                        puts "Exception on method #{__method__}: #{ee}"
                end
end
dn_2_index(dn) click to toggle source

Retrieve the user index from the @madison535_user_access data structure

# File lib/ucert/madison535_tracker.rb, line 157
def dn_2_index (dn)
                begin
(1..@madison535_user_access.count).map do |index|
  return index if @madison535_user_access[index]["DN"]==dn
end
                rescue => ee
                        puts "Exception on method #{__method__}: #{ee}"
                end
end
madison535_search_by_dn(dn) click to toggle source

Search user entitlement record by AD DN

# File lib/ucert/madison535_tracker.rb, line 258
    def madison535_search_by_dn (dn)
            begin
  puts "Perform search on the user entitlement records by AD DN: #{dn}" if @verbose
  @madison535_user_access.each do |key, val|
      return val if @madison535_user_access[key]['DN'].eql? dn
  end
                    return nil
rescue => ee
  puts "Exception on method #{__method__}: #{ee}"
end
    end
Also aliased as: search_by_dn
parse_madison535_user_access_report_2(file=@madison535_user_access_report_2) click to toggle source

Parsing the Madison535 user entitlement report from GO

# File lib/ucert/madison535_tracker.rb, line 92
def parse_madison535_user_access_report_2 (file=@madison535_user_access_report_2)
        #begin
                puts "Start parsing Excel workbook file: #{file}" if @verbose
                madison535_user_access=Hash.new
                workbook = RubyXL::Parser.parse(file)
                worksheet=workbook.worksheets.first
                row_cnt=0
                header=Array.new
                user_index=@madison535_user_access.count
                worksheet.count.times  do |row|
                        record = Hash.new
                        row_cnt+=1
                        puts "Parsing workbook row: #{row_cnt}" if @verbose
                        entry=Array.new
                        # Processing Header Row
                        if row_cnt==1
                                1.upto(worksheet[row].size) do |col|
                                        if worksheet[row][col].nil?
                                                header.push(nil)
                                        else
                                                header.push(worksheet[row][col].value.to_s)
                                        end
                                end
                                next
                        else
                        # Processing the record
                                1.upto(worksheet[row].size) do |col|
                                        if worksheet[row][col].nil?
                                                entry.push(nil)
                                        else
                                                entry.push(worksheet[row][col].value.to_s.strip)
                                        end
                                end
                        end
                        record = header.zip(entry).to_h.reject {|k,v| k.nil?}
                        card_num = record["Card #"].strip
                        my_index = card_num_2_index(card_num)
                        puts "User record: #{record}" if @verbose
                        puts "Card num: #{card_num}" if @verbose
                        puts "User index: #{my_index}" if @verbose
                        if my_index.nil?
                                # 'new' user not found in the building security report
                                user_index += 1
                                record["Company"]="CMBNY"
                                record["DN"]=nil
                                @madison535_user_access[user_index]=Hash.new unless @madison535_user_access.key(user_index)
                                @madison535_user_access[user_index]=record
                        else
                                puts "User Index: #{user_index}" if @verbose
                                #record["First Name"] = @madison535_user_access[my_index]["First Name"]
                                #record["Last Name"] = @madison535_user_access[my_index]["Last Name"]
                                record["Company"] = @madison535_user_access[my_index]["Company"]
                                record["DN"] = @madison535_user_access[my_index]["DN"]
                                #record["Company"] = @madison535_user_access[user_index]["Company"]
                                @madison535_user_access[my_index] = Hash.new unless @madison535_user_access.key?(my_index)
                                @madison535_user_access[my_index] = record
                        end
                end
                workbook=nil
        #rescue => ee
                #puts "Exception on method #{__method__}: #{ee}"
         #end
end
print_user()
Alias for: print_user_access
save!(file=@file_madison535_user_map)
search_by_dn(dn)

Private Instance Methods

insert_dn() click to toggle source

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

# File lib/ucert/madison535_tracker.rb, line 180
        def insert_dn
                        #begin
                                 tracker = Ucert::AdTracker.new(:verbose=>false)
                                 @madison535_user_access.each do |index, record|
                                        puts "\n\nPerform DN lookup for record: #{record}" if @verbose
                                        key1 = record['First Name'].split(/\s/)[0] + " " + record['Last Name'] if record['First Name'] and record['Last Name']
                                        key2 = record['First Name'].split(/\s+/)[1] + " " + record['Last Name'] if record['First Name'].split(/\s+/)[1] and record['Last Name']
                                        key3 = record['First Name'].gsub(/\s+/,"").split(/\(|\)/)[1] + " " + record['Last Name'] if record['First Name'].gsub(/\s+/,"").split(/\(|\)/)[1] and record['Last Name']
                                        my_key = record['Card #'].strip
                                        puts "Perform 1st order search from the local cache: #{my_key}" if @verbose
                                        if @madison535_2_ad_user.key?(my_key)
                                                dn=@madison535_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
#=begin
                                                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
#=end
                                        end
                                        @madison535_user_access[index]['DN'] = dn
                                end
                                tracker=nil
                        #rescue => ee
                        #    puts "Exception on method #{__method__}: #{ee}"
                        #end
        end
parse_madison535_user_access_report(file) click to toggle source

Parsing the Madison535 user entitlement report from building security

# File lib/ucert/madison535_tracker.rb, line 46
def parse_madison535_user_access_report (file)
        begin
                puts "Start parsing Excel workbook file: #{file}" if @verbose
                madison535_user_access=Hash.new
                workbook = RubyXL::Parser.parse(file)
                worksheet=workbook.worksheets.first
                row_cnt=0
                header=Array.new
                user_index=0
                worksheet.count.times  do |row|
                        row_cnt+=1
                        puts "Parsing workbook row: #{row_cnt}" if @verbose
                        entry=Array.new
                        # Processing Header Row
                        if row_cnt==1
                                0.upto(worksheet[row].size) do |col|
                                        if worksheet[row][col].nil?
                                                header.push(nil)
                                        else
                                                header.push(worksheet[row][col].value.to_s)
                                        end
                                end
                                next
                        else
                                0.upto(worksheet[row].size) do |col|
                                        if worksheet[row][col].nil?
                                                entry.push(nil)
                                        else
                                                entry.push(worksheet[row][col].value.to_s.strip)
                                        end
                                end
                                user_index += 1
                        end
                        record = header.zip(entry).to_h.reject {|k,v| k.nil?}
                        puts "User record: #{record}" if @verbose
                        next if record["Card #"].empty?
                        madison535_user_access[user_index] = record unless madison535_user_access.key?(user_index)
                end
                workbook=nil
                return madison535_user_access
        rescue => ee
                puts "Exception on method #{__method__}: #{ee}"
        end
end
print_user_access() click to toggle source

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

Also aliased as: print_user
save_madison535_user_map!(file=@file_madison535_user_map) click to toggle source

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

# File lib/ucert/madison535_tracker.rb, line 238
def save_madison535_user_map!(file=@file_madison535_user_map)
        puts "Saving the known Madison535 to AD user mapping relationship to file: #{file} ..." if @verbose
        begin
                timestamp=Time.now
                f=File.open(file, 'w')
                f.write "# Madison535 to AD user map file created by the #{self.class} class #{__method__} method at: #{timestamp}"
                @madison535_user_access.values.map do |record|
                        key = record['Card #'].strip
                        value = record['DN']
                        f.write "\n#{key}|#{value}"
                end
                f.close
                puts "Madison535 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!