summaryrefslogtreecommitdiff
path: root/FS/FS
diff options
context:
space:
mode:
authormark <mark>2011-10-31 19:20:16 +0000
committermark <mark>2011-10-31 19:20:16 +0000
commit2ad6569982365759d7baaf5a97bc836770a54291 (patch)
tree81c72ce1e7687e27ea1f727643f659fdb6335793 /FS/FS
parent588ac92d054671aa13cb61f76b5da472661cb1ac (diff)
export NAS table to sqlradius, #14697
Diffstat (limited to 'FS/FS')
-rw-r--r--FS/FS/export_nas.pm32
-rw-r--r--FS/FS/nas.pm100
-rw-r--r--FS/FS/part_export.pm3
-rw-r--r--FS/FS/part_export/sqlradius.pm69
4 files changed, 162 insertions, 42 deletions
diff --git a/FS/FS/export_nas.pm b/FS/FS/export_nas.pm
index 3829b41..5282503 100644
--- a/FS/FS/export_nas.pm
+++ b/FS/FS/export_nas.pm
@@ -1,9 +1,12 @@
package FS::export_nas;
use strict;
+use vars qw($noexport_hack);
use base qw( FS::Record );
use FS::Record qw( qsearch qsearchs );
+$noexport_hack = '';
+
=head1 NAME
FS::export_nas - Object methods for export_nas records
@@ -70,7 +73,11 @@ otherwise returns false.
=cut
-# the insert method can be inherited from FS::Record
+sub insert {
+ my $self = shift;
+ $self->SUPER::insert ||
+ ($noexport_hack ? '' : $self->part_export->export_nas_insert($self->nas));
+}
=item delete
@@ -78,16 +85,21 @@ Delete this record from the database.
=cut
-# the delete method can be inherited from FS::Record
+sub delete {
+ my $self = shift;
+ ($noexport_hack ? '' : $self->part_export->export_nas_delete($self->nas))
+ || $self->SUPER::delete;
+}
=item replace OLD_RECORD
-Replaces the OLD_RECORD with this one in the database. If there is an error,
-returns the error, otherwise returns false.
+Unavailable. Delete the record and create a new one.
=cut
-# the replace method can be inherited from FS::Record
+sub replace {
+ die "replace not implemented for export_nas records";
+}
=item check
@@ -113,6 +125,16 @@ sub check {
$self->SUPER::check;
}
+sub part_export {
+ my $self = shift;
+ qsearchs('part_export', { 'exportnum' => $self->exportnum });
+}
+
+sub nas {
+ my $self = shift;
+ qsearchs('nas', { 'nasnum' => $self->nasnum });
+}
+
=back
=head1 BUGS
diff --git a/FS/FS/nas.pm b/FS/FS/nas.pm
index 7fb7db5..4564a63 100644
--- a/FS/FS/nas.pm
+++ b/FS/FS/nas.pm
@@ -1,8 +1,10 @@
package FS::nas;
use strict;
-use base qw( FS::Record );
-use FS::Record qw( qsearch qsearchs );
+use base qw( FS::m2m_Common FS::Record );
+use FS::Record qw( qsearch qsearchs dbh );
+use FS::export_nas;
+use FS::part_export;
=head1 NAME
@@ -30,41 +32,23 @@ FS::Record. The following fields are currently supported:
=over 4
-=item nasnum
+=item nasnum - primary key
-primary key
+=item nasname - "NAS name", i.e. IP address
-=item nasname
+=item shortname - short descriptive name
-nasname
-
-=item shortname
-
-shortname
-
-=item type
-
-type
+=item type - the equipment vendor
=item ports
-ports
+=item secret - the authentication secret for this client
-=item secret
-
-secret
-
-=item server
-
-server
+=item server - virtual server name (optional)
=item community
-community
-
-=item description
-
-description
+=item description - a longer descriptive name
=back
@@ -91,26 +75,62 @@ sub table { 'nas'; }
Adds this record to the database. If there is an error, returns the error,
otherwise returns false.
-=cut
-
-# the insert method can be inherited from FS::Record
-
=item delete
-Delete this record from the database.
+Delete this record from the database and remove all linked exports.
=cut
-# the delete method can be inherited from FS::Record
+sub delete {
+ my $oldAutoCommit = $FS::UID::AutoCommit;
+ local $FS::UID::AutoCommit = 0;
+ my $dbh = dbh;
+
+ my $self = shift;
+ my $error = $self->process_m2m([])
+ || $self->SUPER::delete;
+
+ if ( $error ) {
+ $dbh->rollback;
+ return $error;
+ }
+
+ $dbh->commit if $oldAutoCommit;
+ '';
+}
=item replace OLD_RECORD
Replaces the OLD_RECORD with this one in the database. If there is an error,
returns the error, otherwise returns false.
+To change the list of linked exports, see the C<export_nas> method.
+
=cut
-# the replace method can be inherited from FS::Record
+sub replace {
+ my $oldAutoCommit = $FS::UID::AutoCommit;
+ local $FS::UID::AutoCommit = 0;
+ my $dbh = dbh;
+
+ my ($self, $old) = @_;
+ $old ||= qsearchs('nas', { 'nasnum' => $self->nasnum });
+
+ my $error;
+ foreach my $part_export ( $self->part_export ) {
+ $error ||= $part_export->export_nas_replace($self, $old);
+ }
+
+ $error ||= $self->SUPER::replace($old);
+
+ if ( $error ) {
+ $dbh->rollback;
+ return $error;
+ }
+
+ $dbh->commit if $oldAutoCommit;
+ '';
+}
=item check
@@ -142,6 +162,18 @@ sub check {
$self->SUPER::check;
}
+=item part_export
+
+Return all L<FS::part_export> objects to which this NAS is being exported.
+
+=cut
+
+sub part_export {
+ my $self = shift;
+ map { qsearchs('part_export', { exportnum => $_->exportnum }) }
+ qsearch('export_nas', { nasnum => $self->nasnum})
+}
+
=back
=head1 BUGS
diff --git a/FS/FS/part_export.pm b/FS/FS/part_export.pm
index 9a479b7..f84f2a0 100644
--- a/FS/FS/part_export.pm
+++ b/FS/FS/part_export.pm
@@ -4,8 +4,8 @@ use strict;
use vars qw( @ISA @EXPORT_OK $DEBUG %exports );
use Exporter;
use Tie::IxHash;
+use base qw( FS::option_Common FS::m2m_Common ); # m2m for 'export_nas'
use FS::Record qw( qsearch qsearchs dbh );
-use FS::option_Common;
use FS::part_svc;
use FS::part_export_option;
use FS::export_svc;
@@ -13,7 +13,6 @@ use FS::export_svc;
#for export modules, though they should probably just use it themselves
use FS::queue;
-@ISA = qw( FS::option_Common );
@EXPORT_OK = qw(export_info);
$DEBUG = 0;
diff --git a/FS/FS/part_export/sqlradius.pm b/FS/FS/part_export/sqlradius.pm
index c51429d..07f6cf0 100644
--- a/FS/FS/part_export/sqlradius.pm
+++ b/FS/FS/part_export/sqlradius.pm
@@ -106,6 +106,7 @@ END
'desc' => 'Real-time export to SQL-backed RADIUS (FreeRADIUS, ICRADIUS)',
'options' => \%options,
'nodomain' => 'Y',
+ 'nas' => 'Y', # show export_nas selection in UI
'notes' => $notes1.
'This export does not export RADIUS realms (see also '.
'sqlradius_withdomain). '.
@@ -761,7 +762,7 @@ sub update_svc {
AcctInputOctets, AcctOutputOctets
FROM radacct
WHERE FreesideStatus IS NULL
- AND AcctStopTime != 0
+ AND AcctStopTime IS NOT NULL
") or die $dbh->errstr;
$sth->execute() or die $sth->errstr;
@@ -864,6 +865,72 @@ sub _try_decrement {
return 'skipped';
}
+=item export_nas_insert NAS
+
+=item export_nas_delete NAS
+
+=item export_nas_replace NEW_NAS OLD_NAS
+
+Update the NAS table (allowed RADIUS clients) on the attached RADIUS
+server. Currently requires the table to be named 'nas' and to follow
+the stock schema (/etc/freeradius/nas.sql).
+
+=cut
+
+sub export_nas_insert { shift->export_nas_action('insert', @_); }
+sub export_nas_delete { shift->export_nas_action('delete', @_); }
+sub export_nas_replace { shift->export_nas_action('replace', @_); }
+
+sub export_nas_action {
+ my $self = shift;
+ my ($action, $new, $old) = @_;
+ # find the NAS in the target table by its name
+ my $nasname = ($action eq 'replace') ? $old->nasname : $new->nasname;
+ my $nasnum = $new->nasnum;
+
+ my $err_or_queue = $self->sqlradius_queue('', "nas_$action",
+ nasname => $nasname,
+ nasnum => $nasnum
+ );
+ return $err_or_queue unless ref $err_or_queue;
+ '';
+}
+
+sub sqlradius_nas_insert {
+ my $dbh = sqlradius_connect(shift, shift, shift);
+ my %opt = @_;
+ my $nas = qsearchs('nas', { nasnum => $opt{'nasnum'} })
+ or die "nasnum ".$opt{'nasnum'}.' not found';
+ # insert actual NULLs where FS::Record has translated to empty strings
+ my @values = map { length($nas->$_) ? $nas->$_ : undef }
+ qw( nasname shortname type secret server community description );
+ my $sth = $dbh->prepare('INSERT INTO nas
+(nasname, shortname, type, secret, server, community, description)
+VALUES (?, ?, ?, ?, ?, ?, ?)');
+ $sth->execute(@values) or die $dbh->errstr;
+}
+
+sub sqlradius_nas_delete {
+ my $dbh = sqlradius_connect(shift, shift, shift);
+ my %opt = @_;
+ my $sth = $dbh->prepare('DELETE FROM nas WHERE nasname = ?');
+ $sth->execute($opt{'nasname'}) or die $dbh->errstr;
+}
+
+sub sqlradius_nas_replace {
+ my $dbh = sqlradius_connect(shift, shift, shift);
+ my %opt = @_;
+ my $nas = qsearchs('nas', { nasnum => $opt{'nasnum'} })
+ or die "nasnum ".$opt{'nasnum'}.' not found';
+ my @values = map {$nas->$_}
+ qw( nasname shortname type secret server community description );
+ my $sth = $dbh->prepare('UPDATE nas SET
+ nasname = ?, shortname = ?, type = ?, secret = ?,
+ server = ?, community = ?, description = ?
+ WHERE nasname = ?');
+ $sth->execute(@values, $opt{'nasname'}) or die $dbh->errstr;
+}
+
###
#class methods
###