This commit was generated by cvs2svn to compensate for changes in r5562,
[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   my $overlimit_groups = $export->option('overlimit_groups');
43
44   my @svc_acct =
45     map { qsearchs('svc_acct', { 'svcnum' => $_->svcnum } ) }
46       map { qsearch('cust_svc', { 'svcpart' => $_->svcpart } ) }
47         grep { qsearch('cust_svc', { 'svcpart' => $_->svcpart } ) }
48           $export->export_svc;
49
50   foreach my $svc_acct ( @svc_acct ) {
51
52     $svc_acct->check; #set any fixed usergroup so it'll export even if all
53                       #svc_acct records don't have the group yet
54
55     if ($overlimit_groups && $svc_acct->overlimit) {
56       $svc_acct->usergroup( &{ $svc_acct->_fieldhandlers->{'usergroup'} }
57                             ($svc_acct, $overlimit_groups)
58                           );
59     }
60
61     #false laziness with FS::svc_acct::insert (like it matters)
62     my $error = $export->export_insert($svc_acct);
63     die $error if $error;
64
65   }
66 }
67
68 sub usage {
69   die "Usage:\n\n  freeside-sqlradius-reset user [ exportnum, ... ]\n";
70 }
71
72 =head1 NAME
73
74 freeside-sqlradius-reset - Command line interface to reset and recreate RADIUS SQL tables
75
76 =head1 SYNOPSIS
77
78   freeside-sqlradius-reset username [ EXPORTNUM, ... ]
79
80 =head1 DESCRIPTION
81
82 Deletes the radcheck, radreply and usergroup tables and repopulates them from
83 the Freeside database, for the specified exports, or, if no exports are
84 specified, for all sqlradius and sqlradius_withdomain exports.
85
86 B<username> is a username added by freeside-adduser.
87
88 =head1 SEE ALSO
89
90 L<freeside-reexport>, L<FS::part_export>, L<FS::part_export::sqlradius>
91
92 =cut
93
94
95