Subversion Repositories DevTools

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
6887 dpurdie 1
########################################################################
2
# COPYRIGHT - VIX IP PTY LTD ("VIX"). ALL RIGHTS RESERVED.
3
#
4
# Module name   : rmMerge_process.pl
5
# Module type   : JATS Utility
6
# Compiler(s)   : Perl
7
# Environment(s): jats
8
#
9
# Description   : Compare two sets of users 
10
#
11
# Usage         : See POD at the end of this file
12
#
13
#......................................................................#
14
 
15
require 5.008_002;
16
use strict;
17
use warnings;
18
 
19
use Pod::Usage;
20
use Getopt::Long;
21
 
22
use JatsError;
23
use JatsRmApi;
24
use JatsSystem;
25
use FileUtils;
26
use ConfigurationFile;
27
use File::Copy;
28
use DBI;
29
my $RM_DB;
30
 
31
my $opt_help=0;
32
my $opt_verbose=0;
33
my $opt_debug=0;
34
my $opt_add = 0;
35
 
36
my $VERSION = "1.0";
37
my @oldRMCred = ('OLD', 'jdbc:oracle:thin:@auawsards001:1521:RELEASEM', 'ACCESS_MANAGER', 'skdf0fdfdi');
38
my @newRMCred = ('NEW', 'jdbc:oracle:thin:@auawsards002:1521:RELEASEM', 'RM_READONLY', 'Tp8WmmDKMq2Z');
39
 
40
my %oldData;
41
my %newData;
42
 
43
#-------------------------------------------------------------------------------
44
# Function        : Mainline Entry Point
45
#
46
# Description     :
47
#
48
# Inputs          :
49
#
50
my $result = GetOptions (
51
                "help:+"        => \$opt_help,
52
                "manual:3"      => \$opt_help,
53
                "verbose:+"     => \$opt_verbose,
54
                "debug:+"       => \$opt_debug,
55
                "add"           => \$opt_add,
56
                );
57
 
58
                #
59
                #   UPDATE THE DOCUMENTATION AT THE END OF THIS FILE !!!
60
                #
61
 
62
#
63
#   Process help and manual options
64
#
65
pod2usage(-verbose => 0, -message => "Version: $VERSION") if ($opt_help == 1 || ! $result);
66
pod2usage(-verbose => 1) if ( $opt_help == 2 );
67
pod2usage(-verbose => 2) if ( $opt_help > 2 );
68
#pod2usage(-verbose => 0, -message => "Version: $VERSION") if ( $#ARGV < 0 );
69
 
70
#
71
#   Configure the error reporting rmMerge_process now that we have the user options
72
#
73
ErrorConfig( 'name'    =>'RELCMP',
74
             'verbose' => $opt_verbose,
75
             'debug' => $opt_debug,
76
            );
77
 
78
#
79
#   Determine matching Release Names
80
#
81
getReleaseData( \%oldData, @oldRMCred);
82
getReleaseData( \%newData, @newRMCred);
83
 
84
#DebugDumpData("oldData", \%oldData);
85
#DebugDumpData("newData", \%newData);
86
 
87
#
88
#   Report differences
89
#
90
 
91
#   Don't care about these
92
#foreach my $uname ( sort keys %oldData) {
93
#        next if exists $newData{$uname};
94
#        print("Missing in new: $uname, $oldData{$uname}{FULL_NAME}, $oldData{$uname}{USER_EMAIL}, $oldData{$uname}{IS_DISABLED}\n");
95
#}
96
 
97
foreach my $uname ( sort keys %newData) {
98
        #next if exists $oldData{$uname};
99
        #print("Missing in old: $uname, $newData{$uname}{FULL_NAME}, $newData{$uname}{USER_EMAIL}, $newData{$uname}{DOMAIN}, $newData{$uname}{IS_DISABLED}\n");
100
        print("Processing: $uname, $newData{$uname}{FULL_NAME}, $newData{$uname}{USER_EMAIL}, $newData{$uname}{DOMAIN}, $newData{$uname}{IS_DISABLED}\n");
101
        if ($opt_add) {
102
            my $rv = getUserRoles ($newData{$uname}, @newRMCred);
103
            next unless $rv;
104
            getRoleMap ($newData{$uname}, @oldRMCred);
105
            addUser($newData{$uname}, @oldRMCred) unless exists $oldData{$uname};
106
            getUserId($newData{$uname}, @oldRMCred);
107
            AddRmAppliction($newData{$uname}, @oldRMCred);
108
            AddRoles($newData{$uname}, @oldRMCred);
109
DebugDumpData("Data", $newData{$uname});
110
        }
111
}
112
 
