]> mj.ucw.cz Git - pciids.git/blob - scripts/rights.pl
ids_to_dbdump is still needed by the mailbot
[pciids.git] / scripts / rights.pl
1 #!/usr/bin/perl
2
3 #       PciIds web database
4 #       Copyright (C) 2008 Michal Vaner (vorner@ucw.cz)
5 #
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.
10 #
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
14 #
15 #       GNU General Public License for more details.
16 #
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
20
21 use strict;
22 BEGIN {
23         unshift @INC, ".";
24 }
25 use PciIds::Db;
26 use PciIds::DBQAny;
27 use PciIds::Users;
28
29 my( $privnums, $privnames ) = getRightDefs();
30
31 sub userRights( $$ ) {
32         my( $tables, $user ) = @_;
33         foreach( @{$tables->query( 'rightsName', [ $user, $user ] )} ) {
34                 my( $rid ) = @{$_};
35                 print "  $privnames->{$rid} ($rid)\n";
36         }
37 }
38
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 = ?'
46 });
47
48 while( scalar @ARGV ) {
49         my $cmd = shift @ARGV;
50         if( $cmd eq '-a' ) {
51                 my $lastid = undef;
52                 foreach( @{$tables->query( 'allrights', [] )} ) {
53                         my( $id, $name, $mail, $rid ) = @{$_};
54                         if( $id != $lastid ) {
55                                 print "$mail ($id)\t$name\n";
56                                 $lastid = $id;
57                         }
58                         print "  $privnames->{$rid} ($rid)\n";
59                 }
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";
73         } else {
74                 print "$cmd\n";
75                 userRights( $tables, $cmd );
76         }
77 }
78 $dbh->commit();
79 $dbh->disconnect();