summaryrefslogtreecommitdiff
path: root/FS/FS/part_export/acct_xmlrpc.pm
blob: acd7ffe5df25ab013bce0c1ad4db5ce7b7e62019 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package FS::part_export::acct_xmlrpc;
use base qw( FS::part_export );

use vars qw( %info ); # $DEBUG );
#use Data::Dumper;
use Tie::IxHash;
use Frontier::Client; #to avoid adding a dependency on RPC::XML just now
use Frontier::RPC2;
#use FS::Record qw( qsearch qsearchs );
use FS::Schema qw( dbdef );

#$DEBUG = 1;

tie my %options, 'Tie::IxHash',
  'xmlrpc_url'       => { label => 'XML-RPC URL', },
  'param_style'      => { label   => 'Parameter style',
                          type    => 'select',
                          options => [ 'Individual values',
                                       'Struct of name/value pairs',
                                     ],
                        },
  'insert_method'    => { label => 'Insert method', },
  'insert_params'    => { label => 'Insert parameters', type=>'textarea', },
  'replace_method'   => { label => 'Replace method', },
  'replace_params'   => { label => 'Replace parameters', type=>'textarea', },
  'delete_method'    => { label => 'Delete method', },
  'delete_params'    => { label => 'Delete parameters', type=>'textarea', },
  'suspend_method'   => { label => 'Suspend method', },
  'suspend_params'   => { label => 'Suspend parameters', type=>'textarea', },
  'unsuspend_method' => { label => 'Unsuspend method', },
  'unsuspend_params' => { label => 'Unsuspend parameters', type=>'textarea', },
;

%info = (
  'svc'     => 'svc_acct',
  'desc'    => 'Configurable provisioning of accounts via the XML-RPC protocol',
  'options' => \%options,
  'no_machine' => 1,
  'notes'   => <<'END',
Configurable, real-time export of accounts via the XML-RPC protocol.<BR>
<BR>
If using "Individual values" parameter style, specify one parameter per line.<BR>
<BR>
If using "Struct of name/value pairs" parameter style, specify one name and
value on each line, separated by whitespace.<BR>
<BR>
The following variables are available for interpolation (prefixed with new_ or
old_ for replace operations):
<UL>
  <LI><code>$username</code>
  <LI><code>$domain</code>
  <LI><code>$email</code> - username@domain
  <LI><code>$_password</code>
  <LI><code>$crypt_password</code> - encrypted password
  <LI><code>$ldap_password</code> - Password in LDAP/RFC2307 format (for example, "{PLAIN}himom", "{CRYPT}94pAVyK/4oIBk" or "{MD5}5426824942db4253f87a1009fd5d2d4")
  <LI><code>$uid</code>
  <LI><code>$gid</code>
  <LI><code>$finger</code> - Real name
  <LI><code>$dir</code> - home directory
  <LI><code>$shell</code>
  <LI><code>$quota</code>
  <LI><code>@radius_groups</code>
<!--  <LI><code>$reasonnum (when suspending)</code>
  <LI><code>$reasontext (when suspending)</code>
  <LI><code>$reasontypenum (when suspending)</code>
  <LI><code>$reasontypetext (when suspending)</code>
-->
<!--
  <LI><code>$pkgnum</code>
  <LI><code>$custnum</code>
-->
  <LI>All other fields in <b>svc_acct</b> are also available.
<!--  <LI>The following fields from <b>cust_main</b> are also available (except during replace): company, address1, address2, city, state, zip, county, daytime, night, fax, otaker, agent_custid, locale. -->
</UL>

END
);

sub _export_insert    { shift->_export_command('insert',    @_) }
sub _export_delete    { shift->_export_command('delete',    @_) }
sub _export_suspend   { shift->_export_command('suspend',   @_) }
sub _export_unsuspend { shift->_export_command('unsuspend', @_) }

sub _export_command {
  my ( $self, $action, $svc_acct) = (shift, shift, shift);
  my $method = $self->option($action.'_method');
  return '' if $method =~ /^\s*$/;

  my @params = split("\n", $self->option($action.'_params') );

  my( @x_param ) = ();
  my( %x_struct ) = ();
  foreach my $param (@params) {

    my($name, $value) = ('', '');
    if ($self->option('param_style') eq 'Struct of name/value pairs' ) {
      ($name, $value) = split(/\s+/, $param);
    } else { #'Individual values'
      $value = $param;
    }

    if ( $value =~ /^\s*(\$|\@)(\w+)\s*$/ ) {
      $value = $self->_export_value($2, $svc_acct);
    }

    if ($self->option('param_style') eq 'Struct of name/value pairs' ) {
      $x_struct{$name} = $value;
    } else { #'Individual values'
      push @x_param, $value;
    }

  }

  my @x = ();
  if ($self->option('param_style') eq 'Struct of name/value pairs' ) {
    @x = ( \%x_struct );
  } else { #'Individual values'
    @x = @x_param;
  }

  #option to queue (or not) ?

  my $conn = Frontier::Client->new( url => $self->option('xmlrpc_url') );

  my $result = $conn->call($method, @x);

  #XXX error checking?  $result?  from the call?
  '';
}

