summaryrefslogtreecommitdiff
path: root/FS/bin/freeside-sqlradius-reset
blob: b04c640d842c1b5306258f36153d5f12acdc373a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/perl -w

use strict;
use vars qw( $opt_n );
use Getopt::Std;
use FS::UID qw(adminsuidsetup);
use FS::Record qw(qsearch qsearchs);
use FS::part_export;
#use FS::svc_acct;
use FS::cust_svc;

getopts("n");

my $user = shift or die &usage;
adminsuidsetup $user;

#my $machine = shift or die &usage;

my @exports = ();
if ( @ARGV ) {
  foreach my $exportnum ( @ARGV ) {
    foreach my $exporttype (qw( sqlradius sqlradius_withdomain phone_sqlradius broadband_sqlradius )) {
    push @exports, qsearch('part_export', { exportnum  => $exportnum,
                                            exporttype => $exporttype, } );
    }
  }
 } else {
  @exports = qsearch('part_export', { exporttype=>'sqlradius' } );
  push @exports, qsearch('part_export', { exporttype=>'sqlradius_withdomain' } ); 
}

unless ( $opt_n ) {
  foreach my $export ( @exports ) {
    my $icradius_dbh = DBI->connect(
      map { $export->option($_) } qw( datasrc username password )
    ) or die $DBI::errstr;
    my $usergroup = $export->option('usergroup') || 'usergroup';
    my @attr_tables;
    @attr_tables = qw( radgroupcheck radgroupreply )
      if $export->option('export_attrs');
    for my $table (qw( radcheck radreply ), $usergroup, @attr_tables) {
      my $sth = $icradius_dbh->prepare("DELETE FROM $table");
      $sth->execute or die "Can't reset $table table: ". $sth->errstr;
    }
    $icradius_dbh->disconnect;
  }
}

use FS::svc_Common;
$FS::svc_Common::overlimit_missing_cust_svc_nonfatal_kludge = 1;
$FS::svc_Common::overlimit_missing_cust_svc_nonfatal_kludge = 1;

# this is the same across all exports, for now
my @radius_attrs = qsearch('radius_attr', {});

foreach my $export ( @exports ) {

  #my @svcparts = map { $_->svcpart } $export->export_svc;
  my $overlimit_groups = $export->option('overlimit_groups');

  my @svc_x =
    map  { $_->svc_x }
    #map  { qsearch('cust_svc', { 'svcpart' => $_->svcpart } ) }
    #grep { qsearch('cust_svc', { 'svcpart' => $_->svcpart } ) }
    #     $export->export_svc;
    map  { @{ $_->[1] } }
    grep { scalar( @{ $_->[1] } ) }
    map  { [ $_, [ qsearch('cust_svc', { 'svcpart' => $_->svcpart } ) ] ] }
         $export->export_svc;


  foreach my $svc_x ( @svc_x ) {

    #$svc_x->check; #set any fixed usergroup so it'll export even if all
    #               #svc_acct records don't have the group yet
    #more efficient?
    my $x = $svc_x->setfixed( $svc_x->_fieldhandlers);
    unless ( ref($x) ) {
      warn "WARNING: can't set fixed usergroups for svcnum ". $svc_x->svcnum.
           "\n";
    } 

    if ($overlimit_groups && $svc_x->overlimit) {
      $svc_x->usergroup( &{ $svc_x->_fieldhandlers->{'usergroup'} }
                          ($svc_x, $overlimit_groups)
                       );
    }

    #false laziness with FS::svc_acct::insert (like it matters)
    my $error = $export->export_insert($svc_x);
    die $error if $error;

  }

  if ( $export->option('export_attrs') ) {
    foreach my $attr (@radius_attrs) {
      my $error = $export->export_attr_insert($attr);
      die $error if $error;
    }
  }
}

sub usage {
  die "Usage:\n\n  freeside-sqlradius-reset user [ exportnum, ... ]\n";
}

=head1 NAME

freeside-sqlradius-reset - Command line interface to reset and recreate RADIUS SQL tables

=head1 SYNOPSIS

  freeside-sqlradius-reset [ -n ] username [ EXPORTNUM, ... ]

=head1 DESCRIPTION

Deletes the radcheck, radreply and usergroup tables and repopulates them from
the Freeside database, for the specified exports, or, if no exports are
specified, for all sqlradius and sqlradius_withdomain exports.

B<username> is a username added by freeside-adduser.

The B<-n> option, if supplied, supresses the deletion of the existing data in
the tables.

=head1 SEE ALSO

L<freeside-reexport>, L<FS::part_export>, L<FS::part_export::sqlradius>

=cut

1;