113
foreach my $uname ( sort keys %oldData) {
114
        next unless exists $newData{$uname};
115
        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};
116
        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");
117
}
118
 
119
#-------------------------------------------------------------------------------
120
# Function        :  addUser
121
#
122
# Description     : Add a user to a Release Manager instance
123
#
124
# Inputs          : $data - Ref to user data
125
#                   oldRMCred 
126
#
127
# Returns         : 
128
#
129
sub addUser
130
{
131
    my ($data, $id, $url, $name, $passwd) = @_;
132
 
133
    my (@row);
134
 
135
    Message ("Add: $data->{USER_NAME}");
136
 
137
    $ENV{GBE_RM_LOCATION} = $url;
138
    $ENV{GBE_RM_USERNAME} = $name;
139
    $ENV{GBE_RM_PASSWORD} = $passwd;
140
 
141
    connectRM(\$RM_DB);
142
 
143
    # First get details from pv_id
144
 
145
    my $m_sqlstr = <<"SQL_END";
146
        BEGIN PK_USER.ADD_USER_ACCOUNT ( :FULL_NAME, :USER_NAME, :USER_EMAIL, :DOMAIN ); END;
147
SQL_END
148
    $m_sqlstr =~ s~:FULL_NAME~'$data->{FULL_NAME}'~g;
149
    $m_sqlstr =~ s~:USER_NAME~'$data->{USER_NAME}'~g;
150
    $m_sqlstr =~ s~:USER_EMAIL~'$data->{USER_EMAIL}'~g;
151
    $m_sqlstr =~ s~:DOMAIN~'$data->{DOMAIN}'~g;
152
 
153
    my $sth = $RM_DB->prepare($m_sqlstr);
154
    if ( defined($sth) )
155
    {
156
        if ( $sth->execute( ) )
157
        {
158
            if ( $sth->rows )
159
            {
160
                while ( @row = $sth->fetchrow_array )
161
                {
162
                }
163
            }
164
            $sth->finish();
165
        }
166
        else
167
        {
168
            Error("Execute failure: $m_sqlstr", $sth->errstr() );
169
        }
170
    }
171
    else
172
    {
173
        Error("Prepare failure" );
174
    }
175
 
176
    disconnectRM(\$RM_DB);
177
}
178
 
179
#-------------------------------------------------------------------------------
180
# Function        : getReleaseData 
181
#
182
# Description     : Get Release Content and related data for a release
183
#
184
# Inputs          : dataRef
185
#                   RmCredentails 
186
#
187
# Returns         : 
188
#
189
sub getReleaseData
190
{
191
    my ($dataRef, $id, $url, $name, $passwd) = @_;
192
 
193
    my (@row);
194
 
195
    Message ("Extract configured users");
196
 
197
    $ENV{GBE_RM_LOCATION} = $url;
198
    $ENV{GBE_RM_USERNAME} = $name;
199
    $ENV{GBE_RM_PASSWORD} = $passwd;
200
 
201
    connectRM(\$RM_DB);
202
 
203
    # First get details from pv_id
204
 
205
    my $m_sqlstr = <<"SQL_END";
206
        SELECT USER_NAME, FULL_NAME, USER_EMAIL, IS_DISABLED, DOMAIN, USER_ID FROM USERS
207
SQL_END
208
    my $sth = $RM_DB->prepare($m_sqlstr);
209
    if ( defined($sth) )
210
    {
211
        if ( $sth->execute( ) )
212
        {
213
            if ( $sth->rows )
214
            {
215
                while ( @row = $sth->fetchrow_array )
216
                {
217
                    $dataRef->{$row[0]}{USER_NAME} =$row[0] || '';
218
                    $dataRef->{$row[0]}{FULL_NAME} =$row[1] || '';
219
                    $dataRef->{$row[0]}{USER_EMAIL} =$row[2] || '';
220
                    $dataRef->{$row[0]}{IS_DISABLED} = $row[3] || '';
221
                    $dataRef->{$row[0]}{DOMAIN} = $row[4] || '';
222
                    $dataRef->{$row[0]}{USER_ID} = $row[5] || '';
223
                }
224
            }
225
            $sth->finish();
226
        }
227
        else
228
        {
229
            Error("Execute failure: $m_sqlstr", $sth->errstr() );
230
        }
231
    }
232
    else
233
    {
234
        Error("Prepare failure" );
235
    }
236
 
237
    disconnectRM(\$RM_DB);
238
}
239
 
