chronos
view release on metacpan or search on metacpan
scripts/chronosadmin view on Meta::CPAN
#
# Chronos is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# Chronos is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Foobar; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
use strict;
use Getopt::Long;
use Chronos::Static qw(gettext lang get);
my $text = gettext( '', 1 );
my ( $db_user, $db_pass, $db, $autodel, $new_user, $db_pass, $new_users_batch,
$deluser );
GetOptions(
'help' => \&usage,
'new-user' => \$new_user,
'u=s' => \$db_user,
'p:s' => \$db_pass,
'new-users-batch:s' => \$new_users_batch,
'autodel!' => \$autodel,
'del-user=s' => \$deluser,
'show-holidays' => \&holidays,
)
or usage();
$db = shift || 'chronos';
db_pass($db_pass) if defined $db_pass;
my $dbh = dbh();
print "$text->{chronosadmin_connectok}\n";
new_user() if $new_user;
new_users_batch($new_users_batch) if defined $new_users_batch;
del_user($deluser) if $deluser;
sub db_pass {
$db_pass = shift
|| get( $text->{chronosadmin_p}, { Password => 1, NoConfirm => 1 } );
}
sub usage {
printf $text->{chronosadmin_usage}, $0;
}
{
my $dbh;
sub dbh {
if ( not $dbh ) {
$dbh =
DBI->connect(
"dbi:mysql:$db;mysql_read_default_file=$ENV{HOME}/.my.cnf",
$db_user, $db_pass, { RaiseError => 1, PrintError => 0 } );
}
return $dbh;
}
}
sub new_user {
my $username = get( $text->{chronosadmin_username} );
my $password = get( $text->{chronosadmin_pass}, { Password => 1 } );
my $email = get( $text->{chronosadmin_email} );
my @langs = grep { -f } </usr/share/chronos/lang/*>;
s|/usr/share/chronos/lang/|| foreach @langs;
my $lang =
get( $text->{chronosadmin_lang}, { Default => lang(), Enum => \@langs } );
my $name = get( $text->{chronosadmin_realname} );
newuser( $username, $password, $email, $lang, $name );
}
sub newuser {
my ( $username, $password, $email, $lang, $name ) = @_;
my $username_quoted = $dbh->quote($username);
my $password_quoted = $dbh->quote($password);
my $email_quoted = $dbh->quote($email);
my $name_quoted = $dbh->quote($name);
if (
$dbh->selectrow_array(
"SELECT user FROM user WHERE user = $username_quoted"
)
)
{
if ($autodel) {
del_user($username);
} else {
printf "$text->{chronosadmin_exists} [N/y] ", $username;
if ( <STDIN> =~ /^y/i ) {
del_user($username);
} else {
return 0;
}
}
}
$dbh->do(
"INSERT INTO user (user, password, email, lang, name) VALUES($username_quoted, PASSWORD($password_quoted), $email_quoted, '$lang', $name_quoted)"
);
printf "$text->{chronosadmin_created}\n", $username;
}
sub del_user {
my $username = shift or die "I need a username\n";
my $username_quoted = $dbh->quote($username);
$dbh->do("DELETE FROM user WHERE user = $username_quoted");
my $sth =
$dbh->prepare(
"SELECT eid FROM events WHERE initiator = $username_quoted");
$sth->execute;
while ( my $eid = $sth->fetchrow_array ) {
( run in 2.048 seconds using v1.01-cache-2.11-cpan-ecdf5575e8d )