1 package FS::part_export::sqlradius;
6 @ISA = qw(FS::part_export);
11 my($self, $svc_acct) = (shift, shift);
13 foreach my $table (qw(reply check)) {
14 my $method = "radius_$table";
15 my %attrib = $svc_acct->$method;
16 next unless keys %attrib;
17 my $error = $self->sqlradius_queue( $svc_acct->svcnum, 'insert',
18 $table, $svc_acct->username, %attrib );
19 return $error if $error;
21 my @groups = $svc_acct->radius_groups;
23 my $error = $self->sqlradius_queue( $svc_acct->svcnum, 'usergroup_insert',
24 $svc_acct->username, @groups );
25 return $error if $error;
31 my( $self, $new, $old ) = (shift, shift, shift);
33 #return "can't (yet) change username with sqlradius"
34 # if $old->username ne $new->username;
35 if ( $old->username ne $new->username ) {
36 my $error = $self->sqlradius_queue( $new->svcnum, 'rename',
37 $new->username, $old->username );
38 return $error if $error;
41 foreach my $table (qw(reply check)) {
42 my $method = "radius_$table";
43 my %new = $new->$method;
44 my %old = $old->$method;
45 if ( grep { !exists $old{$_} #new attributes
46 || $new{$_} ne $old{$_} #changed
49 my $error = $self->sqlradius_queue( $new->svcnum, 'insert',
50 $table, $new->username, %new );
51 return $error if $error;
54 my @del = grep { !exists $new{$_} } keys %old;
56 my $error = $self->sqlradius_queue( $new->svcnum, 'attrib_delete',
57 $table, $new->username, @del );
58 return $error if $error;
62 # (sorta) false laziness with FS::svc_acct::replace
63 my @oldgroups = @{$old->usergroup}; #uuuh
64 my @newgroups = $new->radius_groups;
66 foreach my $oldgroup ( @oldgroups ) {
67 if ( grep { $oldgroup eq $_ } @newgroups ) {
68 @newgroups = grep { $oldgroup ne $_ } @newgroups;
71 push @delgroups, $oldgroup;
75 my $error = $self->sqlradius_queue( $new->svcnum, 'usergroup_delete',
76 $new->username, @delgroups );
77 return $error if $error;
81 my $error = $self->sqlradius_queue( $new->svcnum, 'usergroup_insert',
82 $new->username, @newgroups );
83 return $error if $error;
90 my( $self, $svc_acct ) = (shift, shift);
91 $self->sqlradius_queue( $svc_acct->svcnum, 'delete',
92 $svc_acct->username );
96 my( $self, $svcnum, $method ) = (shift, shift, shift);
97 my $queue = new FS::queue {
99 'job' => "FS::part_export::sqlradius::sqlradius_$method",
102 $self->option('datasrc'),
103 $self->option('username'),
104 $self->option('password'),
109 sub sqlradius_insert { #subroutine, not method
110 my $dbh = sqlradius_connect(shift, shift, shift);
111 my( $table, $username, %attributes ) = @_;
113 foreach my $attribute ( keys %attributes ) {
115 my $s_sth = $dbh->prepare(
116 "SELECT COUNT(*) FROM rad$table WHERE UserName = ? AND Attribute = ?"
117 ) or die $dbh->errstr;
118 $s_sth->execute( $username, $attribute ) or die $s_sth->errstr;
120 if ( $s_sth->fetchrow_arrayref->[0] ) {
122 my $u_sth = $dbh->prepare(
123 "UPDATE rad$table SET Value = ? WHERE UserName = ? AND Attribute = ?"
124 ) or die $dbh->errstr;
125 $u_sth->execute($attributes{$attribute}, $username, $attribute)
126 or die $u_sth->errstr;
130 my $i_sth = $dbh->prepare(
131 "INSERT INTO rad$table ( id, UserName, Attribute, Value ) ".
132 "VALUES ( ?, ?, ?, ? )"
133 ) or die $dbh->errstr;
134 $i_sth->execute( '', $username, $attribute, $attributes{$attribute} )
135 or die $i_sth->errstr;
143 sub sqlradius_usergroup_insert { #subroutine, not method
144 my $dbh = sqlradius_connect(shift, shift, shift);
145 my( $username, @groups ) = @_;
147 my $sth = $dbh->prepare(
148 "INSERT INTO usergroup ( id, UserName, GroupName ) VALUES ( ?, ?, ? )"
149 ) or die $dbh->errstr;
150 foreach my $group ( @groups ) {
151 $sth->execute( '', $username, $group )
152 or die "can't insert into groupname table: ". $sth->errstr;
157 sub sqlradius_usergroup_delete { #subroutine, not method
158 my $dbh = sqlradius_connect(shift, shift, shift);
159 my( $username, @groups ) = @_;
161 my $sth = $dbh->prepare(
162 "DELETE FROM usergroup WHERE UserName = ? AND GroupName = ?"
163 ) or die $dbh->errstr;
164 foreach my $group ( @groups ) {
165 $sth->execute( $username, $group )
166 or die "can't delete from groupname table: ". $sth->errstr;
171 sub sqlradius_rename { #subroutine, not method
172 my $dbh = sqlradius_connect(shift, shift, shift);
173 my($new_username, $old_username) = @_;
174 foreach my $table (qw(radreply radcheck usergroup )) {
175 my $sth = $dbh->prepare("UPDATE $table SET Username = ? WHERE UserName = ?")
177 $sth->execute($new_username, $old_username)
178 or die "can't update $table: ". $sth->errstr;
183 sub sqlradius_attrib_delete { #subroutine, not method
184 my $dbh = sqlradius_connect(shift, shift, shift);
185 my( $table, $username, @attrib ) = @_;
187 foreach my $attribute ( @attrib ) {
188 my $sth = $dbh->prepare(
189 "DELETE FROM rad$table WHERE UserName = ? AND Attribute = ?" )
191 $sth->execute($username,$attribute)
192 or die "can't delete from rad$table table: ". $sth->errstr;
197 sub sqlradius_delete { #subroutine, not method
198 my $dbh = sqlradius_connect(shift, shift, shift);
199 my $username = shift;
201 foreach my $table (qw( radcheck radreply usergroup )) {
202 my $sth = $dbh->prepare( "DELETE FROM $table WHERE UserName = ?" );
203 $sth->execute($username)
204 or die "can't delete from $table table: ". $sth->errstr;
209 sub sqlradius_connect {
210 #my($datasrc, $username, $password) = @_;
211 #DBI->connect($datasrc, $username, $password) or die $DBI::errstr;
212 DBI->connect(@_) or die $DBI::errstr;