240
#-------------------------------------------------------------------------------
241
# Function        : getUserRoles 
242
#
243
# Description     : Get a users roles - only within Release Manager
244
#
245
# Inputs          : 
246
#
247
# Returns         : true - has needed role
248
#
249
sub getUserRoles
250
{
251
    my ($dataRef, $id, $url, $name, $passwd) = @_;
252
    my $rv;
253
 
254
    my (@row);
255
 
256
    Message ("Extract user roles");
257
    unless ($dataRef->{USER_ID}){
258
        Warning("No user ID");
259
        return;
260
    }
261
 
262
    $ENV{GBE_RM_LOCATION} = $url;
263
    $ENV{GBE_RM_USERNAME} = $name;
264
    $ENV{GBE_RM_PASSWORD} = $passwd;
265
 
266
    connectRM(\$RM_DB);
267
 
268
    # First get details from pv_id
269
 
270
    my $m_sqlstr = <<"SQL_END";
271
        SELECT
272
            r.role_name, r.role_id
273
        FROM
274
            user_roles ur,
275
            roles r
276
        WHERE
277
            ur.user_id = :USER_ID
278
            AND r.role_id = ur.role_id
279
            AND r.role_id = 1741
280
            AND app_id = 2
281
            AND r.is_role_variation IS NULL
282
SQL_END
283
    $m_sqlstr =~ s~:USER_ID~$dataRef->{USER_ID}~g;
284
    my $sth = $RM_DB->prepare($m_sqlstr);
285
    if ( defined($sth) )
286
    {
287
        if ( $sth->execute( ) )
288
        {
289
            if ( $sth->rows )
290
            {
291
                while ( @row = $sth->fetchrow_array )
292
                {
293
                   $dataRef->{ROLES}{$row[0]} = $row[1];
294
                   $rv = 1;
295
                }
296
            }
297
            $sth->finish();
298
        }
299
        else
300
        {
301
            Error("Execute failure: $m_sqlstr", $sth->errstr() );
302
        }
303
    }
304
    else
305
    {
306
        Error("Prepare failure" );
307
    }
308
 
309
    disconnectRM(\$RM_DB);
310
    return $rv;
311
}
312
 
