ClientAPI
authorivan <ivan>
Tue, 16 Jul 2002 12:29:05 +0000 (12:29 +0000)
committerivan <ivan>
Tue, 16 Jul 2002 12:29:05 +0000 (12:29 +0000)
FS/FS/ClientAPI.pm [new file with mode: 0644]
FS/FS/ClientAPI/MyAccount.pm [new file with mode: 0644]
FS/FS/ClientAPI/passwd.pm [new file with mode: 0644]
FS/MANIFEST

diff --git a/FS/FS/ClientAPI.pm b/FS/FS/ClientAPI.pm
new file mode 100644 (file)
index 0000000..f7b8eb0
--- /dev/null
@@ -0,0 +1,44 @@
+package FS::ClientAPI;
+
+use strict;
+use vars qw(%handler);
+
+%handler = ();
+
+#find modules
+foreach my $INC ( @INC ) {
+  foreach my $file ( glob("$INC/FS/ClientAPI/*") ) {
+    $file =~ /\/(\w+)\.pm$/ or do {
+      warn "unrecognized ClientAPI file: $file";
+      next
+    };
+    my $mod = $1;
+    #warn "using FS::ClientAPI::$mod";
+    eval "use FS::ClientAPI::$mod;";
+    die "error using FS::ClientAPI::$mod: $@" if $@;
+  }
+}
+
+#(sub for modules)
+sub register_handlers {
+  my $self = shift;
+  my %new_handlers = @_;
+  foreach my $key ( keys %new_handlers ) {
+    warn "WARNING: redefining sub $key" if exists $handler{$key};
+    #warn "registering $key";
+    $handler{$key} = $new_handlers{$key};
+  }
+}
+
+#---
+
+sub dispatch {
+  my ( $self, $name ) = ( shift, shift );
+  my $sub = $handler{$name}
+    or die "unknown FS::ClientAPI sub $name (known: ". join(" ", keys %handler );
+    #or die "unknown FS::ClientAPI sub $name";
+  &{$sub}(@_);
+}
+
+1;
+
diff --git a/FS/FS/ClientAPI/MyAccount.pm b/FS/FS/ClientAPI/MyAccount.pm
new file mode 100644 (file)
index 0000000..6747855
--- /dev/null
@@ -0,0 +1,136 @@
+package FS::ClientAPI::MyAccount;
+
+use strict;
+use vars qw($cache);
+use Digest::MD5 qw(md5_hex);
+use Date::Format;
+use Cache::SharedMemoryCache; #store in db?
+use FS::CGI qw(small_custview); #doh
+use FS::Conf;
+use FS::Record qw(qsearchs);
+use FS::svc_acct;
+use FS::svc_domain;
+use FS::cust_main;
+use FS::cust_bill;
+
+use FS::ClientAPI; #hmm
+FS::ClientAPI->register_handlers(
+  'MyAccount/login'         => \&login,
+  'MyAccount/customer_info' => \&customer_info,
+  'MyAccount/invoice'       => \&invoice,
+);
+
+#store in db?
+my $cache = new Cache::SharedMemoryCache();
+
+#false laziness w/FS::ClientAPI::passwd::passwd (needs to handle encrypted pw)
+sub login {
+  my $p = shift;
+
+  my $svc_domain = qsearchs('svc_domain', { 'domain' => $p->{'domain'} } )
+    or return { error => "Domain not found" };
+
+  my $svc_acct =
+    ( length($p->{'password'}) < 13
+      && qsearchs( 'svc_acct', { 'username'  => $p->{'username'},
+                                 'domsvc'    => $svc_domain->svcnum,
+                                 '_password' => $p->{'password'}     } )
+    )
+    || qsearchs( 'svc_acct', { 'username'  => $p->{'username'},
+                               'domsvc'    => $svc_domain->svcnum,
+                               '_password' => $p->{'password'}     } );
+
+  unless ( $svc_acct ) { return { error => 'Incorrect password.' } }
+
+  my $session = {
+    'svcnum' => $svc_acct->svcnum,
+  };
+
+  my $cust_pkg = $svc_acct->cust_svc->cust_pkg;
+  if ( $cust_pkg ) {
+    my $cust_main = $cust_pkg->cust_main;
+    $session->{'custnum'} = $cust_main->custnum;
+  }
+
+  my $session_id;
+  do {
+    $session_id = md5_hex(md5_hex(time(). {}. rand(). $$))
+  } until ( ! defined $cache->get($session_id) ); #just in case
+
+  $cache->set( $session_id, $session, '1 hour' );
+
+  return { 'error'      => '',
+           'session_id' => $session_id,
+         };
+}
+
+sub customer_info {
+  my $p = shift;
+  my $session = $cache->get($p->{'session_id'})
+    or return { 'error' => "Can't resume session" }; #better error message
+
+  my %return;
+
+  my $custnum = $session->{'custnum'};
+
+  if ( $custnum ) { #customer record
+
+    my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
+      or return { 'error' => "unknown custnum $custnum" };
+
+    $return{balance} = $cust_main->balance;
+
+    my @open = map {
+                     {
+                       invnum => $_->invnum,
+                       date   => time2str("%b %o, %Y", $_->_date),
+                       owed   => $_->owed,
+                     };
+                   } $cust_main->open_cust_bill;
+    $return{open_invoices} = \@open;
+
+    my $conf = new FS::Conf;
+    $return{small_custview} =
+      small_custview( $cust_main, $conf->config('defaultcountry') );
+
+    $return{name} = $cust_main->first. ' '. $cust_main->get('last');
+
+  } else { #no customer record
+
+    my $svc_acct = qsearchs('svc_acct', { 'svcnum' => $session->{'svcnum'} } )
+      or die "unknown svcnum";
+    $return{name} = $svc_acct->email;
+
+  }
+
+
+  return { 'error'          => '',
+           'custnum'        => $custnum,
+           %return,
+         };
+
+}
+
+sub invoice {
+  my $p = shift;
+  my $session = $cache->get($p->{'session_id'})
+    or return { 'error' => "Can't resume session" }; #better error message
+
+  my $custnum = $session->{'custnum'};
+
+  my $invnum = $p->{'invnum'};
+
+  my $cust_bill = qsearchs('cust_bill', { 'invnum'  => $invnum,
+                                          'custnum' => $custnum } )
+    or return { 'error' => "Can't find invnum" };
+
+  #my %return;
+
+  return { 'error'        => '',
+           'invnum'       => $invnum,
+           'invoice_text' => join('', $cust_bill->print_text ),
+         };
+
+}
+
+
diff --git a/FS/FS/ClientAPI/passwd.pm b/FS/FS/ClientAPI/passwd.pm
new file mode 100644 (file)
index 0000000..2960622
--- /dev/null
@@ -0,0 +1,56 @@
+package FS::ClientAPI::passwd;
+
+use strict;
+use FS::Record qw(qsearchs);
+use FS::svc_acct;
+#use FS::svc_domain;
+
+use FS::ClientAPI; #hmm
+FS::ClientAPI->register_handlers(
+  'passwd/passwd' => \&passwd,
+  'passwd/chfn' => \&chfn,
+  'passwd/chsh' => \&chsh,
+);
+
+sub passwd {
+  my $packet = shift;
+
+  #my $domain = qsearchs('svc_domain', { 'domain' => $packet->{'domain'} } )
+  #  or return { error => "Domain $domain not found" };
+
+  my $old_password = $packet->{'old_password'};
+  my $new_password = $packet->{'new_password'};
+  my $new_gecos = $packet->{'new_gecos'};
+  my $new_shell = $packet->{'new_shell'};
+
+#false laziness w/FS::ClientAPI::MyAccount::login (needs to handle encrypted pw)
+  my $svc_acct =
+    ( length($old_password) < 13
+      && qsearchs( 'svc_acct', { 'username'  => $packet->{'username'},
+                                 #'domsvc'    => $svc_domain->svcnum,
+                                 '_password' => $old_password } )
+    )
+    || qsearchs( 'svc_acct', { 'username'  => $packet->{'username'},
+                               #'domsvc'    => $svc_domain->svcnum,
+                               '_password' => $old_password } );
+
+  unless ( $svc_acct ) { return { error => 'Incorrect password.' } }
+
+  my %hash = $svc_acct->hash;
+  my $new_svc_acct = new FS::svc_acct ( \%hash );
+  $new_svc_acct->setfield('_password', $new_password ) 
+    if $new_password && $new_password ne $old_password;
+  $new_svc_acct->setfield('finger',$new_gecos) if $new_gecos;
+  $new_svc_acct->setfield('shell',$new_shell) if $new_shell;
+  my $error = $new_svc_acct->replace($svc_acct);
+
+  return { error => $error };
+
+}
+
+sub chfn {}
+
+sub chsh {}
+
+1;
+
index a6a8d93..8355e40 100644 (file)
@@ -20,6 +20,9 @@ bin/freeside-reexport
 FS.pm
 FS/CGI.pm
 FS/InitHandler.pm
+FS/ClientAPI.pm
+FS/ClientAPI/passwd.pm
+FS/ClientAPI/MyAccount.pm
 FS/Conf.pm
 FS/ConfItem.pm
 FS/Record.pm
@@ -97,6 +100,7 @@ t/agent.t
 t/agent_type.t
 t/CGI.t
 t/InitHandler.t
+t/ClientAPI.t
 t/Conf.t
 t/ConfItem.t
 t/Record.t