RT# 39340 - fixed skin_info caching issue
[freeside.git] / FS / FS / ClientAPI / MyAccount.pm
index 1df91e4..c1b6ec2 100644 (file)
@@ -86,6 +86,8 @@ sub skin_info {
   my($context, $session, $custnum) = _custoragent_session_custnum($p);
   #return { 'error' => $session } if $context eq 'error';
 
+  my $domain = $session->{'domain'};
+
   my $agentnum = '';
   if ( $context eq 'customer' ) {
 
@@ -104,7 +106,7 @@ sub skin_info {
   $p->{'agentnum'} = $agentnum;
 
   my $conf = new FS::Conf;
-
+  my $timeout = $conf->config('selfservice-session_timeout') || '1 hour';
   #false laziness w/Signup.pm
 
   my $skin_info_cache_agent = _cache->get("skin_info_cache_agent$agentnum");
@@ -119,6 +121,8 @@ sub skin_info {
     warn "$me populating skin info cache for agentnum $agentnum\n"
       if $DEBUG > 1;
 
+    my $menu = $conf->config("ng_selfservice-menu", $agentnum );
+
     $skin_info_cache_agent = {
       'agentnum' => $agentnum,
       ( map { $_ => scalar( $conf->config($_, $agentnum) ) }
@@ -142,7 +146,93 @@ sub skin_info {
       ( map { $_ => join("\n", $conf->config("selfservice-$_", $agentnum ) ) }
         qw( head body_header body_footer company_address ) ),
       'money_char' => $conf->config("money_char") || '$',
-      'menu' => join("\n", $conf->config("ng_selfservice-menu", $agentnum ) ) ||
+      'menu'  => _menu($domain,$menu),
+    };
+
+    _cache->set("skin_info_cache_agent$agentnum", $skin_info_cache_agent, $timeout);
+
+  }
+
+  #{ %$skin_info_cache_agent };
+  $skin_info_cache_agent;
+
+}
+
+## checks if page is in menu listing, if not sends to main with error.
+sub check_access {
+ my $p = shift;
+ my $error;
+
+ return if $p->{'page'} eq "index.php";
+ return if $p->{'page'} eq "ip_login.php";
+
+ return if substr($p->{'page'}, 0, length("process_")) eq "process_";
+
+ my $conf = new FS::Conf;
+
+ my($context, $session, $custnum) = _custoragent_session_custnum($p);
+
+ my $domain = ref($session) ? $session->{'domain'} : '';
+
+ my $agentnum = '';
+ if ( $context eq 'customer' && $custnum ) {
+
+  my $sth = dbh->prepare('SELECT agentnum FROM cust_main WHERE custnum = ?')
+    or die dbh->errstr;
+
+  $sth->execute($custnum) or die $sth->errstr;
+
+  $agentnum = $sth->fetchrow_arrayref->[0]
+    or die "no agentnum for custnum $custnum";
+
+  #} elsif ( $context eq 'agent' ) {
+  } elsif ( defined($p->{'agentnum'}) and $p->{'agentnum'} =~ /^(\d+)$/ ) {
+    $agentnum = $1;
+  }
+  $p->{'agentnum'} = $agentnum;
+
+ my $menu = $conf->config("ng_selfservice-menu", $agentnum );
+
+ my $allowed_pages = _menu($domain,$menu);
+
+ my %allowed;
+ my @lines = split /\n/, $allowed_pages;
+ foreach my $line (@lines) {
+  chomp; # remove newlines
+  $line =~ s/^\s+//;  # remove leading whitespace
+  next unless length($line);
+  my (@pages) = split(/ /, $line, 2);
+  $allowed{$pages[0]} = $pages[1];
+ }
+
+ $error = "You do not have access to the page ".$allowed{$p->{page}} unless $allowed{$p->{page}};
+
+ return { 'error' => $error, };
+
+}
+
+sub _menu {
+ my $p = shift;
+ my $m = shift;
+
+ my $menu;
+
+ if ($p eq 'ip_mac') {
+   $menu =       'main.php Home
+
+                 payment.php Payments
+                 payment_cc.php Credit Card Payment
+                 payment_ach.php Electronic Check Payment
+                 payment_paypal.php PayPal Payment
+                 payment_webpay.php Webpay Payments
+
+                 docs.php FAQs
+
+                 logout.php Logout
+                ';
+ }
+ else {
+   $menu = join("\n", $m ) ||
                 'main.php Home
 
                  services.php Services
@@ -171,16 +261,31 @@ sub skin_info {
                  docs.php FAQs
 
                  logout.php Logout
-                ',
-    };
+                ';
+ }
+ return $menu;
+}
 
-    _cache->set("skin_info_cache_agent$agentnum", $skin_info_cache_agent);
+sub get_mac_address {
+  my $p = shift;
 
-  }
+## access radius exports acct tables to get mac
+  my @part_export = ();
+  @part_export = (
+    qsearch( 'part_export', { 'exporttype' => 'sqlradius' } ),
+    qsearch( 'part_export', { 'exporttype' => 'sqlradius_withdomain' } ),
+    qsearch( 'part_export', { 'exporttype' => 'broadband_sqlradius' } ),
+  );
 
-  #{ %$skin_info_cache_agent };
-  $skin_info_cache_agent;
+  my @sessions;
+  foreach my $part_export (@part_export) {
+    push @sessions, ( @{ $part_export->usage_sessions( {
+      'ip' => $p->{'ip'},
+      'session_status' => 'open',
+    } ) } );
+  }
 
+  return { 'mac_address' => $sessions[0]->{'callingstationid'}, };
 }
 
 sub login_info {
@@ -190,8 +295,8 @@ sub login_info {
 
   my %info = (
     %{ skin_info($p) },
-    'phone_login'  => $conf->exists('selfservice_server-phone_login'),
-    'single_domain'=> scalar($conf->config('selfservice_server-single_domain')),
+    'phone_login'      => $conf->exists('selfservice_server-phone_login'),
+    'single_domain'    => scalar($conf->config('selfservice_server-single_domain')),
     'banner_url'       => scalar($conf->config('selfservice-login_banner_url')),
     'banner_image_md5' => 
       md5_hex($conf->config_binary('selfservice-login_banner_image')),
@@ -236,6 +341,22 @@ sub login {
 
     $svc_x = $svc_phone;
 
+  } elsif ( $p->{'domain'} eq 'ip_mac' ) {
+
+      return { error => 'MAC address empty '.$p->{'username'} }
+        unless $p->{'username'};
+
+      my $mac_address = $p->{'username'};
+      $mac_address =~ s/[\:\,\-\. ]//g;
+      $mac_address =~ tr/[a-z]/[A-Z/;
+
+      my $svc_broadband = qsearchs( 'svc_broadband', { 'mac_addr' => $mac_address } );
+      return { error => 'MAC address not found '.$p->{'username'} }
+        unless $svc_broadband;
+      $svc_x = $svc_broadband;
+
+      $session->{'domain'} = $p->{'domain'};
+
   } elsif ( $p->{email}
               && (my $contact = FS::contact->by_selfservice_email($p->{email}))
           )
@@ -334,6 +455,7 @@ sub login {
 
   return { 'error'      => '',
            'session_id' => $session_id,
+           %$session,
          };
 }
 
@@ -600,6 +722,8 @@ sub customer_info_short {
     for (@cust_main_editable_fields) {
       $return{$_} = $cust_main->get($_);
     }
+    $return{$_} = $cust_main->masked($_) for qw/ss stateid/;
+
     #maybe a little more expensive, but it should be cached by now
     for (@location_editable_fields) {
       $return{$_} = $cust_main->bill_location->get($_);
@@ -723,7 +847,7 @@ sub edit_info {
     or return { 'error' => "unknown custnum $custnum" };
 
   my $conf = new FS::Conf;
-  if (($p->{payby} eq "CHEK" || $p->{payby} eq "DCHEK") && $conf->exists('selfservice-ACH_info_readonly')) {
+  if (($p->{payby} eq "CHEK" || $p->{payby} eq "DCHK") && $conf->exists('selfservice-ACH_info_readonly')) {
     return { 'error' => "You do not have authority to add a bank account" };
   }
 
@@ -935,6 +1059,7 @@ sub payment_info {
   $return{paybatch} = $return{payunique};  #back compat
 
   $return{credit_card_surcharge_percentage} = $conf->config('credit-card-surcharge-percentage', $cust_main->agentnum);
+  $return{credit_card_surcharge_flatfee} = $conf->config('credit-card-surcharge-flatfee', $cust_main->agentnum);
 
   return { 'error' => '',
            %return,
@@ -1016,13 +1141,10 @@ sub validate_payment {
       if $cust_main->paymask eq $payinfo;
 
     my $achonfile = 0;
-    foreach my $cust_payby ($cust_main->cust_payby('CHEK','DCHK')) {
-      if ( $cust_payby->paymask eq $payinfo ) {
-        $payinfo = $cust_payby->payinfo;
+      if ( $cust_main->paymask eq $payinfo ) {
+        $payinfo = $cust_main->payinfo;
         $achonfile = 1;
-        last;
       }
-    }
 
     if ($conf->exists('selfservice-ACH_info_readonly') && !$achonfile) {
       return { 'error' => "You are not allowed to change your payment information." };
@@ -1654,6 +1776,128 @@ sub payment_receipt {
          };
 }
 
+sub list_payby {
+  my $p = shift;
+
+  my($context, $session, $custnum) = _custoragent_session_custnum($p);
+  return { 'error' => $session } if $context eq 'error';
+
+  my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
+    or return { 'error' => "unknown custnum $custnum" };
+
+  return {
+    'payby' => [ map {
+                       my $cust_payby = $_;
+                       +{
+                          map { $_ => $cust_payby->$_ }
+                            qw( custpaybynum weight payby paymask paydate
+                                payname paystate paytype
+                              )
+                        };
+                     }
+                   $cust_main->cust_payby
+               ],
+  };
+}
+
+sub insert_payby {
+  my $p = shift;
+
+  my($context, $session, $custnum) = _custoragent_session_custnum($p);
+  return { 'error' => $session } if $context eq 'error';
+
+  #XXX payinfo1 + payinfo2 for CHEK?
+  #or take the opportunity to use separate, more well- named fields?
+  # my $payinfo;
+  # $p->{'payinfo1'} =~ /^([\dx]+)$/
+  #   or return { 'error' => "illegal account number ". $p->{'payinfo1'} };
+  # my $payinfo1 = $1;
+  #  $p->{'payinfo2'} =~ /^([\dx\.]+)$/ # . turned on by echeck-country CA ?
+  #   or return { 'error' => "illegal ABA/routing number ". $p->{'payinfo2'} };
+  # my $payinfo2 = $1;
+  # $payinfo = $payinfo1. '@'. $payinfo2;
+
+  my $cust_payby = new FS::cust_payby {
+    'custnum' => $custnum,
+    map { $_ => $p->{$_} } qw( weight payby payinfo paycvv paydate payname
+                               paystate paytype payip 
+                             ),
+  };
+
+  my $error = $cust_payby->insert;
+  if ( $error ) {
+    return { 'error' => $error };
+  } else {
+    return { 'custpaybynum' => $cust_payby->custpaybynum };
+  }
+  
+}
+
+sub update_payby {
+  my $p = shift;
+
+  my($context, $session, $custnum) = _custoragent_session_custnum($p);
+  return { 'error' => $session } if $context eq 'error';
+
+  my $cust_payby = qsearchs('cust_payby', {
+                              'custnum'      => $custnum,
+                              'custpaybynum' => $p->{'custpaybynum'},
+                           })
+    or return { 'error' => 'unknown custpaybynum '. $p->{'custpaybynum'} };
+
+  foreach my $field (
+    qw( weight payby payinfo paycvv paydate payname paystate paytype payip )
+  ) {
+    next unless exists($p->{$field});
+    $cust_payby->set($field,$p->{$field});
+  }
+
+  my $error = $cust_payby->replace;
+  if ( $error ) {
+    return { 'error' => $error };
+  } else {
+    return { 'custpaybynum' => $cust_payby->custpaybynum };
+  }
+  
+}
+
+sub verify_payby {
+  my $p = shift;
+
+  my($context, $session, $custnum) = _custoragent_session_custnum($p);
+  return { 'error' => $session } if $context eq 'error';
+
+  my $cust_payby = qsearchs('cust_payby', {
+                              'custnum'      => $custnum,
+                              'custpaybynum' => $p->{'custpaybynum'},
+                           })
+    or return { 'error' => 'unknown custpaybynum '. $p->{'custpaybynum'} };
+
+  return { 'error' => $cust_payby->verify };
+  
+}
+
+sub delete_payby {
+  my $p = shift;
+
+  my($context, $session, $custnum) = _custoragent_session_custnum($p);
+  return { 'error' => $session } if $context eq 'error';
+
+  my $cust_payby = qsearchs('cust_payby', {
+                              'custnum'      => $custnum,
+                              'custpaybynum' => $p->{'custpaybynum'},
+                           })
+    or return { 'error' => 'unknown custpaybynum '. $p->{'custpaybynum'} };
+
+  my $conf = new FS::Conf;
+  if (($cust_payby->payby eq "DCHK" || $cust_payby->payby eq "CHEK") && $conf->exists('selfservice-ACH_info_readonly')) {
+    return { 'error' => "Sorry you do not have permission to delete bank information." };
+  }
+  else {
+    return { 'error' => $cust_payby->delete };
+  }
+}
+
 sub cancel {
   my $p = shift;
   my $session = _cache->get($p->{'session_id'})
@@ -3760,4 +4004,3 @@ sub _custoragent_session_custnum {
 }
 
 1;
-