313
#-------------------------------------------------------------------------------
314
# Function        : getUserId 
315
#
316
# Description     : Get a user id
317
#
318
# Inputs          : 
319
#
320
# Returns         : 
321
#
322
sub getUserId
323
{
324
    my ($dataRef, $id, $url, $name, $passwd) = @_;
325
 
326
    my (@row);
327
 
328
    Message ("Determine new user id");
329
 
330
    $ENV{GBE_RM_LOCATION} = $url;
331
    $ENV{GBE_RM_USERNAME} = $name;
332
    $ENV{GBE_RM_PASSWORD} = $passwd;
333
 
334
    connectRM(\$RM_DB);
335
 
336
    # First get details from pv_id
337
 
338
    my $m_sqlstr = <<"SQL_END";
339
        SELECT
340
            user_id
341
        FROM
342
            users
343
        WHERE
344
            user_name = :USER_NAME
345
SQL_END
346
    $m_sqlstr =~ s~:USER_NAME~'$dataRef->{USER_NAME}'~g;
347
    my $sth = $RM_DB->prepare($m_sqlstr);
348
    if ( defined($sth) )
349
    {
350
        if ( $sth->execute( ) )
351
        {
352
            if ( $sth->rows )
353
            {
354
                while ( @row = $sth->fetchrow_array )
355
                {
356
                    $dataRef->{NEW_USER_ID} = $row[0];
357
                }
358
            }
359
            $sth->finish();
360
        }
361
        else
362
        {
363
            Error("Execute failure: $m_sqlstr", $sth->errstr() );
364
        }
365
    }
366
    else
367
    {
368
        Error("Prepare failure" );
369
    }
370
 
371
    disconnectRM(\$RM_DB);
372
    Message ("New User id: $dataRef->{NEW_USER_ID}");
373
}
374
#-------------------------------------------------------------------------------
375
#
376
# Description     : Add a user to a Release Manager instance
377
#
378
# Inputs          : $data - Ref to user data
379
#                   oldRMCred 
380
#
381
# Returns         : 
382
#
383
sub AddRmAppliction
384
{
385
    my ($data, $id, $url, $name, $passwd) = @_;
386
 
387
    my (@row);
388
 
389
    Message ("Add to Rm Appliaction: $data->{USER_NAME}");
390
 
391
    $ENV{GBE_RM_LOCATION} = $url;
392
    $ENV{GBE_RM_USERNAME} = $name;
393
    $ENV{GBE_RM_PASSWORD} = $passwd;
394
 
395
    connectRM(\$RM_DB);
396
 
397
    # First get details from pv_id
398
 
399
    my $m_sqlstr = <<"SQL_END";
400
        BEGIN pk_User.Add_Application_User ( :USER_ID_LIST, :APP_ID, :INCLUDE_EVERYONE ); END;
401
SQL_END
402
    $m_sqlstr =~ s~:USER_ID_LIST~'$data->{NEW_USER_ID}'~g;
403
    $m_sqlstr =~ s~:APP_ID~2~g;
404
    $m_sqlstr =~ s~:INCLUDE_EVERYONE~null~g;
405
 
406
    my $sth = $RM_DB->prepare($m_sqlstr);
407
    if ( defined($sth) )
408
    {
409
        if ( $sth->execute( ) )
410
        {
411
            if ( $sth->rows )
412
            {
413
                while ( @row = $sth->fetchrow_array )
414
                {
415
                }
416
            }
417
            $sth->finish();
418
        }
419
        else
420
        {
421
            Error("Execute failure: $m_sqlstr", $sth->errstr() );
422
        }
423
    }
424
    else
425
    {
426
        Error("Prepare failure" );
427
    }
428
 
429
    disconnectRM(\$RM_DB);
430
}
431
 
432
#-------------------------------------------------------------------------------
433
#
434
# Description     : Get role map
435
#
436
# Inputs          : $data - Ref to user data
437
#                   oldRMCred 
438
#
439
# Returns         : 
440
#
441
sub getRoleMap
442
{
443
    my ($data, $id, $url, $name, $passwd) = @_;
444
 
445
    my (@row);
446
 
447
    Message ("Map roles: $data->{USER_NAME}");
448
    my $roleList = quoteList(keys %{$data->{ROLES}});
449
    return unless length($roleList) > 0;
450
 
451
    $ENV{GBE_RM_LOCATION} = $url;
452
    $ENV{GBE_RM_USERNAME} = $name;
453
    $ENV{GBE_RM_PASSWORD} = $passwd;
454
 
455
    connectRM(\$RM_DB);
456
 
457
    # First get details from pv_id
458
 
459
    my $m_sqlstr = <<"SQL_END";
460
        select role_id, role_name from roles r where role_name in ( :ROLE_NAME_LIST ) and app_id = :APP_ID
461
SQL_END
462
    $m_sqlstr =~ s~:ROLE_NAME_LIST~$roleList~g;
463
    $m_sqlstr =~ s~:APP_ID~2~g;
464
 
465
    my $sth = $RM_DB->prepare($m_sqlstr);
466
    if ( defined($sth) )
467
    {
468
        if ( $sth->execute( ) )
469
        {
470
            if ( $sth->rows )
471
            {
472
                while ( @row = $sth->fetchrow_array )
473
                {
474
                    $data->{ROLE_MAP}{$row[1]} = $row[0];
475
                }
476
            }
477
            $sth->finish();
478
        }
479
        else
480
        {
481
            Error("Execute failure: $m_sqlstr", $sth->errstr() );
482
        }
483
    }
484
    else
485
    {
486
        Error("Prepare failure" );
487
    }
488
 
489
    disconnectRM(\$RM_DB);
490
 
491
    #
492
    #   Check that all the roles have been mapped
493
    #
494
    foreach my $role (keys %{$data->{ROLES}}) {
495
        unless (exists $data->{ROLE_MAP}{$role}) {
496
            ReportError("Role cannot be mapped: $role");
497
        }
498
    }
499
    ErrorDoExit();
500
}
501
 
