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