#!/usr/bin/perl -w ##################################################### # sync-roles-in-sites.pl # ----------------------- # originally by Steve Swinsburg (s.swinsburg@lancaster.ac.uk) # # A script for synchronising roles and permissions # from a template site to sites. # # Use of this script is entirely at your own risk. # Run test-connection.pl first if you are not sure. # # REQUIREMENTS: # 1. web services enabled on your Sakai box. # 2. SOAP::List perl Module (perl -MCPAN -e 'install SOAP::Lite') # ##################################################### use SOAP::Lite; use Getopt::Std; my %opt; getopts( 'hH:p:P:su:v', \%opt ) or die "Cannot parse command line\n"; ##################### EDIT HERE ##################### $opt{h} and HELP_MESSAGE(); my $user = $opt{u} // $ENV{SAKAI_USER} // ask('username'); my $password = $opt{p} // $ENV{SAKAI_PASSWORD} // ask( 'password', '*' ); my $host = $opt{H} // $ENV{SAKAI_HOST} // ask('host'); my $s = ( $opt{s} // $ENV{SAKAI_HTTPS} // 0 ) ? 's' : ''; my $port = $opt{P} // $ENV{SAKAI_PORT} // ( $s ? 443 : 80 ); my $siteURI = "http$s://$host:$port"; my $loginURI = "$siteURI/sakai-axis/SakaiLogin.jws?wsdl"; my $scriptURI = "$siteURI/sakai-axis/SakaiScript.jws?wsdl"; my $templateSite = "!site.template"; my @toSites = ( #"/site/05326ef9-9e64-4dd9-abab-19fa06cb5890", ); my @roles = ( "access", "maintain", "readonly", "privileged" ); ############## DO NOT EDIT BELOW HERE ############### do_it(); exit(); sub HELP_MESSAGE { warn "Read the source code for help. Enjoy.\n"; exit(0); } sub oomph { push( @_, '(' . ( $@ =~ s/\s+$//r ), ')' ) if $@; die "$0: fatal error: ", join( ' ', @_ ), "\n"; } sub ehm { warn join( ' ', @_ ), "\n" if $opt{v}; } sub ask # copied from https://svn.win.tue.nl/repos/home-rp/bin/picasaweb { require Term::ReadKey or oomph('cannot ask arguments interactively, install Term::ReadKey'); Term::ReadKey->import; my ( $prompt, $repl ) = @_; open( my $out_fh, '>', '/dev/tty' ) or oomph("cannot write to tty: $!"); open( my $in_fh, '<', '/dev/tty' ) or oomph("cannot read from tty: $!"); my $stdout = *STDOUT; select($out_fh); local $| = 1; # Turn off STDOUT buffering for immediate response print "\u$prompt: "; ReadMode( 4, $in_fh ); # Change to Raw Mode, disable Ctrl-C my $key; my $pword; while (1) { while ( !defined( $key = ReadKey( -1, $in_fh ) ) ) { } if ( $key =~ /^[\r\n]/ ) { # if Enter was pressed... print $key; # print a newline last; # and get out of here } print STDOUT ( $repl // $key ); $pword .= $key; } ReadMode( 0, $in_fh ); # Reset tty mode before exiting. <==IMPORTANT close($in_fh); select($stdout); $pword or oomph( 'you must supply a', $prompt ); $pword; } sub do_it { my $loginsoap = SOAP::Lite->proxy($loginURI)->uri($loginURI); my $scriptsoap = SOAP::Lite->proxy($scriptURI)->uri($scriptURI); my $session = eval { $loginsoap->login( $user, $password )->result } or oomph("cannot login as $user to $loginURI"); print "session is: " . $session . "\n"; foreach $site (@toSites) { print "processing site: " . $site . "\n"; foreach $role (@roles) { print "\t synchronising role: " . $role . "..."; my $result = $scriptsoap->copyRole( $session, $templateSite, $site, $role )->result; print "$result \n"; } } # logout my $logout = $loginsoap->logout($session)->result; print "logging out: " . $logout . "\n"; #END print "\n"; }