ignore problams calling ->overlimit during sqlradius-reset, wtf?! RT#5868
[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 )) {
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 use FS::svc_Common;
46 $FS::svc_Common::overlimit_missing_cust_svc_nonfatal_kludge = 1;
47 $FS::svc_Common::overlimit_missing_cust_svc_nonfatal_kludge = 1;
48
49 foreach my $export ( @exports ) {
50
51   #my @svcparts = map { $_->svcpart } $export->export_svc;
52   my $overlimit_groups = $export->option('overlimit_groups');
53
54   my @svc_x =
55     map  { $_->svc_x }
56     map  { qsearch('cust_svc', { 'svcpart' => $_->svcpart } ) }
57     grep { qsearch('cust_svc', { 'svcpart' => $_->svcpart } ) }
58          $export->export_svc;
59
60   foreach my $svc_x ( @svc_x ) {
61
62     $svc_x->check; #set any fixed usergroup so it'll export even if all
63                    #svc_acct records don't have the group yet
64
65     if ($overlimit_groups && $svc_x->overlimit) {
66       $svc_x->usergroup( &{ $svc_x->_fieldhandlers->{'usergroup'} }
67                           ($svc_x, $overlimit_groups)
68                        );
69     }
70
71     #false laziness with FS::svc_acct::insert (like it matters)
72     my $error = $export->export_insert($svc_x);
73     die $error if $error;
74
75   }
76 }
77
78 sub usage {
79   die "Usage:\n\n  freeside-sqlradius-reset user [ exportnum, ... ]\n";
80 }
81
82 =head1 NAME
83
84 freeside-sqlradius-reset - Command line interface to reset and recreate RADIUS SQL tables
85
86 =head1 SYNOPSIS
87
88   freeside-sqlradius-reset [ -n ] username [ EXPORTNUM, ... ]
89
90 =head1 DESCRIPTION
91
92 Deletes the radcheck, radreply and usergroup tables and repopulates them from
93 the Freeside database, for the specified exports, or, if no exports are
94 specified, for all sqlradius and sqlradius_withdomain exports.
95
96 B<username> is a username added by freeside-adduser.
97
98 The B<-n> option, if supplied, supresses the deletion of the existing data in
99 the tables.
100
101 =head1 SEE ALSO
102
103 L<freeside-reexport>, L<FS::part_export>, L<FS::part_export::sqlradius>
104
105 =cut
106
107 1;