4 # Copyright (C) 2008 Michal Vaner (vorner@ucw.cz)
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # he Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29 my( $privnums, $privnames ) = getRightDefs();
31 sub userRights( $$ ) {
32 my( $tables, $user ) = @_;
33 foreach( @{$tables->query( 'rightsName', [ $user, $user ] )} ) {
35 print " $privnames->{$rid} ($rid)\n";
39 my $dbh = connectDb();
40 my $tables = PciIds::DBQAny::new( $dbh, {
41 'rightsName' => 'SELECT rightId FROM users INNER JOIN rights ON users.id = rights.userId WHERE users.email = ? OR users.login = ? ORDER BY rightId',
42 'allrights' => 'SELECT users.id, users.login, users.email, rights.rightId FROM users INNER JOIN rights ON users.id = rights.userId ORDER BY users.login, users.email, users.id, rights.rightId',
43 'getId' => 'SELECT id FROM users WHERE email = ? OR login = ?',
44 'add' => 'INSERT INTO rights (userId, rightId) VALUES(?, ?)',
45 'del' => 'DELETE FROM rights WHERE userId = ? AND rightId = ?'
48 while( scalar @ARGV ) {
49 my $cmd = shift @ARGV;
52 foreach( @{$tables->query( 'allrights', [] )} ) {
53 my( $id, $name, $mail, $rid ) = @{$_};
54 if( $id != $lastid ) {
55 print "$mail ($id)\t$name\n";
58 print " $privnames->{$rid} ($rid)\n";
60 } elsif( $cmd =~ /^[+-]l?$/ ) {
61 my $user = shift @ARGV;
62 my $id = $tables->query( 'getId', [ $user, $user ] )->[ 0 ]->[ 0 ];
63 die "Invalid user $user\n" unless( defined $id );
64 my $right = $privnums->{shift @ARGV};
65 die "Invalid right $right\n" unless( defined $right );
66 my @params = ( $id, $right );
67 my $q = ( $cmd =~ /-/ ) ? 'del' : 'add';
68 $tables->command( $q, \@params );
69 } elsif( $cmd eq '-h' ) {
70 print "rights.pl username\t\t\tPrint user's rights\n";
71 print "rights.pl -a\t\t\t\tPrint all users and their rights\n";
72 print "rights.pl +/- user right\t\tGrant/revoke user's right\n";
75 userRights( $tables, $cmd );