don't use return value of UPDATE to decide whether or not to INSERT.
[freeside.git] / FS / FS / part_export / sqlradius.pm
1 package FS::part_export::sqlradius;
2
3 use vars qw(@ISA);
4 use FS::part_export;
5
6 @ISA = qw(FS::part_export);
7
8 sub rebless { shift; }
9
10 sub _export_insert {
11   my($self, $svc_acct) = (shift, shift);
12
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;
20   }
21   my @groups = $svc_acct->radius_groups;
22   if ( @groups ) {
23     my $error = $self->sqlradius_queue( $svc_acct->svcnum, 'usergroup_insert',
24       $svc_acct->username, @groups );
25     return $error if $error;
26   }
27   '';
28 }
29
30 sub _export_replace {
31   my( $self, $new, $old ) = (shift, shift, shift);
32
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;
39   }
40
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
47               } keys %new
48     ) {
49       my $error = $self->sqlradius_queue( $new->svcnum, 'insert',
50         $table, $new->username, %new );
51       return $error if $error;
52     }
53
54     my @del = grep { !exists $new{$_} } keys %old;
55     if ( @del ) {
56       my $error = $self->sqlradius_queue( $new->svcnum, 'attrib_delete',
57         $table, $new->username, @del );
58       return $error if $error;
59     }
60   }
61
62   # (sorta) false laziness with FS::svc_acct::replace
63   my @oldgroups = @{$old->usergroup}; #uuuh
64   my @newgroups = $new->radius_groups;
65   my @delgroups = ();
66   foreach my $oldgroup ( @oldgroups ) {
67     if ( grep { $oldgroup eq $_ } @newgroups ) {
68       @newgroups = grep { $oldgroup ne $_ } @newgroups;
69       next;
70     }
71     push @delgroups, $oldgroup;
72   }
73
74   if ( @delgroups ) {
75     my $error = $self->sqlradius_queue( $new->svcnum, 'usergroup_delete',
76       $new->username, @delgroups );
77     return $error if $error;
78   }
79
80   if ( @newgroups ) {
81     my $error = $self->sqlradius_queue( $new->svcnum, 'usergroup_insert',
82       $new->username, @newgroups );
83     return $error if $error;
84   }
85
86   '';
87 }
88
89 sub _export_delete {
90   my( $self, $svc_acct ) = (shift, shift);
91   $self->sqlradius_queue( $svc_acct->svcnum, 'delete',
92     $svc_acct->username );
93 }
94
95 sub sqlradius_queue {
96   my( $self, $svcnum, $method ) = (shift, shift, shift);
97   my $queue = new FS::queue {
98     'svcnum' => $svcnum,
99     'job'    => "FS::part_export::sqlradius::sqlradius_$method",
100   };
101   $queue->insert(
102     $self->option('datasrc'),
103     $self->option('username'),
104     $self->option('password'),
105     @_,
106   );
107 }
108
109 sub sqlradius_insert { #subroutine, not method
110   my $dbh = sqlradius_connect(shift, shift, shift);
111   my( $table, $username, %attributes ) = @_;
112
113   foreach my $attribute ( keys %attributes ) {
114   
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;
119
120     if ( $s_sth->fetchrow_arrayref->[0] ) {
121
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;
127
128     } else {
129
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;
136
137     }
138
139   }
140   $dbh->disconnect;
141 }
142
143 sub sqlradius_usergroup_insert { #subroutine, not method
144   my $dbh = sqlradius_connect(shift, shift, shift);
145   my( $username, @groups ) = @_;
146
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;
153   }
154   $dbh->disconnect;
155 }
156
157 sub sqlradius_usergroup_delete { #subroutine, not method
158   my $dbh = sqlradius_connect(shift, shift, shift);
159   my( $username, @groups ) = @_;
160
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;
167   }
168   $dbh->disconnect;
169 }
170
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 = ?")
176       or die $dbh->errstr;
177     $sth->execute($new_username, $old_username)
178       or die "can't update $table: ". $sth->errstr;
179   }
180   $dbh->disconnect;
181 }
182
183 sub sqlradius_attrib_delete { #subroutine, not method
184   my $dbh = sqlradius_connect(shift, shift, shift);
185   my( $table, $username, @attrib ) = @_;
186
187   foreach my $attribute ( @attrib ) {
188     my $sth = $dbh->prepare(
189         "DELETE FROM rad$table WHERE UserName = ? AND Attribute = ?" )
190       or die $dbh->errstr;
191     $sth->execute($username,$attribute)
192       or die "can't delete from rad$table table: ". $sth->errstr;
193   }
194   $dbh->disconnect;
195 }
196
197 sub sqlradius_delete { #subroutine, not method
198   my $dbh = sqlradius_connect(shift, shift, shift);
199   my $username = shift;
200
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;
205   }
206   $dbh->disconnect;
207 }
208
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;
213 }
214