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( $replycheck, $username, %attributes ) = @_;
113 foreach my $attribute ( keys %attributes ) {
114 my $u_sth = $dbh->prepare(
115 "UPDATE rad$replycheck SET Value = ? WHERE UserName = ? AND Attribute = ?" ) or die $dbh->errstr;
116 my $i_sth = $dbh->prepare(
117 "INSERT INTO rad$replycheck ( id, UserName, Attribute, Value ) ".
118 "VALUES ( ?, ?, ?, ? )"
119 ) or die $dbh->errstr;
120 $u_sth->execute($attributes{$attribute}, $username, $attribute) > 0
121 or $i_sth->execute( '', $username, $attribute, $attributes{$attribute} )
122 or die "can't insert into rad$replycheck table: ". $i_sth->errstr;
127 sub sqlradius_usergroup_insert { #subroutine, not method
128 my $dbh = sqlradius_connect(shift, shift, shift);
129 my( $username, @groups ) = @_;
131 my $sth = $dbh->prepare(
132 "INSERT INTO usergroup ( id, UserName, GroupName ) VALUES ( ?, ?, ? )"
133 ) or die $dbh->errstr;
134 foreach my $group ( @groups ) {
135 $sth->execute( '', $username, $group )
136 or die "can't insert into groupname table: ". $sth->errstr;
141 sub sqlradius_usergroup_delete { #subroutine, not method
142 my $dbh = sqlradius_connect(shift, shift, shift);
143 my( $username, @groups ) = @_;
145 my $sth = $dbh->prepare(
146 "DELETE FROM usergroup ( id, UserName, GroupName ) VALUES ( ?, ?, ? )"
147 ) or die $dbh->errstr;
148 foreach my $group ( @groups ) {
149 $sth->execute( '', $username, $group )
150 or die "can't delete from groupname table: ". $sth->errstr;
155 sub sqlradius_rename { #subroutine, not method
156 my $dbh = sqlradius_connect(shift, shift, shift);
157 my($new_username, $old_username) = @_;
158 foreach my $table (qw(radreply radcheck usergroup )) {
159 my $sth = $dbh->prepare("UPDATE $table SET Username = ? WHERE UserName = ?")
161 $sth->execute($new_username, $old_username)
162 or die "can't update $table: ". $sth->errstr;
167 sub sqlradius_attrib_delete { #subroutine, not method
168 my $dbh = sqlradius_connect(shift, shift, shift);
169 my( $replycheck, $username, @attrib ) = @_;
171 foreach my $attribute ( @attrib ) {
172 my $sth = $dbh->prepare(
173 "DELETE FROM rad$replycheck WHERE UserName = ? AND Attribute = ?" )
175 $sth->execute($username,$attribute)
176 or die "can't delete from rad$replycheck table: ". $sth->errstr;
181 sub sqlradius_delete { #subroutine, not method
182 my $dbh = sqlradius_connect(shift, shift, shift);
183 my $username = shift;
185 foreach my $table (qw( radcheck radreply usergroup )) {
186 my $sth = $dbh->prepare( "DELETE FROM $table WHERE UserName = ?" );
187 $sth->execute($username)
188 or die "can't delete from $table table: ". $sth->errstr;
193 sub sqlradius_connect {
194 #my($datasrc, $username, $password) = @_;
195 #DBI->connect($datasrc, $username, $password) or die $DBI::errstr;
196 DBI->connect(@_) or die $DBI::errstr;