sub _export_replace {
  my( $self, $new, $old ) = (shift, shift, shift);

  my $method = $self->option('replace_method');
  return '' if $method =~ /^\s*$/;

  my @params = split("\n", $self->option('replace_params') );

  my( @x_param ) = ();
  my( %x_struct ) = ();
  foreach my $param (@params) {

    my($name, $value) = ('', '');
    if ($self->option('param_style') eq 'Struct of name/value pairs' ) {
      ($name, $value) = split(/\s+/, $param);
    } else { #'Individual values'
      $value = $param;
    }

    if ( $value =~ /^\s*(\$|\@)(old|new)_(\w+)\s*$/ ) {
      if ($2 eq 'old' ) {
        $value = $self->_export_value($3, $old);
      } elsif ( $2 eq 'new' ) {
        $value = $self->_export_value($3, $new);
      } else {
        die 'guru meditation stella blue';
      }
    }

    if ($self->option('param_style') eq 'Struct of name/value pairs' ) {
      $x_struct{$name} = $value;
    } else { #'Individual values'
      push @x_param, $value;
    }

  }

  my @x = ();
  if ($self->option('param_style') eq 'Struct of name/value pairs' ) {
    @x = ( \%x_struct );
  } else { #'Individual values'
    @x = @x_param;
  }

  #option to queue (or not) ?

  my $conn = Frontier::Client->new( url => $self->option('xmlrpc_url') );

  my $result = $conn->call($method, @x);

  #XXX error checking?  $result?  from the call?
  '';

}

#comceptual false laziness w/shellcommands.pm
sub _export_value {
  my( $self, $value, $svc_acct) = (shift, shift, shift);

  my %fields = map { $_=>1 } $svc_acct->fields;

  if ( $fields{$value} ) {
    my $type = dbdef->table('svc_acct')->column($value)->type;
    if ( $type =~ /^(int|serial)/i ) {
      return Frontier::RPC2::Integer->new( $svc_acct->$value() );
    } elsif ( $value =~ /^last_log/ ) {
      return Frontier::RPC2::DateTime::ISO8601->new( $svc_acct->$value() ); #conversion?
    } else {
      return Frontier::RPC2::String->new( $svc_acct->$value() );
    }
  } elsif ( $value =~ /^(domain|email)$/ ) {
    return Frontier::RPC2::String->new( $svc_acct->$value() );
  } elsif ( $value eq 'crypt_password' ) {
    return Frontier::RPC2::String->new( $svc_acct->crypt_password( $self->option('crypt') ) );
  } elsif ( $value eq 'ldap_password' ) {
    return Frontier::RPC2::String->new( $svc_acct->ldap_password($self->option('crypt') ) );
  } elsif ( $value eq 'radius_groups' ) {
    my @radius_groups = $svc_acct->radius_groups;
    #XXX
  }

#this is the "cust_main" email, not svc_acct->email
#  my $cust_pkg = $svc_acct->cust_svc->cust_pkg;
#  if ( $cust_pkg ) {
#    no strict 'vars';
#    {
#      no strict 'refs';
#      foreach my $custf (qw( company address1 address2 city state zip country
#                             daytime night fax otaker agent_custid locale
#                        ))
#      {
#        ${$custf} = $cust_pkg->cust_main->$custf();
#      }
#    }
#    $email = ( grep { $_ !~ /^(POST|FAX)$/ } $cust_pkg->cust_main->invoicing_list )[0];
#  } else {
#    $email = '';
#  }

#  my ($reasonnum, $reasontext, $reasontypenum, $reasontypetext);
#  if ( $cust_pkg && $action eq 'suspend' &&
#       (my $r = $cust_pkg->last_reason('susp')) )
#  {
#    $reasonnum = $r->reasonnum;
#    $reasontext = $r->reason;
#    $reasontypenum = $r->reason_type;
#    $reasontypetext = $r->reasontype->type;
#
#    my %reasonmap = $self->_groups_susp_reason_map;
#    my $userspec = '';
#    $userspec = $reasonmap{$reasonnum}
#      if exists($reasonmap{$reasonnum});
#    $userspec = $reasonmap{$reasontext}
#      if (!$userspec && exists($reasonmap{$reasontext}));
#
#    my $suspend_user;
#    if ( $userspec =~ /^\d+$/ ) {
#      $suspend_user = qsearchs( 'svc_acct', { 'svcnum' => $userspec } );
#    } elsif ( $userspec =~ /^\S+\@\S+$/ ) {
#      my ($username,$domain) = split(/\@/, $userspec);
#      for my $user (qsearch( 'svc_acct', { 'username' => $username } )){
#        $suspend_user = $user if $userspec eq $user->email;
#      }
#    } elsif ($userspec) {
#      $suspend_user = qsearchs( 'svc_acct', { 'username' => $userspec } );
#    }
#  
#  @radius_groups = $suspend_user->radius_groups
#    if $suspend_user;  
#  
#  } else {
#    $reasonnum = $reasontext = $reasontypenum = $reasontypetext = '';
#  }

#  $pkgnum = $cust_pkg ? $cust_pkg->pkgnum : '';
#  $custnum = $cust_pkg ? $cust_pkg->custnum : '';

  '';

}

1;