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