502
#-------------------------------------------------------------------------------
503
#
504
# Description     : Get role map
505
#
506
# Inputs          : $data - Ref to user data
507
#                   oldRMCred 
508
#
509
# Returns         : 
510
#
511
sub AddRoles
512
{
513
    my ($data, $id, $url, $name, $passwd) = @_;
514
 
515
    my (@row);
516
 
517
    Message ("Add Roles: $data->{USER_NAME}");
518
    my $roleList = join(',', values (%{$data->{ROLE_MAP}}));
519
    return unless length($roleList) > 0;
520
 
521
    $ENV{GBE_RM_LOCATION} = $url;
522
    $ENV{GBE_RM_USERNAME} = $name;
523
    $ENV{GBE_RM_PASSWORD} = $passwd;
524
 
525
    connectRM(\$RM_DB);
526
 
527
    # First get details from pv_id
528
 
529
    my $m_sqlstr = <<"SQL_END";
530
        BEGIN pk_Role.Grant_Role ( ':ROLE_ID_LIST', :USER_ID ); END;
531
SQL_END
532
    $m_sqlstr =~ s~:ROLE_ID_LIST~$roleList~g;
533
    $m_sqlstr =~ s~:USER_ID~$data->{NEW_USER_ID}~g;
534
 
535
    my $sth = $RM_DB->prepare($m_sqlstr);
536
    if ( defined($sth) )
537
    {
538
        if ( $sth->execute( ) )
539
        {
540
            if ( $sth->rows )
541
            {
542
                while ( @row = $sth->fetchrow_array )
543
                {
544
                    $data->{ROLE_MAP}{$row[1]} = $row[0];
545
                }
546
            }
547
            $sth->finish();
548
        }
549
        else
550
        {
551
            Error("Execute failure: $m_sqlstr", $sth->errstr() );
552
        }
553
    }
554
    else
555
    {
556
        Error("Prepare failure" );
557
    }
558
 
559
    disconnectRM(\$RM_DB);
560
 
561
    #
562
    #   Check that all the roles have been mapped
563
    #
564
    foreach my $role (keys %{$data->{ROLES}}) {
565
        unless (exists $data->{ROLE_MAP}{$role}) {
566
            ReportError("Role cannot be mapped: $role");
567
        }
568
    }
569
    ErrorDoExit();
570
}
571
 
572
#-------------------------------------------------------------------------------
573
# Function        : quoteList  
574
#
575
# Description     : Convert an array of strings into a quoted comma-sep string
576
#                   Used in sql of the form select ... in ( 'aaa','bbb',ccc') 
577
#
578
# Inputs          : An array of strings 
579
#
580
# Returns         : quoted comma-sep string
581
#
582
 
583
sub quoteList
584
{
585
    my $rv = '';
586
    my $join = '';
587
    foreach  (@_) {
588
        $rv .= $join . "'" . $_ . "'";
589
        $join = ',';
590
    }
591
    return $rv;
592
}
593
 
594
#-------------------------------------------------------------------------------
595
#   Documentation
596
#
597
 
598
=pod
599
 
600
=for htmltoc    GENERAL::ClearCase::
601
 
602
=head1 NAME
603
 
604
rmMerge_user_compare - Compare users in the Two Access Manager instances
605
 
606
=head1 SYNOPSIS
607
 
608
jats eprog rmMerge_user_compare [options]
609
 
610
 Options:
611
    -help              - brief help message
612
    -help -help        - Detailed help message
613
    -man               - Full documentation
614
    -add               - Add missing users and to Unified Release Manager
615
 
616
=head1 OPTIONS
617
 
618
=over 8
619
 
620
=item B<-help>
621
 
622
Print a brief help message and exits.
623
 
624
=item B<-help -help>
625
 
626
Print a detailed help message with an explanation for each option.
627
 
628
=back
629
 
630
=head1 EXAMPLE
631
 
632
jats eprog rmMerge_user_compare
633
 
634
=cut
635
 
636
 
637