X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2Fpart_export%2Fsqlradius.pm;h=8a8f9bebae1c73f1e9f4322fa5dfa91173f4b676;hb=c0567c688084e89fcd11bf82348b6c418f1254ac;hp=7337b5aa938a0de6991a129eef5e4ecf00d04d28;hpb=6866bdda26d1feb152af991388113e2e9309fafb;p=freeside.git diff --git a/FS/FS/part_export/sqlradius.pm b/FS/FS/part_export/sqlradius.pm index 7337b5aa9..8a8f9beba 100644 --- a/FS/FS/part_export/sqlradius.pm +++ b/FS/FS/part_export/sqlradius.pm @@ -1,28 +1,35 @@ package FS::part_export::sqlradius; use vars qw(@ISA); +use FS::Record qw( dbh ); use FS::part_export; @ISA = qw(FS::part_export); sub rebless { shift; } +sub export_username { + my($self, $svc_acct) = (shift, shift); + $svc_acct->username; +} + sub _export_insert { my($self, $svc_acct) = (shift, shift); foreach my $table (qw(reply check)) { my $method = "radius_$table"; - my %attrib = $svc_acct->$method; + my %attrib = $svc_acct->$method(); next unless keys %attrib; - my $error = $self->sqlradius_queue( $svc_acct->svcnum, 'insert', - $table, $svc_acct->username, %attrib ); - return $error if $error; + my $err_or_queue = $self->sqlradius_queue( $svc_acct->svcnum, 'insert', + $table, $self->export_username($svc_acct), %attrib ); + return $err_or_queue unless ref($err_or_queue); } my @groups = $svc_acct->radius_groups; if ( @groups ) { - my $error = $self->sqlradius_queue( $svc_acct->svcnum, 'usergroup_insert', - $svc_acct->username, @groups ); - return $error if $error; + my $err_or_queue = $self->sqlradius_queue( + $svc_acct->svcnum, 'usergroup_insert', + $self->export_username($svc_acct), @groups ); + return $err_or_queue unless ref($err_or_queue); } ''; } @@ -30,32 +37,66 @@ sub _export_insert { sub _export_replace { my( $self, $new, $old ) = (shift, shift, shift); - #return "can't (yet) change username with sqlradius" - # if $old->username ne $new->username; - if ( $old->username ne $new->username ) { - my $error = $self->sqlradius_queue( $new->svcnum, 'rename', - $new->username, $old->username ); - return $error if $error; + local $SIG{HUP} = 'IGNORE'; + local $SIG{INT} = 'IGNORE'; + local $SIG{QUIT} = 'IGNORE'; + local $SIG{TERM} = 'IGNORE'; + local $SIG{TSTP} = 'IGNORE'; + local $SIG{PIPE} = 'IGNORE'; + + my $oldAutoCommit = $FS::UID::AutoCommit; + local $FS::UID::AutoCommit = 0; + my $dbh = dbh; + + my $jobnum = ''; + if ( $self->export_username($old) ne $self->export_username($new) ) { + my $err_or_queue = $self->sqlradius_queue( $new->svcnum, 'rename', + $self->export_username($new), $self->export_username($old) ); + unless ( ref($err_or_queue) ) { + $dbh->rollback if $oldAutoCommit; + return $err_or_queue; + } + $jobnum = $err_or_queue->jobnum; } foreach my $table (qw(reply check)) { my $method = "radius_$table"; - my %new = $new->$method; - my %old = $old->$method; + my %new = $new->$method(); + my %old = $old->$method(); if ( grep { !exists $old{$_} #new attributes || $new{$_} ne $old{$_} #changed } keys %new ) { - my $error = $self->sqlradius_queue( $new->svcnum, 'insert', - $table, $new->username, %new ); - return $error if $error; + my $err_or_queue = $self->sqlradius_queue( $new->svcnum, 'insert', + $table, $self->export_username($new), %new ); + unless ( ref($err_or_queue) ) { + $dbh->rollback if $oldAutoCommit; + return $err_or_queue; + } + if ( $jobnum ) { + my $error = $err_or_queue->depend_insert( $jobnum ); + if ( $error ) { + $dbh->rollback if $oldAutoCommit; + return $error; + } + } } my @del = grep { !exists $new{$_} } keys %old; if ( @del ) { - my $error = $self->sqlradius_queue( $new->svcnum, 'attrib_delete', - $table, $new->username, @del ); - return $error if $error; + my $err_or_queue = $self->sqlradius_queue( $new->svcnum, 'attrib_delete', + $table, $self->export_username($new), @del ); + unless ( ref($err_or_queue) ) { + $dbh->rollback if $oldAutoCommit; + return $err_or_queue; + } + if ( $jobnum ) { + my $error = $err_or_queue->depend_insert( $jobnum ); + if ( $error ) { + $dbh->rollback if $oldAutoCommit; + return $error; + } + } } } @@ -72,24 +113,47 @@ sub _export_replace { } if ( @delgroups ) { - my $error = $self->sqlradius_queue( $new->svcnum, 'usergroup_delete', - $new->username, @delgroups ); - return $error if $error; + my $err_or_queue = $self->sqlradius_queue( $new->svcnum, 'usergroup_delete', + $self->export_username($new), @delgroups ); + unless ( ref($err_or_queue) ) { + $dbh->rollback if $oldAutoCommit; + return $err_or_queue; + } + if ( $jobnum ) { + my $error = $err_or_queue->depend_insert( $jobnum ); + if ( $error ) { + $dbh->rollback if $oldAutoCommit; + return $error; + } + } } if ( @newgroups ) { - my $error = $self->sqlradius_queue( $new->svcnum, 'usergroup_insert', - $new->username, @newgroups ); - return $error if $error; + my $err_or_queue = $self->sqlradius_queue( $new->svcnum, 'usergroup_insert', + $self->export_username($new), @newgroups ); + unless ( ref($err_or_queue) ) { + $dbh->rollback if $oldAutoCommit; + return $err_or_queue; + } + if ( $jobnum ) { + my $error = $err_or_queue->depend_insert( $jobnum ); + if ( $error ) { + $dbh->rollback if $oldAutoCommit; + return $error; + } + } } + $dbh->commit or die $dbh->errstr if $oldAutoCommit; + ''; } sub _export_delete { my( $self, $svc_acct ) = (shift, shift); - $self->sqlradius_queue( $svc_acct->svcnum, 'delete', - $svc_acct->username ); + my $err_or_queue = $self->sqlradius_queue( $svc_acct->svcnum, 'delete', + $self->export_username($svc_acct) ); + ref($err_or_queue) ? '' : $err_or_queue; } sub sqlradius_queue { @@ -103,23 +167,43 @@ sub sqlradius_queue { $self->option('username'), $self->option('password'), @_, - ); + ) or $queue; } sub sqlradius_insert { #subroutine, not method my $dbh = sqlradius_connect(shift, shift, shift); - my( $replycheck, $username, %attributes ) = @_; + my( $table, $username, %attributes ) = @_; foreach my $attribute ( keys %attributes ) { - my $u_sth = $dbh->prepare( - "UPDATE rad$replycheck SET Value = ? WHERE UserName = ? AND Attribute = ?" ) or die $dbh->errstr; - my $i_sth = $dbh->prepare( - "INSERT INTO rad$replycheck ( id, UserName, Attribute, Value ) ". - "VALUES ( ?, ?, ?, ? )" + + my $s_sth = $dbh->prepare( + "SELECT COUNT(*) FROM rad$table WHERE UserName = ? AND Attribute = ?" ) or die $dbh->errstr; - $u_sth->execute($attributes{$attribute}, $username, $attribute) > 0 - or $i_sth->execute( '', $username, $attribute, $attributes{$attribute} ) - or die "can't insert into rad$replycheck table: ". $i_sth->errstr; + $s_sth->execute( $username, $attribute ) or die $s_sth->errstr; + + if ( $s_sth->fetchrow_arrayref->[0] ) { + + my $u_sth = $dbh->prepare( + "UPDATE rad$table SET Value = ? WHERE UserName = ? AND Attribute = ?" + ) or die $dbh->errstr; + $u_sth->execute($attributes{$attribute}, $username, $attribute) + or die $u_sth->errstr; + + } else { + + my $i_sth = $dbh->prepare( + "INSERT INTO rad$table ( UserName, Attribute, op, Value ) ". + "VALUES ( ?, ?, ?, ? )" + ) or die $dbh->errstr; + $i_sth->execute( + $username, + $attribute, + ( $attribute =~ /Password/i ? '==' : ':=' ), + $attributes{$attribute}, + ) or die $i_sth->errstr; + + } + } $dbh->disconnect; } @@ -129,10 +213,10 @@ sub sqlradius_usergroup_insert { #subroutine, not method my( $username, @groups ) = @_; my $sth = $dbh->prepare( - "INSERT INTO usergroup ( id, UserName, GroupName ) VALUES ( ?, ?, ? )" + "INSERT INTO usergroup ( UserName, GroupName ) VALUES ( ?, ? )" ) or die $dbh->errstr; foreach my $group ( @groups ) { - $sth->execute( '', $username, $group ) + $sth->execute( $username, $group ) or die "can't insert into groupname table: ". $sth->errstr; } $dbh->disconnect; @@ -166,14 +250,14 @@ sub sqlradius_rename { #subroutine, not method sub sqlradius_attrib_delete { #subroutine, not method my $dbh = sqlradius_connect(shift, shift, shift); - my( $replycheck, $username, @attrib ) = @_; + my( $table, $username, @attrib ) = @_; foreach my $attribute ( @attrib ) { my $sth = $dbh->prepare( - "DELETE FROM rad$replycheck WHERE UserName = ? AND Attribute = ?" ) + "DELETE FROM rad$table WHERE UserName = ? AND Attribute = ?" ) or die $dbh->errstr; $sth->execute($username,$attribute) - or die "can't delete from rad$replycheck table: ". $sth->errstr; + or die "can't delete from rad$table table: ". $sth->errstr; } $dbh->disconnect; }