fix typo, RT#14695
[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 phone_sqlradius broadband_sqlradius )) {
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     my $usergroup = $export->option('usergroup') || 'usergroup';
38     for my $table (qw( radcheck radreply ), $usergroup) {
39       my $sth = $icradius_dbh->prepare("DELETE FROM $table");
40       $sth->execute or die "Can't reset $table table: ". $sth->errstr;
41     }
42     $icradius_dbh->disconnect;
43   }
44 }
45
46 use FS::svc_Common;
47 $FS::svc_Common::overlimit_missing_cust_svc_nonfatal_kludge = 1;
48 $FS::svc_Common::overlimit_missing_cust_svc_nonfatal_kludge = 1;
49
50 foreach my $export ( @exports ) {
51
52   #my @svcparts = map { $_->svcpart } $export->export_svc;
53   my $overlimit_groups = $export->option('overlimit_groups');
54
55   my @svc_x =
56     map  { $_->svc_x }
57     #map  { qsearch('cust_svc', { 'svcpart' => $_->svcpart } ) }
58     #grep { qsearch('cust_svc', { 'svcpart' => $_->svcpart } ) }
59     #     $export->export_svc;
60     map  { @{ $_->[1] } }
61     grep { scalar( @{ $_->[1] } ) }
62     map  { [ $_, [ qsearch('cust_svc', { 'svcpart' => $_->svcpart } ) ] ] }
63          $export->export_svc;
64
65
66   foreach my $svc_x ( @svc_x ) {
67
68     #$svc_x->check; #set any fixed usergroup so it'll export even if all
69     #               #svc_acct records don't have the group yet
70     #more efficient?
71     my $x = $svc_x->setfixed( $svc_x->_fieldhandlers);
72     unless ( ref($x) ) {
73       warn "WARNING: can't set fixed usergroups for svcnum ". $svc_x->svcnum.
74            "\n";
75     } 
76
77     if ($overlimit_groups && $svc_x->overlimit) {
78       $svc_x->usergroup( &{ $svc_x->_fieldhandlers->{'usergroup'} }
79                           ($svc_x, $overlimit_groups)
80                        );
81     }
82
83     #false laziness with FS::svc_acct::insert (like it matters)
84     my $error = $export->export_insert($svc_x);
85     die $error if $error;
86
87   }
88 }
89
90 sub usage {
91   die "Usage:\n\n  freeside-sqlradius-reset user [ exportnum, ... ]\n";
92 }
93
94 =head1 NAME
95
96 freeside-sqlradius-reset - Command line interface to reset and recreate RADIUS SQL tables
97
98 =head1 SYNOPSIS
99
100   freeside-sqlradius-reset [ -n ] username [ EXPORTNUM, ... ]
101
102 =head1 DESCRIPTION
103
104 Deletes the radcheck, radreply and usergroup tables and repopulates them from
105 the Freeside database, for the specified exports, or, if no exports are
106 specified, for all sqlradius and sqlradius_withdomain exports.
107
108 B<username> is a username added by freeside-adduser.
109
110 The B<-n> option, if supplied, supresses the deletion of the existing data in
111 the tables.
112
113 =head1 SEE ALSO
114
115 L<freeside-reexport>, L<FS::part_export>, L<FS::part_export::sqlradius>
116
117 =cut
118
119 1;