class Ucert::MantisTracker
Class to handle CMBNY Mantis user report
Attributes
Class constant variables
Class constant variables
Class constant variables
Public Class Methods
Instance default variables
# File lib/ucert/mantis_tracker.rb, line 20 def initialize (params ={}) @verbose=params.fetch(:verbose, false) # CMBNY Mantis ticketing system user report generation: # To generate the active user report, you would need admin access and the corresponding plugins to dump out the user table in Mantis_ActiveUsers_Rpt # Please contact the IT team for the monthly reports. @mantis_user_entitlement_report = File.dirname(__FILE__)+"/../../data/mantis/Mantis_ActiveUsers_Rpt.xlsx" # Follow the similiar procedure above to obtain the update access_level definition. @mantis_access_level_report = File.dirname(__FILE__)+"/../../data/mantis/Mantis_AccessLevels.xlsx" # CMBNY Mantis to AD user map file @file_mantis_user_map = File.dirname(__FILE__)+"/../../data/mantis/mantis_access_user_map.txt" # Load user map from the local cacsh file @mantis_2_ad_user=load_known_user_map_from_file(@file_mantis_user_map) # Load the user entitlement instance variable from the user report @mantis_user_entitlement=parse_mantis_user_report(@mantis_user_entitlement_report) # Procedure to add DN foreign key to the @mantis_user_entitlement, by performing the AD search insert_dn # Load the access_level instance variable from the acliton report @mantis_access_level=parse_mantis_access_level_report(@mantis_access_level_report) # Save the user map to local cache file save! end
Public Instance Methods
Retrieve the user index from the @mantis_user_entitlement data structure
# File lib/ucert/mantis_tracker.rb, line 136 def dn_2_index (dn) begin (1..@mantis_user_entitlement.count).map do |index| return index if @mantis_user_entitlement[index]["DN"]==dn end rescue => ee puts "Exception on method #{__method__}: #{ee}" end end
Search user entitlement record by AD DN
# File lib/ucert/mantis_tracker.rb, line 234 def mantis_search_by_dn (dn) begin puts "Perform search on the user entitlement records by AD DN: #{dn}" if @verbose @mantis_user_entitlement.each do |key, val| return val if @mantis_user_entitlement[key]['DN'].eql? dn end return nil rescue => ee puts "Exception on method #{__method__}: #{ee}" end end
Print out the user access level table in plain text, to be imported into database
# File lib/ucert/mantis_tracker.rb, line 198 def print_access_level begin puts "User Access Level Report in Plain-text Format" if @verbose @mantis_access_level.first[1].each {|k,v| print k,"|"} if @verbose puts if @verbose @mantis_access_level.values.map do |rec| rec.each {|k,v| print v,"|"} puts end rescue => ee puts "Exception on method #{__method__}: #{ee}" end end
Private Instance Methods
Procedures to add additonal field 'dn' into the @mantis_user_entitlement data structure, by person the AD search
# File lib/ucert/mantis_tracker.rb, line 147 def insert_dn begin tracker = Ucert::AdTracker.new(:verbose=>false) @mantis_user_entitlement.each do |index, record| puts "\n\nPerform DN lookup for record: #{record}" if @verbose key1 = record['email'] if record['email'] key2 = record['username'] if record['username'] my_key = record['username'].upcase + ":" + record['email'].upcase puts "Perform 1st order search from the local cache: #{my_key}" if @verbose if @mantis_2_ad_user.key?(my_key) dn=@mantis_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 @mantis_user_entitlement[index]['DN'] = dn end tracker=nil rescue => ee puts "Exception on method #{__method__}: #{ee}" end end
Parsing the CMBNY Mantis access_level report in Excel workbook format TBD as currently the access_level report cannot be tied up to user report in batch
# File lib/ucert/mantis_tracker.rb, line 90 def parse_mantis_access_level_report (file) begin puts "Start parsing Excel workbook file: #{file}" if @verbose mantis_access_level=Hash.new workbook = RubyXL::Parser.parse(file) worksheet = workbook[0] row_cnt=0 acl_index=String.new # access_level index header=Array.new 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) end end end record = header.zip(entry).to_h.reject {|k,v| k.nil?} puts "access_level record: #{record}" if @verbose next if record["Acccess Level Code"].empty? acl_index = record["Acccess Level Code"] mantis_access_level[acl_index] = record unless mantis_access_level.key?(acl_index) end workbook=nil return mantis_access_level rescue => ee puts "Exception on method #{__method__}: #{ee}" end end
Parsing the CMBNY active Mantis user report in text format
# File lib/ucert/mantis_tracker.rb, line 43 def parse_mantis_user_report (file) begin puts "Start parsing Excel workbook file: #{file}" if @verbose mantis_user_entitlement=Hash.new workbook = RubyXL::Parser.parse(file) worksheet = workbook[0] row_cnt=0 user_index=0 # user record index header=Array.new 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) 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["id"].empty? mantis_user_entitlement[user_index] = record unless mantis_user_entitlement.key?(user_index) end workbook=nil return mantis_user_entitlement 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/mantis_tracker.rb, line 182 def print_user_entitlement begin puts "User Entitlement Report in Plain-text Format" if @verbose @mantis_user_entitlement[1].each {|k,v| print k,"|"} if @verbose puts if @verbose @mantis_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 Mantis to AD user mapping relation into the cache file
# File lib/ucert/mantis_tracker.rb, line 214 def save_mantis_user_map!(file=@file_mantis_user_map) puts "Saving the known Mantis to AD user mapping relationship to file: #{file} ..." if @verbose begin timestamp=Time.now f=File.open(file, 'w') f.write "# local CMBNY Mantis to AD user map file created by the #{self.class} class #{__method__} method at: #{timestamp}" @mantis_user_entitlement.values.map do |record| key = record['username'].upcase + ":" + record['email'].upcase value = record['DN'] f.write "\n#{key}|#{value}" end f.close puts "CMBNY Mantis to AD user map file is successfully saved to: #{file}" if @verbose rescue => ee puts "Exception on method #{__method__}: #{ee}" if @verbose end end