add -n option to freeside-sqlradius-reset to supress deleting data
[freeside.git] / FS / bin / freeside-sqlradius-reset
1 #!/usr/bin/perl -w
2
3 use strict;
4 use vars qw( $opt_n );
5 use Getopt::Std;
6 use FS::UID qw(adminsuidsetup);
7 use FS::Record qw(qsearch qsearchs);
8 use FS::part_export;
9 use FS::svc_acct;
10 use FS::cust_svc;
11
12 getopts("n");
13
14 my $user = shift or die &usage;
15 adminsuidsetup $user;
16
17 #my $machine = shift or die &usage;
18
19 my @exports = ();
20 if ( @ARGV ) {
21   foreach my $exportnum ( @ARGV ) {
22     foreach my $exporttype (qw( sqlradius sqlradius_withdomain )) {
23     push @exports, qsearch('part_export', { exportnum  => $exportnum,
24                                             exporttype => $exporttype, } );
25     }
26   }
27  } else {
28   @exports = qsearch('part_export', { exporttype=>'sqlradius' } );
29   push @exports, qsearch('part_export', { exporttype=>'sqlradius_withdomain' } );
30 }
31
32 unless ( $opt_n ) {
33   foreach my $export ( @exports ) {
34     my $icradius_dbh = DBI->connect(
35       map { $export->option($_) } qw( datasrc username password )
36     ) or die $DBI::errstr;
37     for my $table (qw( radcheck radreply usergroup )) {
38       my $sth = $icradius_dbh->prepare("DELETE FROM $table");
39       $sth->execute or die "Can't reset $table table: ". $sth->errstr;
40     }
41     $icradius_dbh->disconnect;
42   }
43 }
44
45 foreach my $export ( @exports ) {
46
47   #my @svcparts = map { $_->svcpart } $export->export_svc;
48   my $overlimit_groups = $export->option('overlimit_groups');
49
50   my @svc_acct =
51     map { qsearchs('svc_acct', { 'svcnum' => $_->svcnum } ) }
52       map { qsearch('cust_svc', { 'svcpart' => $_->svcpart } ) }
53         grep { qsearch('cust_svc', { 'svcpart' => $_->svcpart } ) }
54           $export->export_svc;
55
56   foreach my $svc_acct ( @svc_acct ) {
57
58     $svc_acct->check; #set any fixed usergroup so it'll export even if all
59                       #svc_acct records don't have the group yet
60
61     if ($overlimit_groups && $svc_acct->overlimit) {
62       $svc_acct->usergroup( &{ $svc_acct->_fieldhandlers->{'usergroup'} }
63                             ($svc_acct, $overlimit_groups)
64                           );
65     }
66
67     #false laziness with FS::svc_acct::insert (like it matters)
68     my $error = $export->export_insert($svc_acct);
69     die $error if $error;
70
71   }
72 }
73
74 sub usage {
75   die "Usage:\n\n  freeside-sqlradius-reset user [ exportnum, ... ]\n";
76 }
77
78 =head1 NAME
79
80 freeside-sqlradius-reset - Command line interface to reset and recreate RADIUS SQL tables
81
82 =head1 SYNOPSIS
83
84   freeside-sqlradius-reset [ -n ] username [ EXPORTNUM, ... ]
85
86 =head1 DESCRIPTION
87
88 Deletes the radcheck, radreply and usergroup tables and repopulates them from
89 the Freeside database, for the specified exports, or, if no exports are
90 specified, for all sqlradius and sqlradius_withdomain exports.
91
92 B<username> is a username added by freeside-adduser.
93
94 The B<-n> option, if supplied, supresses the deletion of the existing data in
95 the tables.
96
97 =head1 SEE ALSO
98
99 L<freeside-reexport>, L<FS::part_export>, L<FS::part_export::sqlradius>
100
101 =cut
102
103
104