diff options
author | Christian Pointner <equinox@spreadspace.org> | 2015-09-17 21:57:15 (GMT) |
---|---|---|
committer | Christian Pointner <equinox@spreadspace.org> | 2015-09-17 21:57:15 (GMT) |
commit | 66557641d3fb28103df0a947552993e5827320aa (patch) | |
tree | dc2395a7ea5c358480e1abfccf6fcc7c19b6a4c1 | |
parent | 08cfff490aec608049b031cc148ad89ee5543ea4 (diff) |
added group handling support
-rw-r--r-- | MANIFEST | 1 | ||||
-rw-r--r-- | META.yml | 2 | ||||
-rw-r--r-- | Makefile.PL | 2 | ||||
-rw-r--r-- | lib/RHRD.pm | 2 | ||||
-rwxr-xr-x | lib/RHRD/rddb.pm | 197 | ||||
-rwxr-xr-x | utils/rd-group | 108 |
6 files changed, 309 insertions, 3 deletions
@@ -8,5 +8,6 @@ META.yml utils/get-rd-token utils/get-rd-week utils/rd-user +utils/rd-group utils/rhrd-ws-login utils/update-rd-tokens @@ -23,4 +23,4 @@ requires: POSIX: '0' DateTime: '0' DateTime::TimeZone: '0' -version: '0.2' +version: '0.6' diff --git a/Makefile.PL b/Makefile.PL index 3796727..27d40d6 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -1,7 +1,7 @@ use ExtUtils::MakeMaker; use 5.004; -my @utils = qw(get-rd-token rhrd-ws-login get-rd-week update-rd-tokens rd-user); +my @utils = qw(get-rd-token rhrd-ws-login get-rd-week update-rd-tokens rd-user rd-group); WriteMakefile( NAME => 'RHRD', diff --git a/lib/RHRD.pm b/lib/RHRD.pm index 859641f..ce8bb84 100644 --- a/lib/RHRD.pm +++ b/lib/RHRD.pm @@ -26,6 +26,6 @@ use strict; use vars qw($VERSION); use 5.004; -$VERSION = "0.2" +$VERSION = "0.6" ########################################################################## diff --git a/lib/RHRD/rddb.pm b/lib/RHRD/rddb.pm index ef7740d..b7d0698 100755 --- a/lib/RHRD/rddb.pm +++ b/lib/RHRD/rddb.pm @@ -26,6 +26,8 @@ use strict; use Config::IniFiles; use DBI; +########################### connection handling ########################### + sub opendb { my $RD_CONF = "/etc/rd.conf"; # TODO: hardcoded value @@ -52,6 +54,8 @@ sub closedb $dbh->disconnect(); } +########################### TOKEN handling ########################### + sub get_token { my ($dbh, $username) = @_; @@ -116,6 +120,8 @@ sub check_token return (0, 'ERROR', "wrong password"); } +########################### USER handling ########################### + sub add_user { my ($dbh, $username, $token, $fullname) = @_; @@ -249,6 +255,197 @@ sub get_users return @users; } +########################### GROUP handling ########################### + +sub add_group +{ + my ($dbh, $groupname, $description) = @_; + if(!defined $description) { + $description = ''; + } + + my $sql = qq{insert into GROUPS (NAME, DESCRIPTION) values (?, ?);}; + my $sth = $dbh->prepare($sql) + or return (undef, 'ERROR', "Database Error: " . $dbh->errstr); + + my $cnt = $sth->execute($groupname, $description) + or return (undef, 'ERROR', "Database Error: " . $sth->errstr); + + $sth->finish(); + return ($cnt, 'OK', "success"); +} + +sub remove_group +{ + my ($dbh, $groupname) = @_; + + my @actions = ({ + # Delete Member Carts + sql => qq{delete from CART where GROUP_NAME = ?;}, + name => 'member carts', + cnt => 0 + }, { + # Delete Dropboxes + sql => qq{delete from DROPBOXES where GROUP_NAME = ?;}, + name => 'dropboxes', + cnt => 0 + }, { + # Delete Audio Perms + sql => qq{delete from AUDIO_PERMS where GROUP_NAME = ?;}, + name => 'service permissions', + cnt => 0 + }, { + # Delete Member User Perms + sql => qq{delete from USER_PERMS where GROUP_NAME = ?;}, + name => 'user assignments', + cnt => 0 + }, { + # Delete Replicator Map Records + sql => qq{delete from REPLICATOR_MAP where GROUP_NAME = ?;}, + name => 'replicator map records', + cnt => 0 + }, { + # Delete from Group List + sql => qq{delete from GROUPS where NAME = ?;}, + name => 'group entries', + cnt => 0 + }); + + for my $href (@actions) { + my $sth = $dbh->prepare($href->{sql}) + or return (undef, 'ERROR', "Database Error: " . $dbh->errstr); + delete($href->{sql}); + + $href->{cnt} = $sth->execute($groupname) + or return (undef, 'ERROR', "Database Error: " . $sth->errstr); + + $sth->finish(); + } + + return @actions; +} + +sub check_group +{ + my ($dbh, $groupname) = @_; + + my $sql = qq{select count(*) from GROUPS where NAME = ?;}; + my $sth = $dbh->prepare($sql) + or return (undef, 'ERROR', "Database Error: " . $dbh->errstr); + + $sth->execute($groupname) + or return (undef, 'ERROR', "Database Error: " . $sth->errstr); + + my ($cnt) = $sth->fetchrow_array(); + $sth->finish(); + + if ($cnt != 0) { + $sql = qq{select count(*) from CART where GROUP_NAME = ?;}; + my $sth = $dbh->prepare($sql) + or return (undef, 'ERROR', "Database Error: " . $dbh->errstr); + + $sth->execute($groupname) + or return (undef, 'ERROR', "Database Error: " . $sth->errstr); + + ($cnt) = $sth->fetchrow_array(); + $sth->finish(); + + if($cnt) { return (2, 'OK', "group '" . $groupname . "' exists and has carts assigned to it"); } + else { return (1, 'OK', "group '" . $groupname . "' exists but no cart is assigned to it"); } + } + + return (0, 'OK', "group '" . $groupname . "' does not exist"); +} + +sub get_group_carts +{ + my ($dbh, $groupname) = @_; + + my $sql = qq{select DEFAULT_LOW_CART, DEFAULT_HIGH_CART, DEFAULT_CART_TYPE, ENFORCE_CART_RANGE from GROUPS where NAME = ?;}; + my $sth = $dbh->prepare($sql) + or return (undef, 'ERROR', "Database Error: " . $dbh->errstr); + + $sth->execute($groupname) + or return (undef, 'ERROR', "Database Error: " . $sth->errstr); + + my ($low_cart, $high_cart, $cart_type, $enforce_cart_range) = $sth->fetchrow_array; + $sth->finish(); + + return ($low_cart, $high_cart, $cart_type, $enforce_cart_range); +} + +sub set_group_carts +{ + my ($dbh, $groupname, $low_cart, $high_cart, $cart_type, $enforce_cart_range) = @_; + if(!defined $low_cart) { + $low_cart = 0; + } + if(!defined $high_cart) { + $high_cart = 0; + } + if(!defined $cart_type) { + $cart_type = 1; + } + if(!defined $enforce_cart_range) { + $enforce_cart_range = 'N'; + } + + my $sql = qq{update GROUPS set DEFAULT_LOW_CART = ?, DEFAULT_HIGH_CART = ?, DEFAULT_CART_TYPE = ? , ENFORCE_CART_RANGE = ? where NAME = ?;}; + my $sth = $dbh->prepare($sql) + or return (undef, 'ERROR', "Database Error: " . $dbh->errstr); + + my $cnt = $sth->execute($low_cart, $high_cart, $cart_type, $enforce_cart_range, $groupname) + or return (undef, 'ERROR', "Database Error: " . $sth->errstr); + + $sth->finish(); + + return ($cnt, 'OK', "Success"); +} + +sub get_group_reports +{ + my ($dbh, $groupname) = @_; + + my $sql = qq{select ENABLE_NOW_NEXT, REPORT_TFC, REPORT_MUS from GROUPS where NAME = ?;}; + my $sth = $dbh->prepare($sql) + or return (undef, 'ERROR', "Database Error: " . $dbh->errstr); + + $sth->execute($groupname) + or return (undef, 'ERROR', "Database Error: " . $sth->errstr); + + my ($now_next, $traffic, $music) = $sth->fetchrow_array; + $sth->finish(); + + return ($now_next, $traffic, $music); +} + +sub set_group_reports +{ + my ($dbh, $groupname, $now_next, $traffic, $music) = @_; + if(!defined $now_next) { + $now_next = 'N'; + } + if(!defined $traffic) { + $traffic = 'Y'; + } + if(!defined $music) { + $music = 'Y'; + } + + my $sql = qq{update GROUPS set ENABLE_NOW_NEXT = ?, REPORT_TFC = ?, REPORT_MUS = ? where NAME = ?;}; + my $sth = $dbh->prepare($sql) + or return (undef, 'ERROR', "Database Error: " . $dbh->errstr); + + my $cnt = $sth->execute($now_next, $traffic, $music, $groupname) + or return (undef, 'ERROR', "Database Error: " . $sth->errstr); + + $sth->finish(); + + return ($cnt, 'OK', "Success"); +} + +########################### SHOW handling ########################### + sub get_showtitle_and_log { my ($dbh, $showid) = @_; diff --git a/utils/rd-group b/utils/rd-group new file mode 100755 index 0000000..da50be6 --- /dev/null +++ b/utils/rd-group @@ -0,0 +1,108 @@ +#!/usr/bin/perl -w +# +# rhrdlibs +# +# Copyright (C) 2015 Christian Pointner <equinox@helsinki.at> +# +# This file is part of rhrdlibs. +# +# rhrdlibs is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# any later version. +# +# rhrdlibs 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 Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with rhrdlibs. If not, see <http://www.gnu.org/licenses/>. +# + +use strict; +use lib "../lib/"; + +use RHRD::rddb; + +my $num_args = $#ARGV + 1; +if ($num_args < 2) { + print STDERR "Usage: rd-group ( (check|remove|get-carts|get-reports) <groupname> |\n" . + " add <groupname> [ <description> ] |\n" . + " set-carts <groupname> [ <low> [ <high> [ <type> [ <enforce range> ]]] |\n" . + " set-reports <groupname> [ <nownext> [ <traffic> [ <music> ]]] )\n"; + exit 1; +} + +my $cmd = $ARGV[0]; +my $groupname = $ARGV[1]; + +my ($dbh, undef, $errorstring) = RHRD::rddb::opendb(); +if(defined $dbh) { + if($cmd eq "check") { + (my $result, my $status, $errorstring) = RHRD::rddb::check_group($dbh, $groupname); + print "$result, $status: $errorstring\n"; + RHRD::rddb::closedb($dbh); + exit $result; + } elsif($cmd eq "add") { + (my $cnt, undef, $errorstring) = RHRD::rddb::add_group($dbh, $groupname, $ARGV[2]); + unless(defined $cnt) { + print "$errorstring\n"; + RHRD::rddb::closedb($dbh); + exit 1; + } + print int($cnt) . " rows affected\n"; + } elsif($cmd eq "remove") { + my @results = RHRD::rddb::remove_group($dbh, $groupname); + if(!defined $results[0] && defined $results[2]) { + print "$results[2]\n"; + } else { + for my $href (@results) { + print int($href->{cnt}) . " " . $href->{name} . " deleted\n"; + } + } + } elsif($cmd eq "get-carts") { + my ($low, $high, $type, $enforce_range) = RHRD::rddb::get_group_carts($dbh, $groupname); + unless(defined $low) { + print "$type\n"; + RHRD::rddb::closedb($dbh); + exit 1; + } + print "Range: " . int($low) . " - " . int($high) . ", Type: " . int($type) . ", Enforce Range: " . $enforce_range . "\n"; + } elsif($cmd eq "set-carts") { + (my $cnt, undef, $errorstring) = RHRD::rddb::set_group_carts($dbh, $groupname, $ARGV[2], $ARGV[3], $ARGV[4], $ARGV[5]); + unless(defined $cnt) { + print "$errorstring\n"; + RHRD::rddb::closedb($dbh); + exit 1; + } + print int($cnt) . " rows affected\n"; + } elsif($cmd eq "get-reports") { + my ($nownext, $traffic, $music) = RHRD::rddb::get_group_reports($dbh, $groupname); + unless(defined $nownext) { + print "$music\n"; + RHRD::rddb::closedb($dbh); + exit 1; + } + print "Now-Next: " . $nownext . ", Traffic: " . $traffic . ", Music: " . $music . "\n"; + } elsif($cmd eq "set-reports") { + (my $cnt, undef, $errorstring) = RHRD::rddb::set_group_reports($dbh, $groupname, $ARGV[2], $ARGV[3], $ARGV[4]); + unless(defined $cnt) { + print "$errorstring\n"; + RHRD::rddb::closedb($dbh); + exit 1; + } + print int($cnt) . " rows affected\n"; + } else { + print STDERR "unknown command\n"; + RHRD::rddb::closedb($dbh); + exit 1; + } + + RHRD::rddb::closedb($dbh); +} else { + print STDERR "$errorstring\n"; + exit 1; +} + +exit 0 |