X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2FClientAPI%2FAgent.pm;h=baa05caaf7b77b016b94cf298d8cfeac425988a9;hb=2c112f32561f23f9c538ace00db46659ce16da32;hp=f534a86858298b33044d08a06fdc3932a24e5476;hpb=0d1d2630e726ab4ce32bab9c3e4a889eee43fcb4;p=freeside.git diff --git a/FS/FS/ClientAPI/Agent.pm b/FS/FS/ClientAPI/Agent.pm index f534a8685..baa05caaf 100644 --- a/FS/FS/ClientAPI/Agent.pm +++ b/FS/FS/ClientAPI/Agent.pm @@ -4,24 +4,45 @@ package FS::ClientAPI::Agent; use strict; use vars qw($cache); +use subs qw(_cache); use Digest::MD5 qw(md5_hex); -use Cache::SharedMemoryCache; #store in db? use FS::Record qw(qsearchs); # qsearch dbdef dbh); +use FS::ClientAPI_SessionCache; use FS::agent; -use FS::cust_main qw(smart_search); +use FS::cust_main::Search qw(smart_search); +use FS::svc_domain; +use FS::svc_acct; + +sub _cache { + $cache ||= new FS::ClientAPI_SessionCache( { + 'namespace' => 'FS::ClientAPI::Agent', + } ); +} + +sub new_agent { + my $p = shift; + + my $conf = new FS::Conf; + return { error=>'Disabled' } unless $conf->exists('selfservice-agent_signup'); + + #add a customer record and set agent_custnum? -use FS::ClientAPI; -FS::ClientAPI->register_handlers( - 'Agent/agent_login' => \&agent_login, - 'Agent/agent_logout' => \&agent_logout, - 'Agent/agent_info' => \&agent_info, - 'Agent/agent_list_customers' => \&agent_list_customers, -); + my $agent = new FS::agent { + 'typenum' => $conf->config('selfservice-agent_signup-agent_type'), + 'agent' => $p->{'agent'}, + 'username' => $p->{'username'}, + '_password' => $p->{'password'}, + # + }; -#store in db? -my $cache = new Cache::SharedMemoryCache( { - 'namespace' => 'FS::ClientAPI::Agent', -} ); + my $error = $agent->insert; + + return { 'error' => $error } if $error; + + agent_login({ 'username' => $p->{'username'}, + 'password' => $p->{'password'}, + }); +} sub agent_login { my $p = shift; @@ -45,9 +66,9 @@ sub agent_login { my $session_id; do { $session_id = md5_hex(md5_hex(time(). {}. rand(). $$)) - } until ( ! defined $cache->get($session_id) ); #just in case + } until ( ! defined _cache->get($session_id) ); #just in case - $cache->set( $session_id, $session, '1 hour' ); + _cache->set( $session_id, $session, '1 hour' ); { 'error' => '', 'session_id' => $session_id, @@ -57,7 +78,7 @@ sub agent_login { sub agent_logout { my $p = shift; if ( $p->{'session_id'} ) { - $cache->remove($p->{'session_id'}); + _cache->remove($p->{'session_id'}); return { 'error' => '' }; } else { return { 'error' => "Can't resume session" }; #better error message @@ -67,7 +88,7 @@ sub agent_logout { sub agent_info { my $p = shift; - my $session = $cache->get($p->{'session_id'}) + my $session = _cache->get($p->{'session_id'}) or return { 'error' => "Can't resume session" }; #better error message #my %return; @@ -92,7 +113,7 @@ sub agent_info { sub agent_list_customers { my $p = shift; - my $session = $cache->get($p->{'session_id'}) + my $session = _cache->get($p->{'session_id'}) or return { 'error' => "Can't resume session" }; #better error message #my %return; @@ -119,7 +140,7 @@ sub agent_list_customers { my $cust_main = $_; my $hashref = $cust_main->hashref; $hashref->{$_} = $cust_main->$_() - foreach qw(name status statuscolor); + foreach qw(name status statuscolor status_label); delete $hashref->{$_} foreach qw( payinfo paycvv ); $hashref; } @cust_main @@ -128,3 +149,66 @@ sub agent_list_customers { } +sub check_username { + my $p = shift; + my($session, $agentnum, $svc_acct) = _session_agentnum_svc_acct($p); + return { 'error' => $session } unless ref($session); + + { 'error' => '', + #'username' => $username, + #'domain' => $domain, + 'available' => $svc_acct ? 0 : 1, + }; + +} + +sub _session_agentnum_svc_acct { + my $p = shift; + + my $session = _cache->get($p->{'session_id'}) + or return "Can't resume session"; #better error message + + my $username = $p->{'username'}; + + #XXX some way to default this per agent (by default product's service def?) + my $domain = $p->{'domain'}; + + my $svc_domain = qsearchs('svc_domain', { 'domain' => $domain } ) + or return { 'error' => 'Unknown domain' }; + + my $svc_acct = qsearchs('svc_acct', { 'username' => $username, + 'domsvc' => $svc_domain->svcnum, } ); + + ( $session, $session->{'agentnum'}, $svc_acct ); + +} + +sub _session_agentnum_cust_pkg { + my $p = shift; + my($session, $agentnum, $svc_acct) = _session_agentnum_svc_acct($p); + return $session unless ref($session); + return 'Account not found' unless $svc_acct; + my $cust_svc = $svc_acct->cust_svc; + return 'Unlinked account' unless $cust_svc->pkgnum; + my $cust_pkg = $cust_svc->cust_pkg; + return 'Not your account' unless $cust_pkg->cust_main->agentnum == $agentnum; + ($session, $agentnum, $cust_pkg); +} + +sub suspend_username { + my $p = shift; + my($session, $agentnum, $cust_pkg) = _session_agentnum_cust_pkg($p); + return { 'error' => $session } unless ref($session); + + return { 'error' => $cust_pkg->suspend }; +} + +sub unsuspend_username { + my $p = shift; + my($session, $agentnum, $cust_pkg) = _session_agentnum_cust_pkg($p); + return { 'error' => $session } unless ref($session); + + return { 'error' => $cust_pkg->unsuspend }; +} + +1;