1 package FS::part_export::sqlradius;
4 use FS::Record qw( dbh );
7 @ISA = qw(FS::part_export);
12 my($self, $svc_acct) = (shift, shift);
14 foreach my $table (qw(reply check)) {
15 my $method = "radius_$table";
16 my %attrib = $svc_acct->$method();
17 next unless keys %attrib;
18 my $err_or_queue = $self->sqlradius_queue( $svc_acct->svcnum, 'insert',
19 $table, $svc_acct->username, %attrib );
20 return $err_or_queue unless ref($err_or_queue);
22 my @groups = $svc_acct->radius_groups;
24 my $err_or_queue = $self->sqlradius_queue(
25 $svc_acct->svcnum, 'usergroup_insert',
26 $svc_acct->username, @groups );
27 return $err_or_queue unless ref($err_or_queue);
33 my( $self, $new, $old ) = (shift, shift, shift);
35 local $SIG{HUP} = 'IGNORE';
36 local $SIG{INT} = 'IGNORE';
37 local $SIG{QUIT} = 'IGNORE';
38 local $SIG{TERM} = 'IGNORE';
39 local $SIG{TSTP} = 'IGNORE';
40 local $SIG{PIPE} = 'IGNORE';
42 my $oldAutoCommit = $FS::UID::AutoCommit;
43 local $FS::UID::AutoCommit = 0;
47 if ( $old->username ne $new->username ) {
48 my $err_or_queue = $self->sqlradius_queue( $new->svcnum, 'rename',
49 $new->username, $old->username );
50 unless ( ref($err_or_queue) ) {
51 $dbh->rollback if $oldAutoCommit;
54 $jobnum = $err_or_queue->jobnum;
57 foreach my $table (qw(reply check)) {
58 my $method = "radius_$table";
59 my %new = $new->$method();
60 my %old = $old->$method();
61 if ( grep { !exists $old{$_} #new attributes
62 || $new{$_} ne $old{$_} #changed
65 my $err_or_queue = $self->sqlradius_queue( $new->svcnum, 'insert',
66 $table, $new->username, %new );
67 unless ( ref($err_or_queue) ) {
68 $dbh->rollback if $oldAutoCommit;
72 my $error = $err_or_queue->depend_insert( $jobnum );
74 $dbh->rollback if $oldAutoCommit;
80 my @del = grep { !exists $new{$_} } keys %old;
82 my $err_or_queue = $self->sqlradius_queue( $new->svcnum, 'attrib_delete',
83 $table, $new->username, @del );
84 unless ( ref($err_or_queue) ) {
85 $dbh->rollback if $oldAutoCommit;
89 my $error = $err_or_queue->depend_insert( $jobnum );
91 $dbh->rollback if $oldAutoCommit;
98 # (sorta) false laziness with FS::svc_acct::replace
99 my @oldgroups = @{$old->usergroup}; #uuuh
100 my @newgroups = $new->radius_groups;
102 foreach my $oldgroup ( @oldgroups ) {
103 if ( grep { $oldgroup eq $_ } @newgroups ) {
104 @newgroups = grep { $oldgroup ne $_ } @newgroups;
107 push @delgroups, $oldgroup;
111 my $err_or_queue = $self->sqlradius_queue( $new->svcnum, 'usergroup_delete',
112 $new->username, @delgroups );
113 unless ( ref($err_or_queue) ) {
114 $dbh->rollback if $oldAutoCommit;
115 return $err_or_queue;
118 my $error = $err_or_queue->depend_insert( $jobnum );
120 $dbh->rollback if $oldAutoCommit;
127 my $err_or_queue = $self->sqlradius_queue( $new->svcnum, 'usergroup_insert',
128 $new->username, @newgroups );
129 unless ( ref($err_or_queue) ) {
130 $dbh->rollback if $oldAutoCommit;
131 return $err_or_queue;
134 my $error = $err_or_queue->depend_insert( $jobnum );
136 $dbh->rollback if $oldAutoCommit;
142 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
148 my( $self, $svc_acct ) = (shift, shift);
149 my $err_or_queue = $self->sqlradius_queue( $svc_acct->svcnum, 'delete',
150 $svc_acct->username );
151 ref($err_or_queue) ? '' : $err_or_queue;
154 sub sqlradius_queue {
155 my( $self, $svcnum, $method ) = (shift, shift, shift);
156 my $queue = new FS::queue {
158 'job' => "FS::part_export::sqlradius::sqlradius_$method",
161 $self->option('datasrc'),
162 $self->option('username'),
163 $self->option('password'),
168 sub sqlradius_insert { #subroutine, not method
169 my $dbh = sqlradius_connect(shift, shift, shift);
170 my( $table, $username, %attributes ) = @_;
172 foreach my $attribute ( keys %attributes ) {
174 my $s_sth = $dbh->prepare(
175 "SELECT COUNT(*) FROM rad$table WHERE UserName = ? AND Attribute = ?"
176 ) or die $dbh->errstr;
177 $s_sth->execute( $username, $attribute ) or die $s_sth->errstr;
179 if ( $s_sth->fetchrow_arrayref->[0] ) {
181 my $u_sth = $dbh->prepare(
182 "UPDATE rad$table SET Value = ? WHERE UserName = ? AND Attribute = ?"
183 ) or die $dbh->errstr;
184 $u_sth->execute($attributes{$attribute}, $username, $attribute)
185 or die $u_sth->errstr;
189 my $i_sth = $dbh->prepare(
190 "INSERT INTO rad$table ( id, UserName, Attribute, Value ) ".
191 "VALUES ( ?, ?, ?, ? )"
192 ) or die $dbh->errstr;
193 $i_sth->execute( '', $username, $attribute, $attributes{$attribute} )
194 or die $i_sth->errstr;
202 sub sqlradius_usergroup_insert { #subroutine, not method
203 my $dbh = sqlradius_connect(shift, shift, shift);
204 my( $username, @groups ) = @_;
206 my $sth = $dbh->prepare(
207 "INSERT INTO usergroup ( id, UserName, GroupName ) VALUES ( ?, ?, ? )"
208 ) or die $dbh->errstr;
209 foreach my $group ( @groups ) {
210 $sth->execute( '', $username, $group )
211 or die "can't insert into groupname table: ". $sth->errstr;
216 sub sqlradius_usergroup_delete { #subroutine, not method
217 my $dbh = sqlradius_connect(shift, shift, shift);
218 my( $username, @groups ) = @_;
220 my $sth = $dbh->prepare(
221 "DELETE FROM usergroup WHERE UserName = ? AND GroupName = ?"
222 ) or die $dbh->errstr;
223 foreach my $group ( @groups ) {
224 $sth->execute( $username, $group )
225 or die "can't delete from groupname table: ". $sth->errstr;
230 sub sqlradius_rename { #subroutine, not method
231 my $dbh = sqlradius_connect(shift, shift, shift);
232 my($new_username, $old_username) = @_;
233 foreach my $table (qw(radreply radcheck usergroup )) {
234 my $sth = $dbh->prepare("UPDATE $table SET Username = ? WHERE UserName = ?")
236 $sth->execute($new_username, $old_username)
237 or die "can't update $table: ". $sth->errstr;
242 sub sqlradius_attrib_delete { #subroutine, not method
243 my $dbh = sqlradius_connect(shift, shift, shift);
244 my( $table, $username, @attrib ) = @_;
246 foreach my $attribute ( @attrib ) {
247 my $sth = $dbh->prepare(
248 "DELETE FROM rad$table WHERE UserName = ? AND Attribute = ?" )
250 $sth->execute($username,$attribute)
251 or die "can't delete from rad$table table: ". $sth->errstr;
256 sub sqlradius_delete { #subroutine, not method
257 my $dbh = sqlradius_connect(shift, shift, shift);
258 my $username = shift;
260 foreach my $table (qw( radcheck radreply usergroup )) {
261 my $sth = $dbh->prepare( "DELETE FROM $table WHERE UserName = ?" );
262 $sth->execute($username)
263 or die "can't delete from $table table: ". $sth->errstr;
268 sub sqlradius_connect {
269 #my($datasrc, $username, $password) = @_;
270 #DBI->connect($datasrc, $username, $password) or die $DBI::errstr;
271 DBI->connect(@_) or die $DBI::errstr;