diff options
Diffstat (limited to 'lib/RHRD')
-rwxr-xr-x | lib/RHRD/rddb.pm | 197 |
1 files changed, 197 insertions, 0 deletions
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) = @_; |