Blame | Last modification | View Log | RSS feed
######################################################################### COPYRIGHT - VIX IP PTY LTD ("VIX"). ALL RIGHTS RESERVED.## Module name : rmMerge_process.pl# Module type : JATS Utility# Compiler(s) : Perl# Environment(s): jats## Description : Compare two sets of users## Usage : See POD at the end of this file##......................................................................#require 5.008_002;use strict;use warnings;use Pod::Usage;use Getopt::Long;use JatsError;use JatsRmApi;use JatsSystem;use FileUtils;use ConfigurationFile;use File::Copy;use DBI;my $RM_DB;my $opt_help=0;my $opt_verbose=0;my $opt_debug=0;my $opt_add = 0;my $VERSION = "1.0";my @oldRMCred = ('OLD', 'jdbc:oracle:thin:@auawsards001:1521:RELEASEM', 'ACCESS_MANAGER', 'skdf0fdfdi');my @newRMCred = ('NEW', 'jdbc:oracle:thin:@auawsards002:1521:RELEASEM', 'RM_READONLY', 'Tp8WmmDKMq2Z');my %oldData;my %newData;#-------------------------------------------------------------------------------# Function : Mainline Entry Point## Description :## Inputs :#my $result = GetOptions ("help:+" => \$opt_help,"manual:3" => \$opt_help,"verbose:+" => \$opt_verbose,"debug:+" => \$opt_debug,"add" => \$opt_add,);## UPDATE THE DOCUMENTATION AT THE END OF THIS FILE !!!### Process help and manual options#pod2usage(-verbose => 0, -message => "Version: $VERSION") if ($opt_help == 1 || ! $result);pod2usage(-verbose => 1) if ( $opt_help == 2 );pod2usage(-verbose => 2) if ( $opt_help > 2 );#pod2usage(-verbose => 0, -message => "Version: $VERSION") if ( $#ARGV < 0 );## Configure the error reporting rmMerge_process now that we have the user options#ErrorConfig( 'name' =>'RELCMP','verbose' => $opt_verbose,'debug' => $opt_debug,);## Determine matching Release Names#getReleaseData( \%oldData, @oldRMCred);getReleaseData( \%newData, @newRMCred);#DebugDumpData("oldData", \%oldData);#DebugDumpData("newData", \%newData);## Report differences## Don't care about these#foreach my $uname ( sort keys %oldData) {# next if exists $newData{$uname};# print("Missing in new: $uname, $oldData{$uname}{FULL_NAME}, $oldData{$uname}{USER_EMAIL}, $oldData{$uname}{IS_DISABLED}\n");#}foreach my $uname ( sort keys %newData) {next if exists $oldData{$uname};print("Missing in old: $uname, $newData{$uname}{FULL_NAME}, $newData{$uname}{USER_EMAIL}, $newData{$uname}{DOMAIN}, $newData{$uname}{IS_DISABLED}\n");if ($opt_add) {getUserRoles ($newData{$uname}, @newRMCred);getRoleMap ($newData{$uname}, @oldRMCred);addUser($newData{$uname}, @oldRMCred);getUserId($newData{$uname}, @oldRMCred);AddRmAppliction($newData{$uname}, @oldRMCred);AddRoles($newData{$uname}, @oldRMCred);DebugDumpData("Data", $newData{$uname});}}foreach my $uname ( sort keys %oldData) {next unless exists $newData{$uname};next if $oldData{$uname}{FULL_NAME} eq $oldData{$uname}{FULL_NAME} && $oldData{$uname}{USER_EMAIL} eq $oldData{$uname}{USER_EMAIL} && $oldData{$uname}{IS_DISABLED} eq $oldData{$uname}{IS_DISABLED};print ("Changed $uname -> $oldData{$uname}{FULL_NAME}, $oldData{$uname}{USER_EMAIL}, $oldData{$uname}{IS_DISABLED} :: $newData{$uname}{FULL_NAME}, $newData{$uname}{USER_EMAIL}, $newData{$uname}{IS_DISABLED} \n");}#-------------------------------------------------------------------------------# Function : addUser## Description : Add a user to a Release Manager instance## Inputs : $data - Ref to user data# oldRMCred## Returns :#sub addUser{my ($data, $id, $url, $name, $passwd) = @_;my (@row);Message ("Add: $data->{USER_NAME}");$ENV{GBE_RM_LOCATION} = $url;$ENV{GBE_RM_USERNAME} = $name;$ENV{GBE_RM_PASSWORD} = $passwd;connectRM(\$RM_DB);# First get details from pv_idmy $m_sqlstr = <<"SQL_END";BEGIN PK_USER.ADD_USER_ACCOUNT ( :FULL_NAME, :USER_NAME, :USER_EMAIL, :DOMAIN ); END;SQL_END$m_sqlstr =~ s~:FULL_NAME~'$data->{FULL_NAME}'~g;$m_sqlstr =~ s~:USER_NAME~'$data->{USER_NAME}'~g;$m_sqlstr =~ s~:USER_EMAIL~'$data->{USER_EMAIL}'~g;$m_sqlstr =~ s~:DOMAIN~'$data->{DOMAIN}'~g;my $sth = $RM_DB->prepare($m_sqlstr);if ( defined($sth) ){if ( $sth->execute( ) ){if ( $sth->rows ){while ( @row = $sth->fetchrow_array ){}}$sth->finish();}else{Error("Execute failure: $m_sqlstr", $sth->errstr() );}}else{Error("Prepare failure" );}disconnectRM(\$RM_DB);}#-------------------------------------------------------------------------------# Function : getReleaseData## Description : Get Release Content and related data for a release## Inputs : dataRef# RmCredentails## Returns :#sub getReleaseData{my ($dataRef, $id, $url, $name, $passwd) = @_;my (@row);Message ("Extract configured users");$ENV{GBE_RM_LOCATION} = $url;$ENV{GBE_RM_USERNAME} = $name;$ENV{GBE_RM_PASSWORD} = $passwd;connectRM(\$RM_DB);# First get details from pv_idmy $m_sqlstr = <<"SQL_END";SELECT USER_NAME, FULL_NAME, USER_EMAIL, IS_DISABLED, DOMAIN, USER_ID FROM USERSSQL_ENDmy $sth = $RM_DB->prepare($m_sqlstr);if ( defined($sth) ){if ( $sth->execute( ) ){if ( $sth->rows ){while ( @row = $sth->fetchrow_array ){$dataRef->{$row[0]}{USER_NAME} =$row[0] || '';$dataRef->{$row[0]}{FULL_NAME} =$row[1] || '';$dataRef->{$row[0]}{USER_EMAIL} =$row[2] || '';$dataRef->{$row[0]}{IS_DISABLED} = $row[3] || '';$dataRef->{$row[0]}{DOMAIN} = $row[4] || '';$dataRef->{$row[0]}{USER_ID} = $row[5] || '';}}$sth->finish();}else{Error("Execute failure: $m_sqlstr", $sth->errstr() );}}else{Error("Prepare failure" );}disconnectRM(\$RM_DB);}#-------------------------------------------------------------------------------# Function : getUserRoles## Description : Get a users roles - only within Release Manager## Inputs :## Returns :#sub getUserRoles{my ($dataRef, $id, $url, $name, $passwd) = @_;my (@row);Message ("Extract configured users");$ENV{GBE_RM_LOCATION} = $url;$ENV{GBE_RM_USERNAME} = $name;$ENV{GBE_RM_PASSWORD} = $passwd;connectRM(\$RM_DB);# First get details from pv_idmy $m_sqlstr = <<"SQL_END";SELECTr.role_name, r.role_idFROMuser_roles ur,roles rWHEREur.user_id = :USER_IDAND r.role_id = ur.role_idAND app_id = 2AND r.is_role_variation IS NULLSQL_END$m_sqlstr =~ s~:USER_ID~$dataRef->{USER_ID}~g;my $sth = $RM_DB->prepare($m_sqlstr);if ( defined($sth) ){if ( $sth->execute( ) ){if ( $sth->rows ){while ( @row = $sth->fetchrow_array ){$dataRef->{ROLES}{$row[0]} = $row[1];}}$sth->finish();}else{Error("Execute failure: $m_sqlstr", $sth->errstr() );}}else{Error("Prepare failure" );}disconnectRM(\$RM_DB);}#-------------------------------------------------------------------------------# Function : getUserId## Description : Get a user id## Inputs :## Returns :#sub getUserId{my ($dataRef, $id, $url, $name, $passwd) = @_;my (@row);Message ("Determine new user id");$ENV{GBE_RM_LOCATION} = $url;$ENV{GBE_RM_USERNAME} = $name;$ENV{GBE_RM_PASSWORD} = $passwd;connectRM(\$RM_DB);# First get details from pv_idmy $m_sqlstr = <<"SQL_END";SELECTuser_idFROMusersWHEREuser_name = :USER_NAMESQL_END$m_sqlstr =~ s~:USER_NAME~'$dataRef->{USER_NAME}'~g;my $sth = $RM_DB->prepare($m_sqlstr);if ( defined($sth) ){if ( $sth->execute( ) ){if ( $sth->rows ){while ( @row = $sth->fetchrow_array ){$dataRef->{NEW_USER_ID} = $row[0];}}$sth->finish();}else{Error("Execute failure: $m_sqlstr", $sth->errstr() );}}else{Error("Prepare failure" );}disconnectRM(\$RM_DB);Message ("New User id: $dataRef->{NEW_USER_ID}");}#-------------------------------------------------------------------------------## Description : Add a user to a Release Manager instance## Inputs : $data - Ref to user data# oldRMCred## Returns :#sub AddRmAppliction{my ($data, $id, $url, $name, $passwd) = @_;my (@row);Message ("Add to Rm Appliaction: $data->{USER_NAME}");$ENV{GBE_RM_LOCATION} = $url;$ENV{GBE_RM_USERNAME} = $name;$ENV{GBE_RM_PASSWORD} = $passwd;connectRM(\$RM_DB);# First get details from pv_idmy $m_sqlstr = <<"SQL_END";BEGIN pk_User.Add_Application_User ( :USER_ID_LIST, :APP_ID, :INCLUDE_EVERYONE ); END;SQL_END$m_sqlstr =~ s~:USER_ID_LIST~'$data->{NEW_USER_ID}'~g;$m_sqlstr =~ s~:APP_ID~2~g;$m_sqlstr =~ s~:INCLUDE_EVERYONE~null~g;my $sth = $RM_DB->prepare($m_sqlstr);if ( defined($sth) ){if ( $sth->execute( ) ){if ( $sth->rows ){while ( @row = $sth->fetchrow_array ){}}$sth->finish();}else{Error("Execute failure: $m_sqlstr", $sth->errstr() );}}else{Error("Prepare failure" );}disconnectRM(\$RM_DB);}#-------------------------------------------------------------------------------## Description : Get role map## Inputs : $data - Ref to user data# oldRMCred## Returns :#sub getRoleMap{my ($data, $id, $url, $name, $passwd) = @_;my (@row);Message ("Map roles: $data->{USER_NAME}");my $roleList = quoteList(keys %{$data->{ROLES}});return unless length($roleList) > 0;$ENV{GBE_RM_LOCATION} = $url;$ENV{GBE_RM_USERNAME} = $name;$ENV{GBE_RM_PASSWORD} = $passwd;connectRM(\$RM_DB);# First get details from pv_idmy $m_sqlstr = <<"SQL_END";select role_id, role_name from roles r where role_name in ( :ROLE_NAME_LIST ) and app_id = :APP_IDSQL_END$m_sqlstr =~ s~:ROLE_NAME_LIST~$roleList~g;$m_sqlstr =~ s~:APP_ID~2~g;my $sth = $RM_DB->prepare($m_sqlstr);if ( defined($sth) ){if ( $sth->execute( ) ){if ( $sth->rows ){while ( @row = $sth->fetchrow_array ){$data->{ROLE_MAP}{$row[1]} = $row[0];}}$sth->finish();}else{Error("Execute failure: $m_sqlstr", $sth->errstr() );}}else{Error("Prepare failure" );}disconnectRM(\$RM_DB);## Check that all the roles have been mapped#foreach my $role (keys %{$data->{ROLES}}) {unless (exists $data->{ROLE_MAP}{$role}) {ReportError("Role cannot be mapped: $role");}}ErrorDoExit();}#-------------------------------------------------------------------------------## Description : Get role map## Inputs : $data - Ref to user data# oldRMCred## Returns :#sub AddRoles{my ($data, $id, $url, $name, $passwd) = @_;my (@row);Message ("Add Roles: $data->{USER_NAME}");my $roleList = join(',', values (%{$data->{ROLE_MAP}}));return unless length($roleList) > 0;$ENV{GBE_RM_LOCATION} = $url;$ENV{GBE_RM_USERNAME} = $name;$ENV{GBE_RM_PASSWORD} = $passwd;connectRM(\$RM_DB);# First get details from pv_idmy $m_sqlstr = <<"SQL_END";BEGIN pk_Role.Grant_Role ( ':ROLE_ID_LIST', :USER_ID ); END;SQL_END$m_sqlstr =~ s~:ROLE_ID_LIST~$roleList~g;$m_sqlstr =~ s~:USER_ID~$data->{NEW_USER_ID}~g;my $sth = $RM_DB->prepare($m_sqlstr);if ( defined($sth) ){if ( $sth->execute( ) ){if ( $sth->rows ){while ( @row = $sth->fetchrow_array ){$data->{ROLE_MAP}{$row[1]} = $row[0];}}$sth->finish();}else{Error("Execute failure: $m_sqlstr", $sth->errstr() );}}else{Error("Prepare failure" );}disconnectRM(\$RM_DB);## Check that all the roles have been mapped#foreach my $role (keys %{$data->{ROLES}}) {unless (exists $data->{ROLE_MAP}{$role}) {ReportError("Role cannot be mapped: $role");}}ErrorDoExit();}#-------------------------------------------------------------------------------# Function : quoteList## Description : Convert an array of strings into a quoted comma-sep string# Used in sql of the form select ... in ( 'aaa','bbb',ccc')## Inputs : An array of strings## Returns : quoted comma-sep string#sub quoteList{my $rv = '';my $join = '';foreach (@_) {$rv .= $join . "'" . $_ . "'";$join = ',';}return $rv;}#-------------------------------------------------------------------------------# Documentation#=pod=for htmltoc GENERAL::ClearCase::=head1 NAMErmMerge_user_compare - Compare users in the Two Access Manager instances=head1 SYNOPSISjats eprog rmMerge_user_compare [options]Options:-help - brief help message-help -help - Detailed help message-man - Full documentation-add - Add missing users and to Unified Release Manager=head1 OPTIONS=over 8=item B<-help>Print a brief help message and exits.=item B<-help -help>Print a detailed help message with an explanation for each option.=back=head1 EXAMPLEjats eprog rmMerge_user_compare=cut