fix big in RADIUS session viewing when using an ignored-accounting export
[freeside.git] / FS / bin / freeside-sqlradius-reset
1 #!/usr/bin/perl -Tw
2
3 use strict;
4 use FS::UID qw(adminsuidsetup);
5 use FS::Record qw(qsearch qsearchs);
6 use FS::part_export;
7 use FS::svc_acct;
8 use FS::cust_svc;
9
10 my $user = shift or die &usage;
11 adminsuidsetup $user;
12
13 #my $machine = shift or die &usage;
14
15 my @exports = ();
16 if ( @ARGV ) {
17   foreach my $exportnum ( @ARGV ) {
18     foreach my $exporttype (qw( sqlradius sqlradius_withdomain )) {
19     push @exports, qsearch('part_export', { exportnum  => $exportnum,
20                                             exporttype => $exporttype, } );
21     }
22   }
23  } else {
24   @exports = qsearch('part_export', { exporttype=>'sqlradius' } );
25   push @exports, qsearch('part_export', { exporttype=>'sqlradius_withdomain' } );
26 }
27
28 foreach my $export ( @exports ) {
29   my $icradius_dbh = DBI->connect(
30     map { $export->option($_) } qw( datasrc username password )
31   ) or die $DBI::errstr;
32   for my $table (qw( radcheck radreply usergroup )) {
33     my $sth = $icradius_dbh->prepare("DELETE FROM $table");
34     $sth->execute or die "Can't reset $table table: ". $sth->errstr;
35   }
36   $icradius_dbh->disconnect;
37 }
38
39 foreach my $export ( @exports ) {
40
41   #my @svcparts = map { $_->svcpart } $export->export_svc;
42
43   my @svc_acct =
44     map { qsearchs('svc_acct', { 'svcnum' => $_->svcnum } ) }
45       map { qsearch('cust_svc', { 'svcpart' => $_->svcpart } ) }
46         grep { qsearch('cust_svc', { 'svcpart' => $_->svcpart } ) }
47           $export->export_svc;
48
49   foreach my $svc_acct ( @svc_acct ) {
50
51     #false laziness with FS::svc_acct::insert (like it matters)
52     my $error = $export->export_insert($svc_acct);
53     die $error if $error;
54
55   }
56 }
57
58 sub usage {
59   die "Usage:\n\n  freeside-sqlradius-reset user [ exportnum, ... ]\n";
60 }
61
62 =head1 NAME
63
64 freeside-sqlradius-reset - Command line interface to reset and recreate RADIUS SQL tables
65
66 =head1 SYNOPSIS
67
68   freeside-sqlradius-reset username [ EXPORTNUM, ... ]
69
70 =head1 DESCRIPTION
71
72 Deletes the radcheck, radreply and usergroup tables and repopulates them from
73 the Freeside database, for the specified exports, or, if no exports are
74 specified, for all sqlradius and sqlradius_withdomain exports.
75
76 B<username> is a username added by freeside-adduser.
77
78 =head1 SEE ALSO
79
80 L<freeside-reexport>, L<FS::part_export>, L<FS::part_export::sqlradius>
81
82 =cut
83
84
85