#! /usr/bin/perl -w use strict ; use Repocafe ; my $TAB = 'repo_xusers' ; my $prog = substr($0,rindex($0,'/')+1) ; my $Usage = < ---------------------------------------- - Update the repo_xusers table. The table contains repocafe users that can't be found in the configured ldap(s) or table repo_guests. - The repo_xusers table in only used in the statistics page. - Because running $prog may take some time to complete, we suggest to run it daily by cron. - Cronjob : ( cd ; /usr/bin/perl upd_xusers -f ) ---------------------------------------- USAGE sub Usage { die "$_[0]$Usage" ; } sub Error { die "$prog: $_[0]\n" ; } sub Warn { warn "$prog: $_[0]\n" ; } # usage: &GetOptions(ARG,ARG,..) defines $opt_ID as 1 or user spec'ed value # usage: &GetOptions(\%opt,ARG,ARG,..) defines $opt{ID} as 1 or user value # ARG = 'ID' | 'ID=SPC' | 'ID:SPC' for no-arg, required-arg or optional-arg # ID = perl identifier # SPC = i|f|s for integer, fixedpoint real or string argument use Getopt::Long ; Getopt::Long::config('no_ignore_case') ; our %opt = () ; Usage('') unless GetOptions ( \%opt, qw(v q d f l help) ) ; if ( $opt{help} ) { print $Usage ; exit 0 ; } # Usage("Arg count\n") unless @ARGV == 0 ; Repocafe::set_opt ( %opt ) ; sub restrict { my $hash = shift ; my %keys = () ; for my $key ( @_ ) { $keys { $key } ++ ; } my $res = {} ; for my $key ( keys %$hash ) { $res -> { $key } = $hash -> { $key } if exists $keys { $key } ; } return $res ; } sub add_xuser { my $db = shift ; my $usr = shift ; my $tag = 'WOULD' ; if ( $opt{f} ) { $tag = 'DID' ; my $dbh = $db -> dbh ; my $sql = "INSERT INTO $TAB (pid) VALUES ( ? )" ; my $sth = $dbh -> prepare ( $sql ) ; $sth -> execute ( $usr ) ; } print "$tag add xuser $usr\n" unless $opt{q} ; } sub del_xuser { my $db = shift ; my $usr = shift ; my $tag = 'WOULD' ; if ( $opt{f} ) { $tag = 'DID' ; my $dbh = $db -> dbh ; my $sql = "DELETE FROM $TAB where pid = ?" ; my $sth = $dbh -> prepare ( $sql ) ; $sth -> execute ( $usr ) ; } print "$tag delete xuser $usr\n" unless $opt{q} ; } my $Cafe = Repocafe -> new () ; my $conf = $Cafe -> conf ; my $db = $Cafe -> db ; my $logins = {} ; my $prefix = $conf -> db_pref ; $TAB = $prefix . $TAB ; my $repos = $Cafe -> repos -> get_by_qwe ( '' ) ; my $groups = $Cafe -> groups -> get_by_qwe ( '' ) ; my $members = $Cafe -> gr_mem -> get_by_qwe ( '' ) ; my $xusers = $Cafe -> xusers -> get_by_qwe ( '' ) ; my $grp4gid = {} ; for my $group ( @$groups ) { $grp4gid -> { $group -> gid } = $group ; } for my $repo ( @$repos ) { my $name = $repo -> repo_name ; push @{ $logins -> { $repo -> ownr } }, "owner of repo $name" ; for my $pid ( keys %{ $repo -> rights } ) { push @{ $logins -> { $pid } }, "rights in repo $name" unless $pid =~ /^@/ ; } } for my $mem ( @$members ) { my $pid = $mem -> pid ; my $gid = $mem -> gid ; my $nam = $grp4gid -> { $gid } -> name ; push @{ $logins -> { $pid } }, "member of $nam" unless $pid =~ /^@/ ; } if ( @ARGV ) { $logins = restrict $logins, @ARGV ; $xusers = restrict $xusers, @ARGV ; } my $users = $Cafe -> lookups ( sort keys %$logins ) ; my %users ; for my $user ( @$users ) { $users { $user -> login } = $user ; } if ( $opt{l} ) { if ( $opt{v} ) { for my $login ( sort keys %$logins ) { printf "login %s - clas '%s' :\n %s\n", , $login , ( exists $users { $login } ? $users { $login } -> clas : 'unknown user' ) , ( join "\n ", @{ $logins -> { $login } } ) ; } } else { for my $xuser ( sort keys %$xusers ) { printf "%s\n", $xuser ; } } exit 0 ; } for my $login ( sort keys %$logins ) { add_xuser $db, $login unless exists $users { $login } or exists $xusers -> { $login } ; } for my $xuser ( sort keys %$xusers ) { del_xuser $db, $xuser if exists $users { $xuser } or ! exists $logins -> { $xuser } ; }