Checkin of disparate changes from working on the road:
[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( $replycheck, $username, %attributes ) = @_;
112
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;
123   }
124   $dbh->disconnect;
125 }
126
127 sub sqlradius_usergroup_insert { #subroutine, not method
128   my $dbh = sqlradius_connect(shift, shift, shift);
129   my( $username, @groups ) = @_;
130
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;
137   }
138   $dbh->disconnect;
139 }
140
141 sub sqlradius_usergroup_delete { #subroutine, not method
142   my $dbh = sqlradius_connect(shift, shift, shift);
143   my( $username, @groups ) = @_;
144
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;
151   }
152   $dbh->disconnect;
153 }
154
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 = ?")
160       or die $dbh->errstr;
161     $sth->execute($new_username, $old_username)
162       or die "can't update $table: ". $sth->errstr;
163   }
164   $dbh->disconnect;
165 }
166
167 sub sqlradius_attrib_delete { #subroutine, not method
168   my $dbh = sqlradius_connect(shift, shift, shift);
169   my( $replycheck, $username, @attrib ) = @_;
170
171   foreach my $attribute ( @attrib ) {
172     my $sth = $dbh->prepare(
173         "DELETE FROM rad$replycheck WHERE UserName = ? AND Attribute = ?" )
174       or die $dbh->errstr;
175     $sth->execute($username,$attribute)
176       or die "can't delete from rad$replycheck table: ". $sth->errstr;
177   }
178   $dbh->disconnect;
179 }
180
181 sub sqlradius_delete { #subroutine, not method
182   my $dbh = sqlradius_connect(shift, shift, shift);
183   my $username = shift;
184
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;
189   }
190   $dbh->disconnect;
191 }
192
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;
197 }
198