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