diff options
author | Christian Pointner <equinox@spreadspace.org> | 2015-09-28 16:40:41 (GMT) |
---|---|---|
committer | Christian Pointner <equinox@spreadspace.org> | 2015-09-28 16:40:41 (GMT) |
commit | bcd58fdfc6d8496d5fa250aa8d6e0d0d3a89d49d (patch) | |
tree | 8d3349a97488e9256ab105dc65e581d5b185e369 | |
parent | 222541ee6348f21a0b8107bebb3701422614ba88 (diff) |
add group membership functions
-rwxr-xr-x | lib/RHRD/rddb.pm | 72 | ||||
-rwxr-xr-x | test/get-range | 2 | ||||
-rwxr-xr-x | utils/rd-group | 44 |
3 files changed, 116 insertions, 2 deletions
diff --git a/lib/RHRD/rddb.pm b/lib/RHRD/rddb.pm index a337557..f8832d1 100755 --- a/lib/RHRD/rddb.pm +++ b/lib/RHRD/rddb.pm @@ -534,6 +534,78 @@ sub set_group_reports return ($cnt, 'OK', "Success"); } +sub get_group_members +{ + my ($dbh, $groupname) = @_; + + my $sql = qq{select USER_NAME from USER_PERMS 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); + + my @users; + while(my ($user) = $sth->fetchrow_array()) { + push @users, $user; + } + $sth->finish(); + + return @users; +} + +sub is_group_member +{ + my ($dbh, $groupname, $username) = @_; + + my $sql = qq{select count(*) from USER_PERMS where GROUP_NAME = ? and USER_NAME = ?;}; + my $sth = $dbh->prepare($sql) + or return (undef, 'ERROR', "Database Error: " . $dbh->errstr); + + $sth->execute($groupname, $username) + or return (undef, 'ERROR', "Database Error: " . $sth->errstr); + + my ($cnt) = $sth->fetchrow_array(); + $sth->finish(); + + return ($cnt, 'OK', "success"); +} + +sub add_group_member +{ + my ($dbh, $groupname, $username) = @_; + + my ($cnt, $result, $errostring) = is_group_member($dbh, $groupname, $username); + if($cnt > 0) { + return (undef, 'ERROR', "already a member"); + } + + my $sql = qq{insert into USER_PERMS (GROUP_NAME, USER_NAME) values (?, ?);}; + my $sth = $dbh->prepare($sql) + or return (undef, 'ERROR', "Database Error: " . $dbh->errstr); + + $cnt = $sth->execute($groupname, $username) + or return (undef, 'ERROR', "Database Error: " . $sth->errstr); + + $sth->finish(); + return ($cnt, 'OK', "success"); +} + +sub remove_group_member +{ + my ($dbh, $groupname, $username) = @_; + + my $sql = qq{delete from USER_PERMS where GROUP_NAME = ? and USER_NAME = ?;}; + my $sth = $dbh->prepare($sql) + or return (undef, 'ERROR', "Database Error: " . $dbh->errstr); + + my $cnt = $sth->execute($groupname, $username) + or return (undef, 'ERROR', "Database Error: " . $sth->errstr); + + $sth->finish(); + return ($cnt, 'OK', "success"); +} + ########################### SHOW handling ########################### sub get_showtitle_and_log diff --git a/test/get-range b/test/get-range index 1d4e12e..630d5d6 100755 --- a/test/get-range +++ b/test/get-range @@ -72,7 +72,7 @@ if(defined $dbh) { RHRD::rddb::closedb($dbh); } else { - print STDERR "$errorstring\n"; + print "$errorstring\n"; exit 1; } diff --git a/utils/rd-group b/utils/rd-group index a221d9f..1717caf 100755 --- a/utils/rd-group +++ b/utils/rd-group @@ -23,10 +23,17 @@ use strict; use RHRD::rddb; +# this is ridiculous but makes it a little harder to create/remove users... +if ($> != 0 ) { + print STDERR "this must be run as root!\n"; + exit 1; +} + my $num_args = $#ARGV + 1; if ($num_args < 2) { - print STDERR "Usage: rd-group ( (check|remove|get-carts|get-reports) <groupname> |\n" . + print STDERR "Usage: rd-group ( (check|remove|get-members|get-carts|get-reports) <groupname> |\n" . " add <groupname> [ <description> ] |\n" . + " (add-member|remove-member|is-member) <groupname> <user> |\n" . " set-carts <groupname> [ <low> [ <high> [ <type> [ <enforce range> ]]] |\n" . " set-reports <groupname> [ <nownext> [ <traffic> [ <music> ]]] )\n"; exit 1; @@ -59,6 +66,41 @@ if(defined $dbh) { print int($href->{cnt}) . " " . $href->{name} . " deleted\n"; } } + } elsif($cmd eq "get-members") { + my @users = RHRD::rddb::get_group_members($dbh, $groupname); + if(!defined $users[0] && defined $users[1]) { + print "$users[2]\n"; + RHRD::rddb::closedb($dbh); + exit 1; + } + for my $user (sort @users) { + print "$user\n"; + } + } elsif($cmd eq "add-member") { + (my $cnt, undef, $errorstring) = RHRD::rddb::add_group_member($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-member") { + (my $cnt, undef, $errorstring) = RHRD::rddb::remove_group_member($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 "is-member") { + (my $cnt, undef, $errorstring) = RHRD::rddb::is_group_member($dbh, $groupname, $ARGV[2]); + RHRD::rddb::closedb($dbh); + unless(defined $cnt) { + print "$errorstring\n"; + exit 1; + } + print $ARGV[2] . " is" . (($cnt) ? "" : " not") . " a member\n"; + exit (($cnt) ? 0 : 1); } elsif($cmd eq "get-carts") { my ($low, $high, $type, $enforce_range) = RHRD::rddb::get_group_carts($dbh, $groupname); unless(defined $low) { |