#! /usr/bin/perl -w use strict ; use Repocafe ; my $prog = substr($0,rindex($0,'/')+1) ; my $Usage = < option add : add admin option c : use config-file ---------------------------------------- - $prog - create/delete repocafe admins. - By default, $prog lists the current admins. - Normally, only the configured db_owner can do actual add/deletes, and database user db_user can only do SELECT and UPDATE on the repo_roots database table. However, if db_owner or db_opswd is empty or not configured, $prog uses db_user/db_pswd for the database-connection. - Before an admin is added, the specified admin login is looked up. ---------------------------------------- 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 add=s del=s c=s help) ) ; if ( $opt{help} ) { print $Usage ; exit 0 ; } Usage("Arg count\n") unless @ARGV == 0 ; # Blib -> debug ( 1 ) ; my $Cafe = make_cafe ( %opt, -conn => 0 ) ; my $conf = $Cafe -> conf ; my $roots = $Cafe -> roots ; sub trim { my $x = shift ; $x =~ s/[\s;]+$// ; $x =~ s/^\s+// ; $x ; } sub find_user { my $adm = shift ; my $rec = $roots -> select1 ( -where => "pid = '$adm'" ) ; $rec ? $rec -> pid : '' ; } sub add_user { my $adm = shift ; my $rec = $roots -> new_rec ( { pid => $adm, actv => 1 } ) ; if ( my $err = $rec -> _check ) { die $err ; } $rec -> _insert ; "DID add admin $adm (active)\n" ; } sub del_user { my $adm = shift ; my $rec = $roots -> select1 ( -where => "pid = '$adm'" ) ; die "del_user : user ($adm) not found\n" unless defined $rec ; $rec -> delete ; "DID delete admin $adm\n" ; } sub find_users { map { $_ -> { pid } } $roots -> select ( -order => 'pid' ) ; } sub verify_user { my $login = shift ; my $res = $Cafe -> lookups ( $login ) ; scalar @$res ; } my $db_owner = $conf -> db_owner ; my $db_opswd = $conf -> db_opswd ; Warn "db_owner empty or not configured" unless $db_owner ; Warn "db_opswd empty or not configured" unless $db_opswd ; my $ownr = $db_owner ; my $pswd = $db_opswd ; unless ( $pswd ) { $ownr = $conf -> db_user ; $pswd = $conf -> db_pswd ; Warn "using db_user, db_pswd for database connection" ; } $Cafe -> db_connect_as ( $ownr, $pswd ) ; printf "current admins:\n %s\n", ( join ', ', find_users ) || 'none' unless $opt{q} ; if ( $opt{add} ) { my $adm = $opt{add} ; unless ( verify_user $adm ) { printf "add : can't find user '$adm'\n" ; } elsif ( find_user $adm ) { printf "add : already have admin '$adm'\n" ; } elsif ( $opt{f} ) { print add_user $adm ; } else { print "WOULD add admin $adm\n" ; } } if ( $opt{del} ) { my $adm = $opt{del} ; unless ( find_user $adm ) { printf "del : can't find admin '$adm'\n" ; } elsif ( $opt{f} ) { print del_user $adm ; } else { print "WOULD delete admin $adm\n" ; } } printf "current admins:\n %s\n", ( join ', ', find_users ) || 'none' if ! $opt{q} and $opt{f} and ( $opt{add} or $opt{del} ) ;