set any fixed usergroup before exporting so it'll export even if all svc_acct records...
[freeside.git] / FS / bin / freeside-sqlradius-reset
1 #!/usr/bin/perl -w
2
3 use strict;
4 use FS::UID qw(adminsuidsetup);
5 use FS::Record qw(qsearch qsearchs);
6 use FS::part_export;
7 use FS::svc_acct;
8 use FS::cust_svc;
9
10 my $user = shift or die &usage;
11 adminsuidsetup $user;
12
13 #my $machine = shift or die &usage;
14
15 my @exports = ();
16 if ( @ARGV ) {
17   foreach my $exportnum ( @ARGV ) {
18     foreach my $exporttype (qw( sqlradius sqlradius_withdomain )) {
19     push @exports, qsearch('part_export', { exportnum  => $exportnum,
20                                             exporttype => $exporttype, } );
21     }
22   }
23  } else {
24   @exports = qsearch('part_export', { exporttype=>'sqlradius' } );
25   push @exports, qsearch('part_export', { exporttype=>'sqlradius_withdomain' } );
26 }
27
28 foreach my $export ( @exports ) {
29   my $icradius_dbh = DBI->connect(
30     map { $export->option($_) } qw( datasrc username password )
31   ) or die $DBI::errstr;
32   for my $table (qw( radcheck radreply usergroup )) {
33     my $sth = $icradius_dbh->prepare("DELETE FROM $table");
34     $sth->execute or die "Can't reset $table table: ". $sth->errstr;
35   }
36   $icradius_dbh->disconnect;
37 }
38
39 foreach my $export ( @exports ) {
40
41   #my @svcparts = map { $_->svcpart } $export->export_svc;
42
43   my @svc_acct =
44     map { qsearchs('svc_acct', { 'svcnum' => $_->svcnum } ) }
45       map { qsearch('cust_svc', { 'svcpart' => $_->svcpart } ) }
46         grep { qsearch('cust_svc', { 'svcpart' => $_->svcpart } ) }
47           $export->export_svc;
48
49   foreach my $svc_acct ( @svc_acct ) {
50
51     $svc_acct->check; #set any fixed usergroup so it'll export even if all
52                       #svc_acct records don't have the group yet
53
54     #false laziness with FS::svc_acct::insert (like it matters)
55     my $error = $export->export_insert($svc_acct);
56     die $error if $error;
57
58   }
59 }
60
61 sub usage {
62   die "Usage:\n\n  freeside-sqlradius-reset user [ exportnum, ... ]\n";
63 }
64
65 =head1 NAME
66
67 freeside-sqlradius-reset - Command line interface to reset and recreate RADIUS SQL tables
68
69 =head1 SYNOPSIS
70
71   freeside-sqlradius-reset username [ EXPORTNUM, ... ]
72
73 =head1 DESCRIPTION
74
75 Deletes the radcheck, radreply and usergroup tables and repopulates them from
76 the Freeside database, for the specified exports, or, if no exports are
77 specified, for all sqlradius and sqlradius_withdomain exports.
78
79 B<username> is a username added by freeside-adduser.
80
81 =head1 SEE ALSO
82
83 L<freeside-reexport>, L<FS::part_export>, L<FS::part_export::sqlradius>
84
85 =cut
86
87
88